Monday, April 28, 2025

C Tutorial - 11 - While and Do..While Loop

While loop 

#include <stdio.h>

int main()
{
    int x = 0;

    while (x < 10)
    {
        printf("X in this moment in a loop: %d \n", x);
        x = x + 1;
    }

    return 0;
}

Result:

X in this moment in a loop: 0
X in this moment in a loop: 1
X in this moment in a loop: 2
X in this moment in a loop: 3
X in this moment in a loop: 4
X in this moment in a loop: 5
X in this moment in a loop: 6
X in this moment in a loop: 7
X in this moment in a loop: 8
X in this moment in a loop: 9

Process returned 0 (0x0)   execution time : 0.024 s
Press any key to continue.

This C program uses a while loop to print the value of variable x repeatedly until it reaches the value of 10.

The program starts by initializing x to 0, and then enters a while loop with the condition x < 10. Inside the loop, the program prints the value of x using the printf() function, and then increments x by 1 using the expression x = x + 1. This process is repeated until x is no longer less than 10, at which point the loop is exited and the program returns 0.

Therefore, when you run this program, it will print the values of x from 0 to 9, and then terminate.

 

Why do we need x = x + 1 inside while loop

The statement x = x + 1 inside the while loop is used to increment the value of x by 1 in each iteration of the loop. Without this statement, the value of x would remain the same and the loop would execute infinitely.

In the given example, the initial value of x is set to 0. The while loop checks whether x is less than 10. If x is less than 10, it prints the value of x and increments it by 1 using the statement x = x + 1. This process is repeated until x becomes greater than or equal to 10 and the loop terminates.

 

Do..While loop

#include <stdio.h>

int main()
{
    int x = 0;

    do
    {
        printf("X in this moment in a loop: %d \n", x);
        x = x + 1;
    }
    while (x < 5);

    return 0;
}

Result:

X in this moment in a loop: 0
X in this moment in a loop: 1
X in this moment in a loop: 2
X in this moment in a loop: 3
X in this moment in a loop: 4

Process returned 0 (0x0)   execution time : 0.034 s
Press any key to continue.

 

Why "while" is outside "do" block of code

In the syntax of the do-while loop, the while condition is written after the do block of code. This is because the do block of code is executed first, and then the condition is checked. If the condition is true, the loop will continue to execute the do block of code again, and if the condition is false, the loop will exit.

By contrast, in the while loop, the condition is checked before the first execution of the loop. If the condition is false, the loop is never executed. If the condition is true, the loop is executed, and then the condition is checked again. So, in the while loop, the condition comes before the block of code.

 

When do..while loops are useful

Do-while loops are useful when you want to execute a block of code at least once, regardless of whether the condition is true or false. This is because the condition is checked at the end of the loop rather than the beginning. This can be helpful in situations where you need to perform a task before testing a condition.

Another example where do-while loop can be useful is when you are prompting the user for input. You can use a do-while loop to keep asking the user for input until they provide a valid input.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...