Skip to main content

do-while Loop in C

do-while Loop Overview

A do-while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition.

The do-while loop evaluates the condition after the code within the loop has been executed, which means that the code within the loop will be executed at least once.

The syntax of a do-while loop in C is as follows:

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

The code within the curly braces will be executed at least once, regardless of whether the condition is true or false. After the code is executed, the condition is evaluated. If the condition is true, the code will be executed again. This process will continue until the condition becomes false.

A basic example of a do-while loop in C is:

count = 1;
do {
printf("%d\n", count);
count++;
} while (count <= 10);

Explanation

This will print the numbers 1 through 10, because the loop continues while the value of count is less than or equal to 10.

Where should we use "do-while Loop in C"

A do-while loop in C should be used when you want to execute a block of code at least once and then repeatedly until a certain condition is met.

Here are some scenarios where a do-while loop in C can be useful:

  • When you want to repeatedly prompt the user for input until they enter a valid value
  • When you want to keep reading from a file until the end of file is reached
  • When you want to keep running a certain process until a certain condition is met
  • When you want to repeatedly execute a block of code while a user-defined condition is true
  • When you want to repeatedly execute a block of code while a certain key is pressed.
  • When you want to repeatedly execute a block of code until a specific time or date is reached.
  • When you want to repeatedly execute a block of code until a external interrupt occurs.

As you can see, do-while loop is very useful in situations where you have an uncertain number of iterations and at least one iteration is necessary, to ensure that the execution happens at least once before checking the condition.

Examples of using "do-while loop" in C

Example 1

Below is an example of using a do-while loop in C to repeatedly prompt the user for input until they enter a valid value:

#include <stdio.h>

int main() {
int user_input;
do {
printf("Enter a number between 1 and 10: ");
scanf("%d", &user_input);
} while (user_input < 1 || user_input > 10);

printf("You entered %d\n", user_input);
return 0;
}

Explanation:

  • The program uses a do-while loop to repeatedly prompt the user for input

  • The loop continues to execute as long as the user's input is not a number between 1 and 10

  • The condition to check whether user's input is valid or not is user_input < 1 || user_input > 10

  • Once the user enters a valid number, the loop terminates

  • The program then prints "You entered [valid number]"

The output of this program will be:

Enter a number between 1 and 10: 12
Enter a number between 1 and 10: -5
Enter a number between 1 and 10: 7
You entered 7

Output:

  • The program prompts the user to enter a number between 1 and 10

  • The user enters 12 and -5 which are not valid

  • The program continues prompting the user until the user enters 7 which is valid input

  • The program terminates and prints "You entered 7"

Example 2:

Below is another example of using a do-while loop in C to repeatedly prompt the user for a password until they enter the correct password:

#include <stdio.h>
#include <string.h>

int main() {
char password[20], input[20];
strcpy(password, "password123");

do {
printf("Enter password: ");
scanf("%s", input);
} while (strcmp(input, password) != 0);

printf("Access granted\n");
return 0;
}

Explanation:

  • The program uses a do-while loop to repeatedly prompt the user for password

  • The loop continues to execute as long as the user's input is not equal to the correct password which is "password123"

  • The condition to check whether user's input is correct or not is strcmp(input, password) != 0

  • Once the user enters the correct password, the loop terminates

  • The program then prints "Access granted"

Output:

Enter password: letmein
Enter password: mypassword
Enter password: password123
Access granted
  • The program prompts the user to enter password

  • The user enters "letmein"

  • The program prompts the user again as the input is not correct

  • The user enters "mypassword"

  • The program prompts the user again as the input is not correct

  • The user enters "password123"

  • The program terminates and prints "Access granted"

Example 3

Below is example of a program using a do-while loop in C to repeatedly prompt the user for a sentence until they enter "exit":

#include <stdio.h>
#include <string.h>

int main() {
char input[20];
do {
printf("Enter a sentence or type 'exit' to quit: ");
scanf("%s", input);
} while (strcmp(input, "exit") != 0);
printf("Exiting program\n");
return 0;
}

Explanation:

  • The program uses a do-while loop to repeatedly prompt the user for input

  • The user is prompted to enter a sentence or type 'exit' to quit

  • The loop continues to execute as long as the user's input is not equal to "exit"

  • The condition to check whether user's input is correct or not is strcmp(input, "exit") != 0

  • Once the user enters "exit", the loop terminates

  • The program then prints "Exiting program"

Shell output:

Enter a sentence or type 'exit' to quit: hello
Enter a sentence or type 'exit' to quit: how are you
Enter a sentence or type 'exit' to quit: exit
Exiting program

Output explanation:

  • The program prompts the user to enter a sentence or type 'exit' to quit
  • The user enters "hello"
  • The program prompts the user again
  • The user enters "how are you"
  • The program prompts the user again
  • The user enters "exit"
  • The program terminates and prints "Exiting program"
caution

A caution when using do-while loop is to make sure that the condition that stops the loop is defined and updated correctly inside the loop to avoid infinite loop. Also, ensure that the variables used in the condition are updated within the loop, otherwise the loop may not terminate and could cause the program to crash. Additionally, be mindful of the resources and memory used by the loop to avoid overuse.