It may help to look at the code from another angle:
This code uses an infinite loop (one which never terminates, the only way out is via the
break;
statement).
1 2 3 4 5 6 7 8 9 10 11 12 13
|
while (true) // repeat forever
{
if (gullible == 5)
break;
if (iteration == 10)
break;
cout << endl << "Enter any number other than 5.\n";
cin >> gullible;
iteration++;
cout << iteration << endl;
}
|
There are two obstacles to overcome before reaching the main body of the code.
If either the test at line 3
OR the test at line 6 is true, then the loop will terminate.
And looking at it the converse way,
both line 3
AND line 6 must be
false, in order to execute the body of the code.
Or to express the same code yet another way, this time using the
not-equal
condition,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
while (true) // repeat forever
{
if (gullible != 5)
{
if (iteration != 10)
{
cout << endl << "Enter any number other than 5.\n";
cin >> gullible;
iteration++;
cout << iteration << endl;
}
else
{
break;
}
}
else
{
break;
}
}
|
This time, in order to reach line 7 both line 3
AND line 5 must be
true. If either condition is false, the corresponding
else
statement is executed and the loop terminates.