C - Loops

While writing a program, there may be a situation when you need to repeat a block of code again and again up to certain number of times or up to infinity. In such situation, you would need to write loop statements. Loop is also known as iteration.

Advantages of Loops

  1. Reduce redundancy of code.
  2. Makes program less complex.
  3. It helps to traverse the elements of array.

In C-Language, there are 3 types of loops available:

  1. for loop
  2. while loop
  3. do...while loop

For Loop

For loop iterates the code until the condition is false. For loop uses initialization, and increment/decrement at the time of checking the condition.

  • Initialization is the initial value from where the loop starts execution.
  • Condition will execute a loop until condition is false.
  • Increment/decrement will change the variable value for the next iteration.

Example 1: WAP to print counting 1 to 10 using For Loop. 1

#include<stdio.h>
int main()
{
    int i;
    //for(initialization; condition; inc/dec)
    for(i=1; i<=10; i++)
    {
        printf("For Loop %d ",i);
    }
    return 0;
}
For Loop 1 For Loop 2 For Loop 3 For Loop 4 For Loop 5 For Loop 6 For Loop 7 For Loop 8 For Loop 9 For Loop 10

For loop is useful when the number of iteration is known to the user.

While Loop

In While loop, initialization statement appears before the while loop begins, condition appears to test the loop, increment/decrement statement will appear inside the while loop to change the variable value for the next iteration. It is better if number of iterations is not known by the user.

Example 2: WAP to print counting 1 to 10 using While Loop. 1

#include<stdio.h>
int main()
{
    int i=1; //initialization
    while(i<=10) //condition 
    {
        printf("While Loop %d ",i);
        i++; //increment
    }
    return 0;
}
While Loop 1 While Loop 2 While Loop 3 While Loop 4 While Loop 5 While Loop 6 While Loop 7 While Loop 8 While Loop 9 While Loop 10

While loop is use where number of iteration is not known by the user.

Do While Loop

Do while loop is similar to while loop, only the different is that the condition appears in the end. So, the code is executed at least once, even if the condition is false in the beginning.

Example 3: WAP to print counting 1 to 10 using Do While Loop. 1

#include<stdio.h>
int main()
{
    int i=1; //initialization
    do {
        printf("Do while loop %d ",i);
        i++; //increment
    }while(i<=10); //condition
    return 0;
}
Do while loop 1 Do while loop 2 Do while loop 3 Do while loop 4 Do while loop 5 Do while loop 6 Do while loop 7 Do while loop 8 Do while loop 9 Do while loop 10

Do while loop is used where you want to execute the code at least once.

Difference between while & do-while loop

S.No. While loop Do while loop
In while loop, condition appears at the beginning of the loop. In do while loop, condition appears at the end of the loop.
While loop statement will not execute if the condition is false at the first iteration. Do while loop statement will execute at least once even if the condition is false at the first iteration.
Semicolon (;) is not required to terminate the while loop. Semicolon (;) is required to terminate the do while loop.

Example 4: WAP to explain the use of While Loop. 1

#include<stdio.h>
int main()
{
    int n=1; //initialization
    while(n!=0)
    {
        printf("\nEnter Number (0-exit)");
        scanf("%d",&n);
    }
    return 0;
}
Above program keep on asking the 'Enter Number' till user press 0 for exit.

Infinitive Loop

If you pass 1 as an expression or any value other than 0, then the condition is true, 0 means false.

Example 5: Infinitive while loop.

int i=1;
while(1)
{
    printf("%d",i++);
}
Above program will run infinite number of times.

Example 6: Infinitive do while loop

int i=1;
do
{
    printf("%d",i++);
}while(1);
Above program will run infinite number of times.

Example 7: Infinitive for loop

int i=1;
for(;;)
{
    printf("%d",i++);
}
If you specify 2 semicolons with no initialize, no condition check and no increment or decrement in the for loop, it is known as infinitive for loop.