For loop in C-with syntax and example


For Loop In C Programming


Today we will learn about for loop in c proramming with its syntax and few examples. 

There are 3 types of loops in C Programming:-

  • For Loop
  • While Loop and
  • Do while Loop
Loops are used to repeat a block of code until the given condition is specified. For Example to print "Hello world" in the console u can use loops to do that easily rather  then writing printf 100 times.

To learn about while loop and do while loop click here  While and do while loop.

syntax:-

for (initializationstatement; conditionstatement; updatestatement)

{

//Statements written inside the loop


}

Working:-

  • First the intialization statement is executed which will execute only the first time loop is executed.
  • Then the conditional statement is executed and if it evaluates to false, then the loop will be terminated.
  • If the conditional statement executed evaluates to true, then the body of the loop is executed and the the then the statement is updated.
  • And again the conditional statement is evaluated and this process will run until this condition becomes false.


Example 1:-


#include <stdio.h>

int main()
{
    int i;
    
    for(i=0; i<11; i++){
        printf("%d ", i);
    }

    return 0;
}

Output:-

0 1 2 3 4 5 6 7 8 9 10                                                                                                
                    

Explanation:-

  1. First i will be initialized to 0.
  2. And then the condition i < 11 is checked which is correct as i is 1 and 0 < 11 which is true then the body of the loop which is to print i is executed and i is printed.
  3. After executing the body the statement is updated (The expression is i++) and i will become 1. And the condition is checked again which will evaluate to true.
  4. This process continues until the condition becomes false that is. when i becomes 11 then the loop will be terminated.

Example 2:-

Here is another example in which we are making use of for loop to find the sum of 5 integers (i.e 1+2+3+4+5 which is equal to 15). So here first we are declaring the index variable 'i' which will iterate from one to 5 and then we are initializing an integer variable 'sum' to zero and this will store our final result which is the sum of 5 numbers from 1 through 5. In the for loop we initialize the index variable 'i' to zero and the condition to be checked is 'i<6'. If this condition is true then we enter the body of the loop and then increment i by one every time and then checks the condition again. 

In the below example inside the body of the loop we are finding the sum of numbers.
Initially sum=0 and i=1.
  • In the first time through the loop  sum is added with 1, resulting in sum=1.
  • In the second time through the loop  sum is added with 2, and the sum becomes sum=3.
  • In the third time through the loop  sum is added with 3, and the sum becomes sum=6.
  • In the fourth time through the loop  sum is added with 4, and the sum becomes sum=10.
  • In the fifth time through the loop  sum is added with 5, and the sum becomes sum=15.
  • In the sixth time the condition of the loop failed as i=6 and 6 is not less than 6. 
So we come out of the loop and prints the final sum on the output screen.


// Program to print sum of first 5 numbers

#include <stdio.h>

int main()
{
    int i;
    int sum=0;
    
    for(i=1; i<6; i++){
        sum = sum + i;
    }
    
    printf("Sum: %d", sum);

    return 0;
}

Output:-

Sum: 15                                                                                                               
    

Post a Comment

0 Comments