The for loop of C#

The for loop is one of the available loops in C sharp programming language. We use for loop to execute a given block of code again and again to the given number of times. You have to set the counter that will be checked in each iteration against a given condition, followed by the increment or decrement operator(counter update). Others loop types are the while and do..while.

Following are a few examples of using for C# but let us first look at its structure.

Syntax of C# for loop

Following is the general syntax to use for loop in C#:

for ( counter_initialization; condition; update_the_counter){

//Code to be executed;

}

Where:

The for keyword is followed by parenthesis.

The counter_initialization sets the initial value of the counter in the for loop which is executed only once.

The condition, which is the second statement in for loop, will be evaluated in each iteration. If the condition is true, the given block of code will be executed inside the for loop.

After that, an optional update_the_counter part that can be an increment or decrement operator.

Now let us look at a few examples of using for loop in C#.

A for loop in C# example

Following is an example of using for in C#. In this example, we have declared an integer type variable with a value of one. This value is incremented by one in the for loop. Click the following link to see the code and output of the above example.

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

usingSystem;

namespacecsharp_demo

{

classProgram

{

staticvoidMain(string[]args)

{

intinta;

for(inta=1;inta<=10;inta++)

{

Console.WriteLine(“Current value of inta = “+inta);

}

Console.ReadLine();

}

}

}

You can see, the loop is iterated ten times. As inta variable reaches 11 value and it meets the condition check, that turns to be false. As soon as the condition becomes false the execution will go out of the for loop to the next line, which is Console.ReadLine(); in that case.

A for loop example with decrement operator example

As mentioned earlier you can use increment or decrement operator in the for loop of C#. In this example, the variable is initialized with a value of 10. In the each iteration, the value will be decremented by -1. See the output and code by clicking the link below:

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

usingSystem;

namespacecsharp_demo

{

classProgram

{

staticvoidMain(string[]args)

{

intinta;

for(inta=10;inta>0;inta—)

{

Console.WriteLine(“Current value of inta = “+inta);

}

Console.ReadLine();

}

}

}

You can see values of variable inta is displayed from 10 to 1 by using decrement in the counter update of the for loop.

C# continue statement with for loop example

Sometimes it is required to skip one or more iterations in the for loop, however, still keep on running the loop till the end. The continue statement can be used for that purpose.

In this example, we will use continue in the for loop. An int type variable is initiated with the value of 1 and will increment by 1 in each iteration till it reaches the value of 10. As the value of inta variable reaches 4 (after execution) an if condition will check it and we will increment it by 3 followed by continue statement.

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

usingSystem;

namespacecsharp_demo

{

classProgram

{

staticvoidMain(string[]args)

{

intinta;

for(inta=1;inta<=10;inta++)

{

Console.WriteLine(“Current value of inta = “+inta);// C# loop example

if(inta==4)

{

/* Using continue */

inta=inta+3;

continue;

}

}

Console.ReadLine();

}

}

}

You can see 5,6 and 7 will be omitted in the for loop due to increment in the if statement. While the for loop was still iterating by using continue statement till the condition turned false.

If you need to end the loop as the given condition is met you can use C# break statement. See example below.

C# break statement in the for loop

You can use C# break statement to stop the loop and execution will move out of the C# for loop. See the following example of using C# break statement.

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

usingSystem;

namespacecsharp_demo

{

classProgram

{

staticvoidMain(string[]args)

{

intinta;

for(inta=1;inta<=10;inta++)

{

Console.WriteLine(“Current value of inta = “+inta);// C# loop example

if(inta==4)

{

/* Using break */

break;

}

}

Console.ReadLine();

}

}

}

You can see, unlike the continue command, the break statement will stop the for loop and control will be moved to next line of code, outside of the for loop.