Loops

While loop

The while loop in c programming is a very important loop. It has some advantages over the for loop. In for loop, we must know the number of iterations in advance. But there are some occasions where I don’t know the number of iterations. In this case, we can use a while loop.

Definition: while loop is a pre-test loop, it first checks  the condition and as long as the condition is true, action taken.

Syntax of while loop is:

while(condition){
 //statement(s);
}

Let’s describe the syntax:

Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any nonzero value. The loop iterates while the condition is true. And when the condition becomes false, the program control passes to the line immediately following the loop.

Working of while loop

  • The while loop evaluates the ‘condition’ inside the parenthesis ().
  • If the condition is true, statements inside the body of the while loop are executed. Then, ‘condition’ is evaluated again.
  • The process goes on until the ‘condition’ is evaluated to false.

If the condition is false, the loop terminates(ends).

Advantages:

  • Like all loops, “while loops” execute blocks of code over and over again.
  • The advantage to a while loop is that it will go (repeat) as often as necessary to accomplish its goal.

Real life example:

If you want to travel in a flight, you should buy a ticket then only you are eligible to enter into the Flight. (While loop —- First satisfy the condition then Proceed).

For example1: Write a program in C  to print the first five natural numbers using a ‘while’ loop.

#include <stdio.h>
int main()
{
    int i = 1;
    while (i <= 5)
    {
        printf(" %d  ", i);
        i++;
    }
    return 0;
}

Output:

 1   2   3   4   5 

For example2: Write a program in C  to print 10 to 0 numbers using a ‘while’ loop.

#include <stdio.h>
int main()
{
    int n;
    printf("enter the value of n  ");
    scanf("%d", &n);
    int i = n;
    while (i >= 0)
    {
        printf(" %d  ", i);
        i--;
    }
    return 0;
}

Output:

enter the value of n  10

 10   9   8   7   6   5   4   3   2   1   0

  

Do while loop

Do-while loop is a post-test loop.It is similar to while loop except it executes its body at least once whether the condition is true or false.The do-while loop terminates when the text expression is evaluated to be zero.

Syntax of do-while loop is:

do)
    {
        // statement(s);
    }
while (condition);

Let’s describe the terms of syntax:

Test Expression: In this expression we have to test the condition. If the condition evaluates to true then we will execute the body of the loop and go to update the expression. Otherwise, we will exit from the while loop.

Example:

i<=10

Update Expression: After executing the loop body, this expression increments/decrements the loop variable by some value. 

Example:– i++;

Working of do-while loop

  1. Control falls into the do-while loop.
  2. The statements inside the body of the loop get executed.
  3. Updation takes place.
  4. The flow jumps to Condition
  5. Condition is tested. 
    1. If Condition yields true, go to Step 6.
    2. If Condition yields false, the flow goes outside the loop
  6. Flow goes back to Step 2.

Advantages:

The advantage of a do… while loop is that it executes the block of code at least once , and then repeatedly executes the block depending on the condition.

Real life example:

Do-while loops are sometimes useful if you want the code to output some sort of menu to a screen so that the menu is guaranteed to show once.

For example1: Write a program in C  to print the first 10 natural numbers in reverse order using a ‘ do-while’ loop.

#include <stdio.h>
int main()
{
    int i = 1;
    do
    {
        printf("%d \n", i);
        i++;
    } while (i <= 10);
    return 0;
}

Output:

1
2
3
4
5
6
7
8
9
10

For example2: Write a program in C  to print the value of T (20 to 30)  using a ‘do-while’ loop.

#include <stdio.h>
int main()
{
    int T = 20;
    do
    {
        printf("The value of T is = %d \n", T);
        T++;
    } while (T <= 30);
    return 0;
}

Output:

The value of T is = 20 
The value of T is = 21
The value of T is = 22
The value of T is = 23
The value of T is = 24
The value of T is = 25
The value of T is = 26
The value of T is = 27
The value of T is = 28
The value of T is = 29
The value of T is = 30

    Leave a Reply

    Your email address will not be published.

    Need Help?