Why use do-while(true)?

182 Views Asked by At
do {
  ...
} while (true);

What is meant by true in this example?

Example:

do {
    cout << "  1-basic arithmetic operations  " << endl;
    cout << "  2-summation  " << endl;
    cout << "  3-Exit " <<endl;
    cout << "Enter your choice: " << endl;
    cin >> main_choice;
    switch(main_choice){
        case 1:
            operation();
            break;
        case 2:
            summation();
            break;
        case 3:
            cout << "Exiting program. Goodbye!" << endl; 
            return 0;
        default:
            cout << "Invalid choice. Please try again." << endl;
    }
} while (true);
3

There are 3 best solutions below

1
MaTh On

When using a do-while construction, you employ the following syntax:

do{
<body>
} while(<condition>)

Here, <condition> represents a boolean expression, meaning it can evaluate to either true or false. If the condition is true, the instructions within <body> are executed, and the loop continues. If the condition is false, the loop terminates, and the program proceeds with instructions outside the loop's body.

With the do-while construction, you always execute the <body> once, before evaluating the condition.

If <condition> is true, you are writing an infinite loop.

0
Adrian McCarthy On

A while loop repeatedly executes the body until the condition is false. The difference between while (condition) {} and do { } while(condition) is when the condition is checked: before or after each iteration. Usually, it's best to test the condition before you begin the first iteration, so you'd use a while loop. But sometimes the loop body must be executed at least one, in which case a do-while loop is appropriate.

In the example you provided, the most convenient place to check when it's time to exit the loop is in the midst of an iteration (specifically, in case 3 where the return statement will exit the loop and the function.)

With these types of loops, the choice is largely up to the programmer's style. Any of these would have worked:

do {
    // loop body here
} while (true);
while (true) {
    // loop body here
}
for (;;) {
    // loop body here
}

The for-loop case works because the initialization, condition, and increment expressions are all optional. If the condition is omitted here, it's the same as if you had said true.

0
H.S. On

In the given example, the author of code has implemented a loop, given true as the value of loop expression which means the loop will keep on iterating and when user will enter 3, the function (in which this loop is implemented) will return 0 and exit.

From do-while loop [emphasis added]:

Executes a statement repeatedly, until the value of expression becomes false. The test takes place after each iteration.

Syntax
attr (optional) do statement while ( expression ) ;

expression is defined as:

expression - any expression which is contextually convertible to bool. This expression is evaluated after each iteration, and if it yields false, the loop is exited.


Additional:

From C++20#6.8.1 [Fundamental types]:

10 Type bool is a distinct type that has the same object representation, value representation, and alignment requirements as an implementation-defined unsigned integer type. The values of type bool are true and false. [Note: There are no signed, unsigned, short, or long bool types or values. — end note]