For iteration purposes Loops are used, i.e., doing a task multiple times (usually until a termination condition is met). The most common type of loop in C# is the for a loop. The basic structure of a for loop is strictly equivalent as in Java and C/C++ and is:
for(assignment; condition; increment/decrement){
// block of code
}
Let's see how a for loop writes the integers from 1 to 100 on the console: and make some understanding about For loop.
for(int i=1; i<=10; i++)
{
Console.Write(i + ",");
}
1,2,3,4,5,6,7,8,9,10,
In the beginning, the integer variable i is initialized with the worth of 1. The statements within the for loop are executed while
the condition (i<= 100
) remains true. Every time when the loop starts the value of i
incremented by 1.
All three statements for()
, assignment, condition, and increment/decrement are optional. You can use any combination of those and even decide to not use anybody in the least. This would make what's called an indefinite or infinite loop’ (a loop which will never end until or unless the break instruction is encountered inside the body of the loop). But, you continue to need to supply the acceptable semicolons:
for(; ;)
for( ; i<100; i++)
for(int i=0; ; i--)
for( ; i>100; )
If you don’t use the {}
brackets, the statement immediately following for() will be treated as the iteration statement.
The example below is just like the one given above:
for(int i=1; i<=100; i++)
Console.WriteLine(i);
I will again recommend that you simply always use {}
brackets and proper indentation. If you declare a variable in for()
’s assignment, its life (scope) will only last inside the loop and it'll die after the loop body terminates (unlike some implementations of C++). Hence if you write:
for(int i=1; i<=100; i++)
{
Console.WriteLine(i);
}
i++; //suppose this is line 5
The compiler will complain at line 5 that "i is an undeclared identifier". You can use a break and continue certain loops or the other loop to vary the normal execution path. break terminates the loop and transfers the execution to some extent just outside the for loop:
for(int i=1; i<=10; i++)
{
if(i>5)
{
break;
}
Console.WriteLine(i);
}
1
2
3
4
5
when the value of "i
" gets greater than 5 loops will terminate. If some statements are present after the break
, the break
must be enclosed under some condition, otherwise the lines the following break will become unreachable and In this case, visual studio will generate a warning (in Java, it’s a syntax error).
for(int i=0; i<100; i++)
{
break; // warning, Console.WriteLine (i); is unreachable code
Console.WriteLine(i);
}
in the case of continue, it will ignore the remaining part of the iteration and starts the next iteration.
for(int i=0; i<=10; i++)
{
if(i==5)
{
continue;
}
Console.WriteLine(i);
}
0
1
2
3
4
6
7
8
9
10
As we already discussed nested if statement so we have some understanding about nested for loop. A loop within the loop is known as nested. For example, we have to implement a for loop and within the for loop, we also want to implement iteration then we will use nested for loop. Let's make some understanding with the help of an example.
for(int i=0; i<5; i++)
{
for(int j=0; j<=i; j++){
Console.WriteLine(i);
}
}
0
1
1
2
2
2
3
3
3
3
4
4
4
4
4
First of all, our first loop will start after the First loop our nested loop will be called when the value of i = 0 our out should be 0 in the second round when the value of i =1 then the output should 0,1 and so on.