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 )

Practice Questions on For Loop

  1. Display numbers from 1 to n
  2. Display even numbers from 1 to n
  3. Display odd numbers from 1 to n
  4. Write sum of numbers from 1 to n
  5. Write sum of numbers in each step with final Sum of numbers from 1 to n
  6. Write sum of all even numbers from 1 to n
    Hint : Use Continue to skip adding odd numbers.
  7. Write sum of all odd numbers from 1 to n
  8. Write sum of all numbers from 1 to n which are divisible by 10
  9. Find factorial of n (user input ). Factorial is the multiplication of all positive integers upto n.
  10. List the cube value of all numbers upto n ( user input )
  11. Display the factors of one input number n
  12. Display the common factors of two input number n1 and n2
  13. Display Highest and lowest common factors of two input number n1 and n2
  14. Display sum of the digits of any inputs number
  15. Display the digits in reverse of any inputs number ( Example 341 should return 143 )
  16. All prime numbers between 1 to n.
  17. List out the prime factors of any input number n ( If factor of a number is prime number then it is a Prime factor, 13 is prime factor of 26)
  18. Print ASCII table ( Decimal value and Char ) , Output should be like this
    100 -> d
    101 -> e
    102 -> f
  19. Print ASCII table ( Decimal value and Char ) for lower case chars ( use value 97 to 122 )
  20. Print ASCII table ( Decimal value and Char ) for Upper case chars
  21. Create multiplication tables from 1 to n, each row should have multiplication values from 1 to 10 with n

    1 ,2,3,4,5,6,7,8,9,10
    2,4,6,8,10,12,14,16,18,20
    3,6,9,12,15,18,21,24,27,30
    ------
    ----
String patterns using for loop

Subscribe

* indicates required
Subscribe to plus2net

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