LATEST

Which is better while or do while loop?

To answer the question "Which is better while or do while loop?", you need to learn about while loop and do while loop in C / C++.

In previous tutorials, we have explained these two types of loops very thoroughly. And now, we summarize as follows.

While and do while loops are used to repeat a block of code inside the loop body. If the condition is true, the loop continues. If the condition is false, the loop ends.

So what's the difference between them? and is it better to use while loop or do while loop? Let's start to find out!!!

Which is better while or do while loop?

For each different case, we will choose to use while loop or do while loop.

Suppose, we want to execute the block of code in the loop at least once. At that time, using do while loop is best.

Or in another case, we want to execute a block of code without knowing the number of iterations in advance. This block of code may not be executed. Then we can use while loop.

See the examples below for better understanding.

Example 1: Display a menu for the user to choose from

For this program you should use a do while loop. Because we need to show the menu at least once for the user to choose.

If the user chooses correctly, we perform the corresponding actions. If the user chooses wrong, we display the menu again for the user to choose.

Program with while loop.

C++ programming:

#include <iostream>
using namespace std;
int main() {
  int selection;
  while (selection < 1 || selection > 4){
    cout << "\n------------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;
  }
  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;
    while (selection < 1 || selection > 4){
      printf("\n------------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);
    }
    printf("You have selected option: #%d\n",selection);
    
  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output: The results returned are not what we expected. Because the while loop does not execute the previous block of code, the inner statements are not executed.

faq 02 PNG

Let's see if using a do while loop will be like.

Program with do while loop.

C++ programming:

#include <iostream>
using namespace std;
int main() {
  int selection;
    do
    {
      cout << "\n------------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;
      if(selection < 1 || selection > 4)
        cout<<"\nYou can only choose from 1 to 4, please choose again!\n";
    } 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("\n------------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);
      if(selection < 1 || selection > 4)
        printf("\nYou can only choose from 1 to 4, please choose again!\n");
    } 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:

faq 01 PNG

Example 2: Displaying message on screen

Program with while loop:

C++ programming:


#include <iostream>
using namespace std;
int main() {
  while(false){
    cout<<"Welcome to learnnc.com!";
  }
  cout<<"\n\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
#include <stdbool.h>
int main(void) {
  while(false){
    printf("Welcome to learnnc.com!");
  }
    
  printf("\n\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

If the condition is false, when using the while loop, no results will be returned.

Output:

faq 04 PNG

Program with do while loop:

C++ programming:

#include <iostream>
using namespace std;
int main() {
  do{
    cout<<"Welcome to learnnc.com!";
  }
  while(false);
  cout<<"\n\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
#include <stdbool.h>
int main(void) {
  do{
    printf("Welcome to learnnc.com!");
  }
  while(false);
    
  printf("\n\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

In this program, even though the loop condition is false. However, the message line is still printed to the screen.

Output:

faq 03 PNG

Conclusion

Depending on the requirements of the program, we choose to use while loop or do while loop accordingly. If we want the code blocks inside the loop to be executed at least once, we use the do while loop. Otherwise, those blocks of code may not be executed, use a while loop.

So the answer is "Depending on the requirements of the program, it is better to choose while or do while loops".

Cùng chuyên mục:

Why do we use arrays instead of other data structures?

Why do we use arrays instead of other data structures?

What happens if no break statement in switch...case?

What happens if no break statement in switch...case?

How to stop while loop in C / C++?

How to stop while loop in C / C++?

Why for loop in C / C++ not stopping?

Why for loop in C / C++ not stopping?

Top