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.

Break statement

We can terminate the execution of loop in between and came out of the loop by using break statement.
Here we have used one if condition checking and once the value of i became 3 then break; statement gets executed and the loop is terminated.
#include <stdio.h>
int main(void){
int i=1;

for(i=1;i <= 5;i++){
printf("\n I value before if condition  : %d  ", i);

if(i==3){
break;
}

printf("\n I value after if condition   : %d  ", i);
}
return 0;
}
Output is here
 I value before if condition  : 1
 I value after if condition   : 1
 I value before if condition  : 2
 I value after if condition   : 2
 I value before if condition  : 3
Note that i value 4 and i value 5 is not printed as break; statement stopped the execution of the loop.

continue statement

Using continue; statement we can skip the execution of code blok in rest of the loop, however the loop will continue the execute from starting.

Difference between break and continue

After encountering break; statement the execution will come out of the loop. In case of continue; statement the execution for rest of the loop is skipped and the execution will start from starting of the loop ( checking of condition ).

We will use the same code as above and in place of break; we used continue; here
for(i=1;i <= 5;i++){
printf("\n I value before if condition  : %d  ", i);

if(i==3){
continue;
}

printf("\n I value after if condition   : %d  ", i);
}
return 0;
}
Output
 I value before if condition  : 1
 I value after if condition   : 1
 I value before if condition  : 2
 I value after if condition   : 2
 I value before if condition  : 3
 I value before if condition  : 4
 I value after if condition   : 4
 I value before if condition  : 5
 I value after if condition   : 5
Note that one step is skiped. The step to show I value after if condition : 3 is not displayed ( skipped ) but execution continued from staring of the loop again and displayed next value.

incrementing or decrementing steps

We will increment the variable by more steps.
i++; // increment i value by 1 
i--; // decrement i value by 1
i += 5; // increment i value by 5 
i -= 5; // Decrement i value by 5 

Example with increment of value by 5

#include <stdio.h>
int main(void){
int i=1;

for(i=0;i <= 25;i+=5){
printf("\n I value : %d  ", i);
}
return 0;
}
Output
 I value : 0
 I value : 5
 I value : 10
 I value : 15
 I value : 20
 I value : 25

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;
}
Test on Operators

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
  1. Exercise on For loop
  2. Create a number series like this

    1,2,3,4,5,6,7,8,9
    1,3,5,7,9
    1,4,7
    1,5,9
    1,6
    1,7
    1,8
    1,9
    1

    Here the difference between the numbers in a row is increasing.

    Hint : You have to use nested loops and change the step value of inside loop.

    (For solution check the link bellow about String patterns )
String patterns using for loop


plus2net.com



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-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer