find and solve || findandsolve.com
Please wait.....

Welcome back.






Before Read Terms of use

C# For Loop - With Example



The for loop is one the most popular loops.The condition in the for loop is checked at the beginning of the loop.This implies that the set of statments inside the for loop is executed only when the condition is true.Therefore,The set of statements inside the for loop is executed zero or more number of times.The for loop is suitable either to traverse through the elements of arrays or to execute a set of statements predefined number of times.


The syntax of the for loop is as follows:

for(initializer;condition;update)
{
//statements
}

In the preceding syntax,

for :Refers to the for keyword that indicates a for loop.
initializer :Refers to the initializer expression,which is an expression that initializes the loop control variable(s). A loop control variable is a local variable whose value controls the execution of the for loop
condition: Refers to the condition expression,which is a Boolean expression.This expression uses the loop control variable to determine whether the set of statements inside the for loop is executed or not.
update: Refers to the update expression,which is an expression that increments or decrements the value of the loop control variable. This expression is executed at the end of each iteration of the for loop.
statements: Refers to the set of statements inside the for loop that is executed when the condition expression evaluates to true.


When the for loop executes,the initializer expression is executed first. After this,the condition expression is executed. If the execution of update expression. The control again passes to the condition expression,where it is evaluated again.This process continues as long as the condition is true. When the condition becomes false,the for loop is terminated and the control goes to the statement that succeeds the for loop.Note that the initializer expression executes only one;whereas,the condition and update expressions may execute more than once.

For Example



using System;
public class Program
{
public static void Main()
{
int i;
for(i=1;i<=10;i++)
{
Console.WriteLine("Iteration " +i);
}
Console.WriteLine("For loop terminated");
Console.ReadLine();
}
}


In the code above,we first declare an int type variable,i.The i variable acts as the loop control variable for the loop.It is declared before the for loop but initialized to 1 in the for loop. After initializing the i variable,the for loop checks the expression i<=10,which is the condition of the for loop.If this condition is true,then the number of current iteration is displayed.After this,the i variable is incremented by 1. This completes one iteration of the for loop. The control now goes to the condition where the updated value of the i variable is checked again. In this way,the for loop iteration, 10 times.At the end of the tenth iteration,the i variable is equal to 11.Therefore,in the eleventh iteration,the condition becomes false.As a result,the control terminates the for loop and the message,for loop terminated,is displayed.
Note that you can omit any or all of the initializer,condition,or update expressions.However,you cannot omit the semicolon that separates the three expression.You can omit the initilizer expression only when you declare and initialize the loop control variable before the for loop.If you omit the keeps executing infinitely.The loops that keeps executing forever are called infinite loops.you can also create an infinite loop by omitting the update expression of the for loop. However,you should avoid creating infinite loops while developing programs
Let's look at the example of an infinite for loop given in the following code snippet:

for(int i=0;i < 3;)
{
Console.WriteLine("infinite loop");
}

In the preceding code snippet,the loop control variable,i is declared and initialized to zero .In the for loop,the expression i < 3 is the condition.Note that there is no update expression in the for loop. Therefore, in this case,the i < 3 condition is always true because the value of the i variable does not change and is always less than 3. This implies that this for loop executes infinite number of times,displaying the message infinite loop.




Sundar  Neupane

Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.

Comments



Report Response