DO ... WHILE C++ looping

In this C++ tutorial, we will learn how to use do ... while looping in C++, through ready-made examples with commented code.

DO ... WHILE Looping

To recap: loop is a certain piece of code that can be repeated as many times as we wish. We have already studied WHILE looping in C++, now let's know DO WHILE.

Remembering that in the WHILE loop, the conditional test occurs before the loop executes.

This is the main difference for the DO WHILE loop, in this, the conditional test will only happen after each iteration (loop internal code repetition).

The loop structure of WHILE is:
do
{
   // code
   // code
} while (condition) ; 

That is, the code is executed first (DO this).
Then the conditional test occurs within the parentheses of while.

If true, the code is repeated again and again as long as the condition is true.

See the flow chart:
How to use DO WHILE loop in C++ tutorial

How to use DO WHILE in C++

The first thing to keep in mind when deciding to use DO WHILE is that it will execute the code at least once! That is, always performs an iteration of the loop!

A very common use is in menus. A menu is always displayed at least once (either in a game or in your banking system).
Let's create a menu of a bank.

See our code:
#include <iostream>
using namespace std;

int main()
{
    int op;

    do
    {
        cout << "Choose an option:\n";
        cout << "0. Exit\n";
        cout << "1. Balance\n";
        cout << "2. Extract\n";
        cout << "3. Withdraw\n";
        cout << "4. Transfer\n";
        cin >> op;

    } while(op);

    return 0;
}
Each time you type an option, it can go to a different section of the system (we'll learn how to do that using functions), and only comes out if you type 0 (ie as long as the op value is different from 0, the loop occurs, because any value other than 0 is true, only 0 is False).

Note that we do not even need to initialize the variable (if we were to use the while loop, we would need to initialize, to ensure that the loop occurred at least once).

DO WHILE example usage

We use the while looping too when we want to do something at least once, and we want to give it the option of repeating itself again.

For example, the code below calculates the average of two grades the user provides.
At the end, he asks if you want to calculate another average:
#include <iostream>
using namespace std;

int main()
{
    float grade1, grade2;
    int op;

    do
    {
        cout << "First grade: ";
        cin >> grade1;

        cout << "Second grade: ";
        cin >> grade2;

        cout <<"Average: " << (grade1+grade2)/2 << endl;
        cout <<"Calculate again ?\n";
        cout <<"1. Yes\n";
        cout <<"2. No\n";
        cin >> op;

    } while(op!=2);

    return 0;
}
That is, the person using this program can calculate as many averages as he or she wants, infinitely, thanks to the do while looping.

So whenever you need to run code at least once (but you don't know how many times, it depends on the user), use DO WHILE looping.

For example:
do
{
   cout << "Blablabla (or type 0 to exit): ";
   //code
   //códe
} while(op);
Cool, isn't?

No comments:

Post a Comment