LATEST

How to use do while loop in C / C++

In the previous tutorial, we learned together about for loop and while loop in C / C++. In this tutorial, we will learn about do while loop in C / C++ and its working with the help of some examples.

The do while loop is used when a block of code is executed first, and then the condition of the loop is checked. If the condition is true, continue the loop. If the condition is false, exit the loop.

For example: Suppose we ask the user to enter positive numbers. If the user enters correctly, then continue to enter. If the input is incorrect, exit the loop.

Syntax of do while loop in C / C++

Basically, the do while loop is similar to the for and while loop in C / C++. It is used to execute a block of code over and over again.

However, the do while loop executes the block of code first, and then checks the condition. This means that the block of code in the loop is executed at least once.

Syntax of do while loop

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

Here,

  • statement(s): These are the statements inside the loop. It is executed at least once.
  • condition: This is the condition of the loop, it determines whether the loop continues or stops.

For example: Suppose we want to display "Welcome to learnnc.com" 10 times, then the do while loop would look like this:

int i = 1;
do{
    cout<<"Welcome to learnnc.com\n";
    i++;
}
while(i<=10);

Output:

loop 01 PNG

Flowchart of do while loop

loop 02 PNG

How do while loop in C / C++ work?

The first do while loop executes the code blocks inside it. Then check the condition to decide whether to continue the iteration or not.

  • If the condition is true: The code blocks inside the loop continue to be executed.
  • If the condition is false: The do while loop ends.

*Note: We need to change the condition of the loop so that it can stop. Avoid the case where the do while loop is repeated infinitely. This is very dangerous, because it slows down the program.

Infinite do while loop in C / C++

In C and C++ programming languages, the do while loop is looped indefinitely when its condition is always true. That means the code blocks inside the loop body are executed multiple times with no breakpoint.

Check out an example of an infinite do while loop below.

Example: Infinite do while loop.

#include <iostream>  
using namespace std;  
int main() {  
    do{    
        cout << "Infinite do while loop" << endl;    
    } while(true);     
    return 0;
}

Explain:

  1. First, the loop will execute the statement inside it. That is, it will print to the screen the line "Infinite do while loop".
  2. Second, It checks the condition of the loop. Here the condition is true.
  3. Because the condition is true, the do while loop repeats without stopping.

Output:

loop 03 PNG

Solution: For this program, we can declare variable i = 1. Then display the line "Infinite do while loop" 5 times.

#include <iostream>  
using namespace std;  
int main() {  
  int i = 1;
  do{    
    cout << "Infinite do while loop" << endl;   
    i++;
  } while(i <= 5);   
  
  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

Output:

loop 04 PNG

Do while loop in C / C++ with examples

Here, we have two examples with do while loop in C/C++. These are basic examples when using do while loop in C/C++.

Each of the examples are written in two different programming languages, C and C++.

Example 1: Displays numbers from 1 to n, with n being entered from the user

C++ programming:

#include <iostream>
using namespace std;
int main() {
  int n, i = 0;
  cout<<"Enter number n: ";
  cin>>n;
  cout<<"Numbers from 1 to "<<n<<" : \n";
  do{
    cout<<i<<" ";
    i++;
  }
  while(i<=n);
  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
  
int main(void) {
  int n, i = 0;
  printf("Enter number n: ");
  scanf("%d", &n);
  printf("Numbers from 1 to %d : \n",n);
  do{
    printf("%d ",i);
    i++;
  }
  while(i<=n);
    
  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

loop 05 PNG

Example 2: Create menu using do while loop in C / C++

C++ programming:

#include <iostream>
using namespace std;
int main() {
  int selection;
    do
    {
      cout << "------------Menu------------- \n";
      cout << "1) Withdraw money\n";
      cout << "2) Transfers\n";
      cout << "3) Recharge\n";
      cout << "4) Check account\n";
      cout << "Your choice is: ";
      cin >> selection;
    } while (selection < 1 || selection > 4);
 
    cout << "You have selected option: #" << selection << "\n";
  
  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
  
int main(void) {
  int selection;
    do
    {
      printf("------------Menu------------- \n");
      printf("1) Withdraw money\n");
      printf("2) Transfers\n");
      printf("3) Recharge\n");
      printf("4) Check account\n");
      printf("Your choice is: ");
      scanf("%d", &selection);
    } while (selection < 1 || selection > 4);
 
    printf("You have selected option: #%d\n",selection);
    
  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

loop 06 PNG

In the next tutorial, we will learn about how to use if...else in C / C++, please keep an eye on it. Thank youuuu !!!

Cùng chuyên mục:

How to use strings in C / C++

How to use strings in C / C++

How to use multidimensional Arrays in C / C++

How to use multidimensional Arrays in C / C++

Searching and sorting arrays in C / C++

Searching and sorting arrays in C / C++

How to use arrays in C / C++

How to use arrays in C / C++

How to use goto statement in C / C++

How to use goto statement in C / C++

How to use switch...case statement in C / C++

How to use switch...case statement in C / C++

How to use continue statement in C / C++

How to use continue statement in C / C++

How to use break statement in C / C++

How to use break statement in C / C++

How to use if - else statement in C / C++

How to use if - else statement in C / C++

How to use Binary Search Tree (BST) in C / C++

How to use Binary Search Tree (BST) in C / C++

How to use while loop in C / C++

How to use while loop in C / C++

How to use for loop in C / C++

How to use for loop in C / C++

Top