is it true you can use break to end a loop? if so would putting a nested switch statement in a loop then using a break intended to break the switch also break the loop?
Note that, while you can nest them inside a conditional to break loops, a break inside a switch will break the switch, not the loop in which it is nested.
#include <iostream>
using std::cout;
using std::cin;
int main(void)
{
bool test = true;
while (true)
{
if (test)
{
break; // Breaks out of while,
} // even though it is inside a conditional.
}
cout << "Broke out of loop one.\n\n";
int count = 1;
while (count <= 5)
{
switch (test)
{
casefalse:
break;
casetrue:
break; // Will break the switch, not the while.
default:
break;
}
cout << count << "\n";
++count;
}
cin.ignore();
return 0;
}
something you can copy and paste and run:
only reason I made this sample some months back was because I thought break was breaking out of every loop, this obviously shows otherwise. break will break out of loops ( for, while, do/while, switch ) 1 level.
converted from java, but should be ok, I didn't test....