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


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

Learn C Loops: While and Do-While

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

There are primarily three varieties of loops in C. 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 process or to run a particular block of code a number of instances, the idea of LOOP comes in image.

A loop is an instruction given to the pc that it has to run a particular a 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.

2.1. Flowchart

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

C While Loop Flowchart

2.2. Syntax

While (situation)

    //Statement block
    //Increment/Decrement operation

2.3. While Loop Example

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

#embrace<stdio.h>
#embrace<conio.h>

void foremost()

    int n = 1;

    while(n <= 10)
    
        printf("%dnn", n);
        n++;
    

    getch();

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

Its output ought to look one thing like this-

C While Loop Program 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 exit it.

3.1. Flowchart

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

C Do While Loop Flowchart

3.2. Syntax

do

    //assertion block

While(situation);

3.3. C Do-While Loop Example

Here is a easy instance to search out the sum of 1 to 10 utilizing the do-while loop

#embrace<stdio.h>
#embrace<conio.h>

void foremost()

    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 Program 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 an 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 with out truly utilizing the * operator however have to make use of each the while and do-while loops.

4.1. Program utilizing the while loop

#embrace<stdio.h>
#embrace<conio.h>

void foremost()

    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 utilizing the do-while loop

#embrace<stdio.h>
#embrace<conio.h>

void foremost()

    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 the next applications is identical, examine from beneath screenshot.

C While Loop Exercises Result

Python  Recommended Post:

Source hyperlink

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories

You have successfully subscribed to myprogrammingschool

There was an error while trying to send your request. Please try again.

My Programming School will use the information you provide on this form to be in touch with you and to provide updates and marketing.