Updated March 16, 2023
Introduction to 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
}
Differentiating in Three Parts which include Countering in For Loop
- Variable initialization and Assignment: This step is used to declare and initialize a counter flag used to invalidate a conditional expression and trigger a sequence for the program to be executed in the STEPS present in the execution module.
- Loop conditions: The condition part generally points to a parameter or a Boolean expression, which can only return from false to true.
- Loop steps: The Execution module for steps corresponds to an instruction to increment or decrement the flag counter.
Flow Diagram
How For loop works in C#?
The C# For loop has three parts, as can be seen from the flow chart above:
- The initialization statement is instantiated first. Here, the variable is usually declared and initialized. This variable is referred to as a counter flag as it governs the number of iteration for which the loop shall be executed. This counter can be an input parameter with associated initial or pre-value or an explicit counter register with initial value defined in the for syntax.
- The next steps in the evaluation of the FOR condition. The condition is a boolean expression, i.e. it returns either true or false. If the condition results to true, then the statements /Programs inside the For loop are executed. followed by the iterator statement, which changes the value of the initialized variable/Program counter, and Again, the condition assigned to the for syntax is validated. These iterations continue until the condition is evaluated to false.
- As soon as the condition evaluates to false the flow moves from within the loop to outside the block.
Let us try to understand the concept using a program:
Syntax:
for (int i = 0; i <= 5; i++)
{
Console.WriteLine("i value: {0}", i);
}
- Here int i = 1 is the initialization part; the loop initializes a counter flag for the parameter I, which is explicitly defined in the For loop syntax
- I <= 5 is the condition part; the condition evaluates to true as long as the value of I is less than or equal to 5
- i++ is the iterator part and points to post-increment of the pointer as soon as the iteration is completed once
1. A simple Iterative For loop
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:
2. An infinite For loop
Code
using System;
public class Program
{
public static void Main()
{
for ( ; ; )
{
Console.Write("x");
}
}
}
Output:
3. Breaks in Loop
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:
4. Nested For loops
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:
Recommended Articles
This has been a guide to C# For Loop. Here we discuss how For loop works in C#? three parts: countering in For loop with the Flow diagram, codes, syntax, and outputs. You can also go through our other suggested articles to learn more –