C Programming Tutorial

C Programming Tutorial

Do While Loop in C

Hello Everyone, In this article, we will cover the concept of Do While Loop in C Programming. We will cover its syntax, usage, and practical examples to help you understand how and when to implement this loop in your C programs. Whether you’re new to C or have some experience, this guide will help you to understand Do While Loop in C effectively. Let’s get started.

A “do-while” loop in the C programming is a type of loop that allows a block of code to be executed repeatedly while a certain condition is true. Unlike a “while loop“, which checks the condition before executing the code block, a “do-while” loop checks the condition after the block of code has executed. This guarantees that the code block runs at least once, regardless of whether the condition is initially true or false.

Basic Syntax of do-while loop in C

do {
   // block of code to be executed
} while (condition);

Here, the code block is executed first, and then the condition is checked. If the condition is true, the loop executes again, and continues until the condition becomes false.

It is important to note that the do-while loop is different from the more commonly used “while” loop, in which the condition is checked before the loop is executed. In a do-while loop, the block of code is always executed at least once, regardless of whether the condition is initially true or false.

In order to avoid infinite loops, it is important to ensure that the condition within the do-while loop is eventually false. The loop can also be exited prematurely using the “break” statement.

Example of a do-while loop in C

#include <stdio.h>
int main() {
   int i = 1;

   do {
      printf("i is %d\\n", i);
      i++;
   } while (i <= 5);

   return 0;
}

In this example, the code block within the do-while loop will be executed at least once, since the condition (i <= 5) is checked at the end of the loop.

The loop will continue to execute until the condition becomes false, which in this case happens when i becomes greater than 5.

Each time the loop iterates, the value of i is printed to the console, and then incremented by 1 using the i++ statement.

The output of this program would be:

i is 1
i is 2
i is 3
i is 4
i is 5

As you can see, the loop executes five times, once for each value of i from 1 to 5 inclusive.

Another Example of Do while loop in C

#include <stdio.h>
int main() {
   int num, sum = 0;

   do {
      printf("Enter a number (enter 0 to quit): ");
      scanf("%d", &num);
      sum += num;
   } while (num != 0);

   printf("The sum of the numbers is: %d", sum);

   return 0;
}

In this program, the user is asked to enter a series of numbers. The loop continues to execute until the user enters the number 0.

Each time the loop iterates, the user is prompted to enter a number using the printf and scanf statements. The value of num is added to the running total sum using the += operator.

Once the user enters the number 0, the condition within the while statement becomes false, and the loop exits. The final value of sum is then printed to the console using the printf statement.

Output:

Enter a number (enter 0 to quit): 5
Enter a number (enter 0 to quit): 7
Enter a number (enter 0 to quit): 0
The sum of the numbers is: 12

As you can see, the program correctly calculates the sum of the numbers entered by the user, and prints the result to the console.

Share This Article