|
|
for loop in C programmingLoops are used to iterate program execution based on some conditions and requirements. For loop is widely used when we require a set of statements are to be executed for a number of times. Here some examples of for loops.
#include <stdio.h>
int main(void){
for(int i=0; i<=3; i++){
printf("\n %d",i); // printing the value of i
}
return 0;
}
The above code will give this output.
0 1 2 3
As you have seen we have declared the variable i in the for loop and used it. So this value is localized within this loop.
Nested for loops
We will use one inside other for loop to check how the value of i changes, here is the code.
The output of the above code is here .
#include <stdio.h>
int main(void){
for(int i=0; i<=2; i++){
printf("\n outside = %d",i);
for(int i=0; i<=4; i++){
printf("\n inside = %d",i);
}
}
return 0;
}
The output is here
outside = 0
inside = 1
inside = 2
inside = 3
inside = 4
outside = 1
inside = 1
inside = 2
inside = 3
inside = 4
outside = 2
inside = 1
inside = 2
inside = 3
inside = 4
| |
| |
|
|
|
|
|