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:
Flowchart of do while loop
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:
- First, the loop will execute the statement inside it. That is, it will print to the screen the line "Infinite do while loop".
- Second, It checks the condition of the loop. Here the condition is true.
- Because the condition is true, the do while loop repeats without stopping.
Output:
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:
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:
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:
In the next tutorial, we will learn about how to use if...else
in C / C++, please keep an eye on it. Thank youuuu !!!