In c#, for loop is useful to execute a statement or a group of statements repeatedly until the defined condition returns true.
Generally, for loop is useful in c# applications to iterate and execute a certain block of statements repeatedly until the specified number of times.
Following is the syntax of defining for loop in c# programming language.
If you observe the above syntax, we defined a for loop with 3 parts: initialization, condition, and iterator, and these are separated with a semicolon (;).
For example, if we have a for loop to print the variable (i) value 4 times, the process flow of for loop will be as shown below.
As discussed, first we declared and assigned a value (1) to the variable i, then the condition (i <= 4) will be evaluated. Since the condition is true, then the statements within the loop will be executed.
After that, the iterator (i++) will be evaluated, and it will increase the value of a variable (i). Again the condition (i <= 4) checking will happen, and it will continue until the condition returns true.
Here the condition will return true till the variable i value becomes 4. After that, if i value becomes 5, then the condition (5 <= 4) will fail, and it returns false.
Following is the pictorial representation of for loop process flow diagram in the c# programming language.
Now we will see how to use for loop in c# programming language with examples.
Following is the example of using for loop in c# programming language to iterate or loop through a particular list of statements.
If you observe the above code, we defined a for loop to iterate over 4 times to print the value of variable i, and the following are the main parts of for loop.
When we execute the above c# program, we will get the result below.
If you observe the above result, for loop executed 4 times and printed the i variable value for 4 times.
In c# for loop, we can declare and initialize multiple variables and iterator expressions by separating with comma (,) operator.
Following is the example of using multiple variables and iterator expressions in c# for loop.
If you observe the above example, we defined two variables (i, j) and two iterator expressions (i++, j++) by separating them with a comma (,) operator.
When we execute the above c# program, we will get the result below.
This is how we can use multiple variables and multiple iterators in c# for loops based on our requirements.
In c#, by using the break keyword we can stop the execution of for loop statement based on our requirements.
Following is the example of stopping the execution of for loop using the break statement.
If you observe the above code, we used a break statement to exit for loop whenever the variable i value equals 3.
When we execute the above c# program, we will get the result below.
If you observe the above result, the loop execution has stopped automatically whenever the variable i value equals 3.
This is how we can use the break statement in for loop to terminate the execution of for loop based on our requirements.
Generally, the initializer, condition, and iterator parameters are optional to create for loop in c# programming language.
Following is the example of creating a for loop in c# programming language without an initializer and iterator.
If you observe the above example, we defined a for loop without any initializers and iterators.
When we execute the above c# program, we will get the result below.
This is how we can implement for loop in c# programming language without initializers and iterators based on our requirements.
If the condition parameter in for loop always returns true, the for loop will be infinite and runs forever. Even if we miss the condition parameter in for loop automatically, that loop will become an infinite loop.
The following are the different ways to make for loop an infinite loop in the c# programming language.
Following is the example of making a for loop as an infinite in the c# programming language.
If you observe the above code, the condition (i > 0), whatever we defined in for loop, will always become true and return infinite results.
In c#, we can create one for loop within another for loop based on our requirements. Following is the example of creating a nested for loop in the c# programming language.
If you observe the above example, we created a for loop within another loop and printed the values based on our requirements.
When we execute the above c# program, we will get the result below.
This is how we can create nested for loops in our c# programming language based on our requirements.