Bool question
Is it true that when the Boolean expression in the test-condition of a while loop becomes false, the loop exits?? I am new at this. Thanks!
Exactly, that's the whole point.
Yes, but be clear that it doesn't exit until it endeavors to "loop". Observe:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include <iostream>
using namespace std;
int main()
{
int counter = 0;
while (counter < 5)
{
cout << "top of loop: counter == " << counter << "; ";
counter++;
cout << "bottom of loop: counter == " << counter << ";\n";
}
return 0;
}
| |
Another way (a bad way, but still correct) to write the loop would be:
1 2 3 4 5 6 7
|
loop: if (counter < 5)
{
cout << "top of loop: counter == " << counter << "; ";
counter++;
cout << "bottom of loop: counter == " << counter << ";\n";
goto loop;
}
| |
Hope this helps.
Topic archived. No new replies allowed.