Anda di halaman 1dari 14

C# for loop:

The for keyword indicates a loop in C#. The for loop executes a block of


statements repeatedly until the specified condition returns false.

Syntax:
for (variable initialization; condition; steps)
{
//execute this code block as long as condition is satisfied
}
As per the syntax above, the for loop contains three parts: initialization,
conditional expression and steps, which are separated by a semicolon.

1. variable initialization: Declare & initialize a variable here which will be


used in conditional expression and steps part.
2. condition: The condition is a boolean expression which will return either
true or false.
3. steps: The steps defines the incremental or decremental part

Consider the following example of a simple for loop.

Example: for loop


for (int i = 0; i < 10; i++)
{
Console.WriteLine("Value of i: {0}", i);
}

Try it
Output:

Value of i: 0  

Value of i: 1 

Value of i: 2 

Value of i: 3 

Value of i: 4 
Value of i: 5  

Value of i: 6 

Value of i: 7 

Value of i: 8 

Value of i: 9

The below figure illustrates the execution steps of above example.

for loop execution


steps
As you can see in the above example, first step is to declare & initialize an int
type variable. The second step is to check the condition. The third step is to
execute the code block if the 'if' condition returns true. The fourth step is to
increment the int variable and last step is to eveluate the condition again and
repeat the steps.

It is not necessary to put the initialization, condition and steps into brackets.
You can initialize a variable before the 'for' loop, and the condition and steps
can be defined inside the for loop.

Example: for loop C#

int i = 0;

for(;;)
{
if (i < 10)
{
Console.WriteLine("Value of i: {0}", i);
i++;
}
else
break;
}

Try it
Output:

Value of i: 0  

Value of i: 1 

Value of i: 2 

Value of i: 3 

Value of i: 4 

Value of i: 5 

Value of i: 6 

Value of i: 7 

Value of i: 8 

Value of i: 9

Infinite loop:
Be careful with infinite loop. It will be an infinite loop if for loop does not contain
initialization, condition or steps part. Also, make sure that conditional
expression will return false at some point of time to stop the looping.

Example: Infinite loop

for ( ; ; )
{
Console.Write(1);
}
Output:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1.....

The control variable for the for loop can be of any numeric data type, such as
double, decimal, etc.

Example: for loop


for (double d = 1.01D; d < 1.10; d+= 0.01D)
{
Console.WriteLine("Value of i: {0}", d);
}

Try it
Output:

Value of i: 1.01  

Value of i: 1.02 

Value of i: 1.03 

Value of i: 1.04 

Value of i: 1.05 

Value of i: 1.06 

Value of i: 1.07 

Value of i: 1.08 

Value of i: 1.09

The steps part in a for loop can either increase or decrease the value of a
variable.

Example: for loop


for(int i = 10; i> 0;i--)
{
Console.WriteLine("Value of i: {0}", i);
}

Try it
Output:

Value of i: 10  

Value of i: 9 

Value of i: 8 

Value of i: 7 

Value of i: 6 

Value of i: 5 

Value of i: 4 

Value of i: 3 

Value of i: 2

Value of i: 1

Break:
You can also exit from a for loop by using the break keyword.

Example: break in for loop


for (int i = 0; i < 10; i++)
{
if( i == 5 )
break;

Console.WriteLine("Value of i: {0}", i);


}
Try it
Output:

Value of i: 0  

Value of i: 1 

Value of i: 2 

Value of i: 3 

Value of i: 4

Nested For loop:


C# allows a for loop inside another for loop.

Example: Nested for loop


for (int i = 0; i < 10; i++)
{
for(int j =i; j< 10; j++)
Console.WriteLine("Value of i: {0}, J: {1} ", i,j);
}

Try it
Output:

Value of i: 0 , j: 0  

Value of i: 0 , j: 1 

Value of i: 0 , j: 2 

Value of i: 0 , j: 3 

Value of i: 0 , j: 4 
Value of i: 0 , j: 5  

Value of i: 0 , j: 6 

Value of i: 0 , j: 7 

Value of i: 0 , j: 8 

Value of i: 0 , j: 9 

Value of i: 1 , j: 1 

Value of i: 1 , j: 2 

Value of i: 1 , j: 3 

Value of i: 1 , j: 4 

Value of i: 1 , j: 5 

Value of i: 1 , j: 6 

Value of i: 1 , j: 7 

Value of i: 1 , j: 8 

Value of i: 1 , j: 9 

Value of i: 2 , j: 2 

Value of i: 2 , j: 3 

Value of i: 2 , j: 4 
Value of i: 2 , j: 5  

Value of i: 2 , j: 6 

Value of i: 2 , j: 7  

Points to Remember :
1. The for loop executes the block of code repeatedly.
2. The for loop has three steps: initialization, condition and
increment/decrement.
3. The for loop can use control variable of any numeric data type.
4. Use break keyword to stop the execution and exit from for loop.
5. Nested for loop is allowed in C#.

C# while loop:
C# includes the while loop to execute a block of code repeatedly.

Syntax:
While(boolean expression)
{
//execute code as long as condition returns true

}
As per the while loop syntax, the while loop includes a boolean expression as a
condition which will return true or false. It executes the code block, as long as
the specified conditional expression returns true. Here, the initialization should
be done before the loop starts and increment or decrement steps should be
inside the loop.

Example: while loop in C#


int i = 0;

while (i < 10)


{
Console.WriteLine("Value of i: {0}", i);
i++;
}

Try it
Output:

Value of i: 0  

Value of i: 1 

Value of i: 2 

Value of i: 3 

Value of i: 4 

Value of i: 5 

Value of i: 6 

Value of i: 7 

Value of i: 8 

Value of i: 9

In the above example, while loop inclues an expression  i < 10 . Inside while
loop, value of i increased to 1 (using  i++ ). So, the above while loop will be
executed till the value of i will be 10.

Use the break keyword to exit from a while loop as shown below.

Example: break in while loop


int i = 0;

while (true)
{
Console.WriteLine("Value of i: {0}", i);
i++;

if (i > 10)
break;
}

Try it
Output:

Value of i: 0

Nested while loop:


Nested while loop is allowed in C#

Example: Nested while loop


int i = 0;

while (i < 2)
{
Console.WriteLine("Value of i: {0}", i);
int j = 1;

i++;

while (j < 2)
{
Console.WriteLine("Value of j: {0}", j);
j++;
}
}

Try it
Output:

Value of i: 0  

Value of j: 1 

Value of i: 1 

Value of j: 1 
Note :Please make sure that conditional expression evaluates to false at some
point to avoid infinite loop.
 

Points to Remember :
1. The while loop executes the block of code repeatedly.
2. The while loop includes condition expression. Increment/decrement step
should be inside the loop.
3. Use break keyword to stop the execution and exit from while loop.
4. An nested while loop is allowed.

C# do-while:
The do-while loop is the same as a 'while' loop except that the block of code will
be executed at least once, because it first executes the block of code and then it
checks the condition.

Syntax:
do
{
//execute code block

} while(boolean expression);

As per the syntax above, do-while loop starts with the 'do' keyword followed by
a code block and boolean expression with 'while'.

Example: do while loop

int i = 0;

do
{
Console.WriteLine("Value of i: {0}", i);

i++;

} while (i < 10);


Try it
Output:

Value of i: 0  

Value of i: 1 

Value of i: 2 

Value of i: 3 

Value of i: 4 

Value of i: 5 

Value of i: 6 

Value of i: 7 

Value of i: 8 

Value of i: 9

Just as in the case of the for and while loops, you can break out of the do-while
loop using the break keyword.

Example: break inside do-while


int i = 0;

do
{
Console.WriteLine("Value of i: {0}", i);

i++;

if (i > 5)
break;

} while (true);
Try it
Output:

Value of i: 0  

Value of i: 1 

Value of i: 2 

Value of i: 3 

Value of i: 4 

Value of i: 5

Nested do-while loop:


The do-while loop can be used inside another do-while loop.

Example: Nested do while loop


int i = 0;

do
{
Console.WriteLine("Value of i: {0}", i);
int j = i;

i++;

do
{
Console.WriteLine("Value of j: {0}", j);
j++;

} while (j < 2);

} while (i < 2);

Try it
Output:
Value of i: 0  

Value of j: 0

Value of j: 1

Value of i: 1

Value of j: 1  

Points to Remember :
1. The do-while loop executes the block of code repeatedly.
2. The do-while loop execute the code atleast once. It includes the
conditional expression after the code block and the increment/decrement
step should be inside the loop.
3. Use the break keyword to stop the execution and exit from a do-while
loop.
4. An nested do-while loop is allowed.

Anda mungkin juga menyukai