C# For Loop
C# for loop is one of the loop statements in C#. It provides the language an ability to run the same pieces of code for a specific times.
Syntax:
for(init; condition; increment/decrement)
{
a block of statement(s);
}
- Step 1: We'll run the init part code ONCE, it is always used to define local variables which are only accessed in the for statement.
- Step 2: We'll evaluate the condition.
- Step 3: If the condition returns true, we'll run the block of statements in the loop.
- Step 4: We'll run increment/decrement code.
- Step 5: Repeat from the step 2 to step 4 until the condition returns false.
- Step 6: Run the next statement which follows the for statement if there is.
![]() |
---|
Initializer, condition or increment/decrement can be omitted or move to other places. |
To compare with while loop statement or do while 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
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System; namespace ForLoop1 { class Program { static void Main(string[] args) { const int MaxRun = 10; for(int i=0; i<MaxRun; i++){ Console.Write("{0} ", i); } Console.WriteLine(); int j=0; for(; j<MaxRun; j++){ Console.Write("{0} ", j); } Console.WriteLine(); for(int k=0; k<MaxRun; ){ Console.Write("{0} ", k); k++; } Console.WriteLine(); for(int m=0;;m++){ if(m<MaxRun){ Console.Write("{0} ", m); }else{ break; } } Console.WriteLine(); for(int n=MaxRun-1; n>=0; n--){ Console.Write("{0} ", n); } Console.WriteLine(); Console.Read(); } } }
Output
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 0
- Line 9: Declare a constant MaxRun to hold the maximum loop times.
- Line 11-13: For loop statement. Step 1 is to declare a local variable i=0; Step 2 is to run condition i<MaxRun which returns true; Step 3 is to run the statement in line 12 to output 0. Step 4 is run i++ then i=1; Step 5 is to run from step 2 to step 4 until i=10 and the condition returns false;
- Line 14: Output a new line.
- Line 16: the initializer runs first to declare a variable j=0;
- Line 17-19: This is a for statement with initializer is an empty statement.
- Line 22-25: This is another for statement. The increment statement moves to the last statement of the for loop.
- Line 28-34: The condition in the for statement moves inside. If line 29 returns false, we'll run break statement to get out of the loop. For details, you can check the next section.
- Line 37-39: We use a for statement to print the digits from 9 to 0 and each loop the local variable n decrements by 1 until n is a negative number.
In the following example, we'll use nested for statement to print a multiplication table.
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 WhileLoop2 { class Program { static void Main(string[] args) { const int Max = 10; for(int i=1; i<Max; i++){ for(int j=1; j<Max; j++){ Console.Write("{0}x{1}={2} ", i, j, i*j); } Console.WriteLine(); } Console.Read(); } } }
Output
1x1=1 1x2=2 1x3=3 1x4=4 1x5=5 1x6=6 1x7=7 1x8=8 1x9=9 2x1=2 2x2=4 2x3=6 2x4=8 2x5=10 2x6=12 2x7=14 2x8=16 2x9=18 3x1=3 3x2=6 3x3=9 3x4=12 3x5=15 3x6=18 3x7=21 3x8=24 3x9=27 4x1=4 4x2=8 4x3=12 4x4=16 4x5=20 4x6=24 4x7=28 4x8=32 4x9=36 5x1=5 5x2=10 5x3=15 5x4=20 5x5=25 5x6=30 5x7=35 5x8=40 5x9=45 6x1=6 6x2=12 6x3=18 6x4=24 6x5=30 6x6=36 6x7=42 6x8=48 6x9=54 7x1=7 7x2=14 7x3=21 7x4=28 7x5=35 7x6=42 7x7=49 7x8=56 7x9=63 8x1=8 8x2=16 8x3=24 8x4=32 8x5=40 8x6=48 8x7=56 8x8=64 8x9=72 9x1=9 9x2=18 9x3=27 9x4=36 9x5=45 9x6=54 9x7=63 9x8=72 9x9=81
- Line 9: Declare an int constant to hold the maximum times to loop.
- Line 11: Start outer for loop and define an int variable i.
- Line 12: Start inner for loop and define an int variable j.
- Line 13: Output i times j result.
- Line 14: End of the inner loop.
- Line 15: Output a new line.
- Line 16: End of the outer loop.
ixj is the same as jxi, so half of the above result do not need outputting. The example is changed 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 WhileLoop2 { class Program { static void Main(string[] args) { const int Max = 10; for(int i=1; i<Max; i++){ for(int j=1; j<=i; j++){ Console.Write("{0}x{1}={2} ", i, j, i*j); } Console.WriteLine(); } Console.Read(); } } }
Output
1x1=1 2x1=2 2x2=4 3x1=3 3x2=6 3x3=9 4x1=4 4x2=8 4x3=12 4x4=16 5x1=5 5x2=10 5x3=15 5x4=20 5x5=25 6x1=6 6x2=12 6x3=18 6x4=24 6x5=30 6x6=36 7x1=7 7x2=14 7x3=21 7x4=28 7x5=35 7x6=42 7x7=49 8x1=8 8x2=16 8x3=24 8x4=32 8x5=40 8x6=48 8x7=56 8x8=64 9x1=9 9x2=18 9x3=27 9x4=36 9x5=45 9x6=54 9x7=63 9x8=72 9x9=81
Please be noted, only j<Max in line 12 was changed to j<=i.