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);
When using a do-while construction, you employ the following syntax:
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.