C# For Loop

WBOY
Release: 2024-09-03 15:10:40
Original
426 people have browsed it

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
}
Copy after login

Differentiating in Three Parts which include Countering in For Loop

  1. 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.
  2. Loop conditions: The condition part generally points to a parameter or a Boolean expression, which can only return from false to true.
  3. Loop steps: The Execution module for steps corresponds to an instruction to increment or decrement the flag counter.

Flow Diagram

C# For Loop

How For loop works in C#?

The C# For loop has three parts, as can be seen from the flow chart above:

  1. 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.
  2. 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.
  3. 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);
}
Copy after login
  1. 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
  2. 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
  3. 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);
}
}
}
Copy after login

Output:

C# For Loop

2. An infinite For loop

Code

using System;
public class Program
{
public static void Main()
{
for (  ;  ; )
{
Console.Write("x");
}
}
}
Copy after login

Output:

C# For Loop

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);
}
}
}
Copy after login

Output:

C# For Loop

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);
}
}
}
Copy after login

Output:

C# For Loop

The above is the detailed content of C# For Loop. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!