C# While Loop

C# while loop is a statement which keeps running a block of statements until condition is false. The condition will be checked before the block of statements run.

Syntax:

while(condition)
{
   a block of statement(s);
}

C# while loop diagram

  • In above diagram, the condition is a boolean expression and evaluated first.
  • If the result of the condition returns true, a block of statements will be executed.
  • After the statements are executed, we'll check the condition again.
  • If the condition returns true again, the block of statements will be executed again.
  • We keep checking the condition and running the statements until the condition returns false.
  • If the condition returns false, we'll the following code after the while loop.
  • If the condition is return false at the first cycle, the block of statements will never be run and the control will go to the next code of the while loop statement.

In the following example, we'll output decimal digit from 0 to 9.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;

namespace WhileLoop1
{
    class Program
    {
        static void Main(string[] args)
        {
            const int MaxRun = 10;
            int i = 0;

            while( i < MaxRun ){
                Console.Write("{0} ", i);
                i++;
            }
            Console.WriteLine();
            Console.Read();
        }
    }
}

Output

0 1 2 3 4 5 6 7 8 9 
  • Line 9: Declare a constant MaxRun to hold the maximum number to be printed out. It will not include itself.
  • Line 10: Declare an int variable i and assign it with 0.
  • Line 12: While loop statement started here. Firstly, we'll check if i
  • Line 13-14: Then we'll run the statements in the curly brackets. In line 13, we'll output the i's value 0. Then in line 14, we'll increment i by 1. So after the statement, i is 1. Then we'll check the condition again, 1
  • Line 16: Output a new line. So you can check the result, 0-9 has been outputted.

What will be happened if we combine the line 13-14 together into one line? Check the example below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;

namespace WhileLoop1
{
    class Program
    {
        static void Main(string[] args)
        {
            const int MaxRun = 10;
            int i = 0;

            while( i < MaxRun ){
                Console.Write("{0} ", i++);
            }
            Console.WriteLine();
            Console.Read();
        }
    }
}

In line 12, right after we output i, i is incremented by 1. So the result is the same as before.

In the following example, we'll show you how to output a triangle stars by a nested while statement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System;

namespace WhileLoop2
{
    class Program
    {
        static void Main(string[] args)
        {
            const int Max = 10;
            int i = 1;

            while( i < Max ){
                int j = 0;
                while( j++ < i )
                    Console.Write("*");
                Console.WriteLine();
                i++;
            }
            Console.Read();
        }
    }
}

Output

*
**
***
****
*****
******
*******
********
*********
  • Line 9: Declare an int constant to hold the maximum outputted lines.
  • Line 10: Declare an int variable i to hold current the amount of output stars.
  • Line 12: Start the outer while loop. In each loop, we output one line of stars.
  • Line 13: Declare a variable j to calculate how many stars were already outputted.
  • Line 14-15: Start the nested while loop to print the stars of each line. At the first time, i=1 and j=0 so we loop once to output one star at the first line. Pay attention, after comparing in the conditon, j is incremneted by 1 and the curly braces were omited because there is only one statement in the loop.
  • Line 16: Output a new line so that we'll prepare to output the stars of the next line.
  • Line 17: i increments by 1. Then We'll check the outer loop conditon and start the next cycle process.