Abnormal Loop Termination

Abnormal Loop Termination

By default, a while statement executes until its condition becomes false. The executing program checks this condition only at the “top” of the loop. This means that even if the Boolean expression that makes up the condition becomes false before the program completes executing all the statements within the body of the loop, all the remaining statements in the loop’s body must complete before the loop can once again check its condition. In other words, the while statement in and of itself cannot exit its loop somewhere in the middle of its body.

Abnormal Loop Termination AndroWep-Tutorials
Abnormal Loop Termination AndroWep-Tutorials

The break statement

C++ provides the break statement to implement middle-exiting control logic. The break statement causes the immediate exit from the body of the loop .

#include <iostream>
int main() {
int input, sum = 0;
std::cout << "Enter numbers to sum, negative number ends list:";
while (true) {
std::cin >> input;
if (input < 0)
break; // Exit loop immediately
sum += input;
}
std::cout << "Sum = " << sum << '\n';
}

The goto Statement

The break statement exits the single loop in which it is located. A break statement is insufficient to jump completely out of the middle of a nested loop. The goto statement allows the program’s execution flow to jump to a specified location within the function. Listing 6.16 (exitnested.cpp) uses a goto statement to jump out from the middle of a nested loop.

#include <iostream>
int main() {
// Compute some products
int op1 = 2;
while (op1 < 100) {
int op2 = 2;
while (op2 < 100) {
if (op1 * op2 == 3731)
goto end;
std::cout << "Product is " << (op1 * op2) << '\n';
op2++;
}
op1++;
}
end:
std::cout << "The end" << '\n';
}

When op1 * op2 is 3731, program flow will jump to the specified label within the program. In this example, the label is named end, but this name is arbitrary. Like variable names, label names should be chosen to indicate their intended purpose. The label here named end comes after and outside the nested while loops.

#include <iostream>
int main() {
int count = 1; // Initialize counter
top:
if (count > 5)
goto end;
std::cout << count << '\n'; // Display counter, then
count++; // Increment counter
goto top;
end:
; // Target is an empty statement
}

The continue Statement

When a program’s execution encounters a break statement inside a loop, it skips the rest of the body of the loop and exits the loop. The continue statement is similar to the break statement, except the continue statement does not necessarily exit the loop. The continue statement skips the rest of the body of the loop and immediately checks the loop’s condition. If the loop’s condition remains true, the loop’s execution resumes at the top of the loop. Listing 6.18 (continueexample.cpp) shows the continue statement in action .

#include <iostream>
int main() {
int input, sum = 0;
bool done = false;
while (!done) {
std::cout << "Enter positive integer (999 quits): ";
std::cin >> input;
if (input < 0) {
std::cout << "Negative value " << input << " ignored\n";
continue; // Skip rest of body for this iteration
}
if (input != 999) {
std::cout << "Tallying " << input << '\n';
sum += input;
}
else
done = (input == 999); // 999 entry exits loop
}
std::cout << "sum = " << sum << '\n';
}

Programmers do not use the continue statement as frequently as the break statement since it is easy to transform code using continue into an equivalent form that does not.