C# Do While
C# do while is a loop statement which run a block of statements until the condition returns false. The statements run first and then check the condition. So the statements run at least once.
Syntax:
do { a block of statement(s); }while(condition);
- In the statement, we'll run the statements first and then check the condition.
- If the condition returns true, we'll run the statements again.
- Then we'll check the condition again. The loop continues running until the condition returns false.
- If the condition is false, we'll run the following code of the do while statement.
- So comparing with while loop statement in the previous section, the statements in the do while loop run at least once.
![]() |
---|
The semi-colon at the end of do while statement cannot be omitted. |
To compare with while loop statement, we'll change the example 01-32-01 as follows.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System; namespace DoWhile1 { class Program { static void Main(string[] args) { const int MaxRun = 10; int i = 0; do{ Console.Write("{0} ", i); i++; }while( i < MaxRun ); 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(exclude itself) to be printed out.
- Line 10: Declare an int variable i and initialize it with 0.
- Line 12: Do while loop statement starts here. We'll run the statements in the curly brackets.
- Line 13: Output i's value 0.
- Line 14: i increments by 1.
- Line 15: It is the first time we check the condition. It returns true. So we will run statements (line 13-14) again. The we'll check the condition again until the condition returns false. After running 10 times, i = 10 and i < MaxRun returns false. Then the control goes to the next statement line 17.
- Line 17: Output a new line. See the result, 0-9 has been outputted.
What will be happened if we remove line 14 and make the increment happened in the condition? Check the example below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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; do Console.Write("{0} ", i); while( ++i < MaxRun ); Console.WriteLine(); Console.Read(); } } }
- Line 12-14: Here is the do while loop statement. The curly braces were omitted because there is only one statement running in the loop.
- Line 14: ++i instead of i++ were added because we need to increment first then compare.
The example below will show you how to output an inverted triangle of stars.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System; namespace WhileLoop2 { class Program { static void Main(string[] args) { const int Max = 9; int i = Max; do{ int j = 0; do{ Console.Write("*"); }while( ++j < i ); Console.WriteLine(); i--; }while( i > 0 ); Console.Read(); } } }
Output
********* ******** ******* ****** ***** **** *** ** *
- Line 9: Declare an int constant to hold the maximum lines to be printed.
- Line 10: Declare an int variable i to hold the amount of stars to be printed. The initial value is the constant Max.
- Line 12: Start the outer do while loop. In each loop, we output one line of stars.
- Line 13: Declare a variable j to calculate how many stars were already printed.
- Line 14: Start the nested do while loop to print the stars of each line.
- Line 15: Output one star.
- Line 16: If the condition returns true, we'll go back to line 14 to loop again. Otherwise we'll go to line 17.
- Line 17: Output a new line to make the current line printing over.
- Line 18: i decrements by 1 so at the next cycle we'll print less stars in the next line.
- Line 19: If i>0, the outer loop will be over otherwise we'll go to line 12 to start the next cycle.