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 .
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