Loops in C programming with example  

Loops are very important concept in any programming language including C. Loops are used in programming language to execute a statement or group of statements multiple number of times until a given statement becomes false.

0 441 Technology | C-Programming
Tue Dec 25 , 2018

There are three types of loops in C programming language.

  • for loop
  • while loop
  • do while loop

for loop

for loop is most commonly used in C programming language when number of iterations are known. for loop is used to execute a set of statements repeatedly until a particular condition becomes false. In for loop we have two semicolons, one after initialization and second after the condition, and everything is defined in a single line. In this loop we can have more than one initialization or increment/decrement, separated by using comma operator but only have one condition.

Syntax of for loop

for ( counter variable initialization; test condition; increment/decrement)
{
       // statements to be executed repeatedly
}

The for loop is executed in this way:

  • The initialization statement is executed firstly, and only once.
  • Then, the test condition is evaluated. If the condition is false {0}, for loop gets terminated. But if the condition is true {1}, executes the for-loop body and evaluate the increment/decrement condition.
  • This process repeats until the test condition becomes false.

Example Program to print table using for loop


#include<stdio.h>

void main( )
{
    int num;

    printf("Enter a number: ");
    scanf("%d",&num);

    printf("Table of %d is:\n",num);

    for(int i = 1; i <= 10; i++)
    {
        printf("%d*%d=%d\n", num*i);
    }
}

Output

Enter a number: 5
Table of 5 is:
5 * 1 = 5 

5 * 2 = 10 

5 * 3 = 15 

5 * 4 = 20 

5 * 5 = 25 

5 * 6 = 30 

5 * 7 = 35 

5 * 8 = 40 

5 * 9 = 45 

5 * 10 = 50


while loop

while loop executes the block of statements multiple number of times. In while loop, the initialization done before the loop starts and increment or decrement condition inside the loop body.

Syntax of while loop

counter variable initialization;
while (testCondition) 
{
    //statements 
     counter variable increment/decrement; 
}

where, testCondition is either true or false before each loop.

The while loop is executed in this way:

  • while loop firstly evaluates the test condition.
  • If the test condition is true {1}, statements inside the body of while loop are executed, and the test condition is evaluated again. This process goes on until the test condition becomes false.
  • When the test condition is false {0}, while loop gets terminated.

Example Program to print table using while loop


#include <stdio.h>
int main()
{
    int num, i;

    printf("Enter a number: ");
    scanf("%d",&num);
    i=1;

    printf("Table of %d is:\n",num);

    while(i <= 10)
    {
        printf("%d * %d = %d \n\n", num, i, num*i);
        i++;
    }
    
    return 0;
}

Output

Enter a number: 2
Table of 2 is:
2 * 1 = 2 

2 * 2 = 4 

2 * 3 = 6 

2 * 4 = 8 

2 * 5 = 10 

2 * 6 = 12 

2 * 7 = 14 

2 * 8 = 16 

2 * 9 = 18 

2 * 10 = 20 


do while loop

do while loop is similar to while loop with only one main important difference. The body of the do while loop is executed at least once, even though the test condition inside while is false.

Syntax of do while loop

counter variable initialization;
do
{
  //statements 
}
while(testcondition);

The do while loop is executed in this way:

  • do while loop body is executed at least once,even though the test condition inside while is false.
  • Then, the test condition is evaluated.If the test condition is true {1}, the loop body is executed again. This process goes on until the test condition is evaluated to 0 (false).
  • When the test condition is false {0}, do while loop gets terminated.

Example Program to print table using do while loop


#include<stdio.h>
 
void main()
{
    int num, i;
     
    printf("Enter a number: ");
    scanf("%d",&num);
    i=1;

    printf("Table of %d is:\n",num);
    do
    {
       printf("%d * %d = %d \n\n", num, i, num*i);
       i++;
    } 
    while(i <= 10);
}

Output

Enter a number: 3
Table of 3 is:
3 * 1 = 3 

3 * 2 = 6 

3 * 3 = 9 

3 * 4 = 12 

3 * 5 = 15 

3 * 6 = 18 

3 * 7 = 21 

3 * 8 = 24 

3 * 9 = 27 

3 * 10 = 30 
 



Subscribe & Get Regular Updates On Your E-mail