Home > Article > Backend Development > C# For Loop
In the fundamentals of computer programming languages, a loop can be considered a chain or sequence of executive instructions continually performed/repeated/ or iterated till a certain condition is achieved. Loops are used for repeatedly executing a block of statements; in the classification of flow in loops, we can determine if a loop is an entry-controlled loop or if it is exit controlled loop, The programming compiler, before Executing the Statements, checks if all conditions associated with the beginning of the loops executions are validated and true, this activity is Performed by Entry controlled loops. In this topic, we are going to learn about C# For Loop
For Exit Controlled Loop, the compiler validates the Conditions associated with the termination of Loop. During the flow of the loops, a certain process is completed, such as getting a parameter or any item from the input data, analysis of the parameters, or changing it, followed by a condition that checks the limiting value, such as whether a counter(a flag value associated with the loop).
If the conditions are satisfied, the subsequent instruction directs the sequence to return to the first instruction in the sequence and repeat the sequence. Once the condition has reached, the flow of the compiler moves to programmed instruction branches present outside the loop. Thus a loop is a programming concept that is commonly used for reducing code redundancy and complexity.
In reference to For loop, it can be considered an iterative block based on a FOR condition and then progress to instantiate the execution of steps specified by a code sequence as long as these conditions are satisfied. The loop can be differentiated from other loops on the basis of a presence of a loop counter variable or a parameterized loop variable that helps in iterating through the body of the loop for the exact match and satisfying the sequence of each iteration.
Syntax:
for (counter/variable initialize; termination condition; iteration steps) { // code block to be executed as long as condition is satisfied }
Flow Diagram
The C# For loop has three parts, as can be seen from the flow chart above:
Let us try to understand the concept using a program:
Syntax:
for (int i = 0; i <= 5; i++) { Console.WriteLine("i value: {0}", i); }
Code:
using System; public class Program { public static void Main() { for (int i = 0; i < 8; i++) { Console.WriteLine("Value of i: {0}", i); } } }
Output:
Code
using System; public class Program { public static void Main() { for ( ; ; ) { Console.Write("x"); } } }
Output:
Code
using System; public class Program { public static void Main() { for (int i = 0; i < 8; i++) { if( i == 4 ) break; Console.WriteLine("Value of i: {0}", i); } } }
Output:
Code
using System; public class Program { public static void Main() { for (int p = 0; p < 3; p++) {for(int q =5; q>= 0; q--) Console.WriteLine("Value of p: {0}, Q: {1} ", p,q); } } }
Output:
The above is the detailed content of C# For Loop. For more information, please follow other related articles on the PHP Chinese website!