SQL PHP HTML ASP JavaScript articles and free scripts to download
 

for loop in C programming

Loops 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




Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked

Join Our Email List
Email:  
For Email Newsletters you can trust
Basic of C
C Sections