C While and Do-While Loops Explained with Examples – MPS

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

While and Do-While loops

Now that you’ve begun this journey of studying C programming, there shall be cases the place you could have to run a selected statement block greater than as soon as. It could also be for entering, processing, or output.

There are primarily three varieties of loops in C like for loop, while and do-while loops. In this tutorial, we are going to see the primary two loops in element.

1. What are Loops In C Programming?

To carry out a selected processor to run a particular block of code in a number of instances, the idea of LOOP comes in an image.
A loop is an instruction given to the pc that it has to run a particular part of the code for a given variety of instances.

2. While Loop

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

If the underlying situation is true, then it goes forward and executes the block of code in the loop. After the primary iteration, it once more checks with the modified (elevated/decreased) values of the variables (the situation operands) and decides the additional course of execution.

While loop Flowchart

The beneath flowchart will provide help to perceive the functioning of the while loop.

C While Loop Flowchart
While loop

Syntax

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

While Loop Example

Here is an easy instance of how a while loop works? This program prints numbers from 1 to 10 without truly utilizing the ten printf statements however a while loop.

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

void main()
{
    int num = 1;

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

Here, the “num” in the printf name is used to maneuver to the following line.

Its output ought to look one thing like this-

While Loop Example with output

3. Do-While Loop

In this loop, the assertion block will get executed first, and then the situation is checked.

If the underlying situation is true, then the management returns to the loop in any other case to exit it.

Do-while loop Flowchart

The beneath flowchart will provide help to perceive the functioning of the do-while loop.

C Do While Loop Flowchart

Syntax

do
{
    //assertion block
}
While(situation);

C Do-While Loop Example

Here is an easy instance to search out the sum of 1 to 10 utilizing 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 must be one thing like this-

C Do While Loop example with Output

Generally, the do-while loop is not most popular in purposes as it first executes the block of statements and then checks the situation. It dangers the safety which is like permitting an unauthorized particular person right into a facility and then asking for his ID.

Above was the clarification of the while and do-while loops. We will see the for-loop in element in the following chapter.

There is a train you may carry out on the following web page which is able to provide help to perceive these two loops properly.

4. C Loops Exercises for Practice

Write a program in C to multiply two numbers without truly utilizing the * operator however have to make use of each of the while and do-while loops.

Example of 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();
}

Example of 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 thoughts that in a do-while loop, the semicolon at all times comes after while assertion however not in while loop.

The output for each of the next applications is identical, examine from the beneath screenshot.

C example of do while loop with Result

Related Posts

 

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment