C While and Do-While Loops

In this C programming class, we’ll cover the C while and do-while loop statements. The loops are the main constructs to implement iterative programming in C.

Learn C Loops: While and Do-While

Now that you have started this journey of learning C programming, there will be instances where you may need to run a particular statement block more than once. It may be for input, processing or output.

There are mainly three types of loops in C. In this tutorial, we will see the first two loops in detail.

1. What are Loops In C Programming?

To perform a particular task or to run a specific block of code several times, the concept of LOOP comes in picture.

A loop is an instruction given to the computer that it has to run a specific part of the code for a given number of times.

2. While Loop

The while loop begins by first checking the terminal condition and then decides whether to enter the loop or not.

If the underlying condition is true, then it goes ahead and executes the block of code in the loop. After the first iteration, it again checks with the changed (increased/decreased) values of the variables (the condition operands) and decides the further course of execution.

2.1. Flowchart

The below flowchart will help you understand the functioning of the while loop.

C While Loop Flowchart

2.2. Syntax

While (condition)
{
    //Statement block
    //Increment/Decrement operation
}

2.3. While Loop Example

Here is a simple example of how a while loop works? This program prints numbers from 1 to 10 without actually using the ten printf statements but a while loop.

#include<stdio.h>
#include<conio.h>

void main()
{
    int n = 1;

    while(n <= 10)
    {
        printf("%d\n\n", n);
        n++;
    }

    getch();
}

Here, the “\n” in the printf call is used to move to the next line.

Its output should look something like this-

C While Loop Program Output

3.Do-While Loop

In this loop, the statement block gets executed first, and then the condition is checked.

If the underlying condition is true, then the control returns to the loop otherwise exit it.

3.1. Flowchart

The below flowchart will help you understand the functioning of the do-while loop.

C Do While Loop Flowchart

3.2. Syntax

do
{
    //statement block
}
While(condition);

3.3. C Do-While Loop Example

Here is a simple example to find the sum of 1 to 10 using the do-while loop

#include<stdio.h>
#include<conio.h>

void main()
{
    int i = 1,a = 0;

    do
    {
        a = a + i;
        i++;
    }
    while(i <= 10);

    printf("Sum of 1 to 10 is %d",a);
    getch();
}

Its output should be something like this-

C Do While Loop Program Output

Generally, the do-while loop is not preferred in applications as it first executes the block of statements and then checks the condition. It risks the security which is like allowing an unauthorized person into a facility and then asking for his ID.

Above was the explanation of the while and do-while loops. We will see the for loop in detail in the next chapter.

There is an exercise you can perform on the next page which will help you understand these two loops nicely.

4. C Loops Exercises for Practice

Write a program in C to multiply two numbers without actually using the * operator but have to use both the while and do-while loops.

4.1. Program using the while loop

#include<stdio.h>
#include<conio.h>

void main()
{
    int a, b, i = 0, c = 0;

    printf("Enter two numbers to multiply : ");
    scanf("%d %d",&a, &b);

    while(i < b)
    {
        c = c + a ;
        i++;
    }

    printf("The product of these numbers is %d", c);
    getch();
}

4.2. Program using the do-while loop

#include<stdio.h>
#include<conio.h>

void main()
{
    int a, b, i = 0, c = 0;

    printf("Enter two numbers to multiply : ");
    scanf("%d %d",&a, &b);

    do
    {
        c = c + a ;
        i++;
    }
    while(i < b);

    printf("The product of these numbers is %d", c);
    getch();
}

Keep in mind that in a do-while loop, the semicolon always comes after while statement but not in while loop.

The output for both the following programs is same, check from below screenshot.

C While Loop Exercises Result