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

Printing string in reverse order without using strrev()

#include<stdio.h>
#include<string.h>

int main()
{
 char str[30]="plus2net";
 int str_length;
 str_length=strlen(str); // length of the string
 for(int i=str_length; i>=0; i--){
 printf("%c",str[i]);   // printing the char at position  i
 }
return 0;
}

Value of i outside the loop

It will increase by the increment value outside the loop.
#include <stdio.h>
int main(void){
   int i;

   for(i=0;i<=5;i++){
    printf("\n i= %d  ",i);
   }
   printf("\n You came out of loop.");
   printf("\n i= %d  ",i);
   return 0;
}
Output is here
 i= 0
 i= 1
 i= 2
 i= 3
 i= 4
 i= 5
 You came out of loop

 i= 6
String patterns using for loop
Your Rating




Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer