C++ loop
I want to know if you have a infinite while loop and a switch statment inside the while loop is it possible to break out of the while loop from the switch statment. If you just put a break; in the switch statment it only breaks out of the switch statment and not from the loop so what can I do? I know I can probably use goto but I heard thats bad programming pratice.
break only exits the current block, so no, you can't get all the way out without using a goto.
It's probably possible to restructure your code so that this isn't necessary. Alternatively, do something like this:
It's probably possible to restructure your code so that this isn't necessary. Alternatively, do something like this:
bool bRun = true;MSG msg;while( bRun ){ if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) { switch( msg ) { case WM_QUIT: bRun = false; //break out } }}This way you have essentially an infinite loop, but leave yourself with an escape route.
This is pretty much the only place that the goto keyword should be used:
BUT I would suggest the method used above (Thanks Promit :D), since I don't like the goto statement (seems too BASIC too me :D).
[Edited by - Programmer16 on September 10, 2004 3:48:59 PM]
MSG msg;while( true ){ if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) { switch( msg ) { case WM_QUIT: goto ExitMessageLoop; } }}ExitMessageLoop://code here
BUT I would suggest the method used above (Thanks Promit :D), since I don't like the goto statement (seems too BASIC too me :D).
[Edited by - Programmer16 on September 10, 2004 3:48:59 PM]
Another solution is to encapsulate the loops (or loop and switch, etc) into a function and use return instead of break when you need to jump out of the major block completely.
Quote:Original post by Programmer16
BUT I would suggest the method used below, since I don't like the goto statement (seems too BASIC too me :D).
By "below" I guess you mean "above" ? [wink]
The boolean exit flag is quite preferred to the goto, I think.
Or you could make the condition for your loop false/true (depending on what will end the loop) in one of your switches.
Yeah its simplest and just to say:
bool done = false;
while(done != true) {
switch(MyVariable) {
case 0:
done = true;
break; //Exits while loop because done is now true
}
}
bool done = false;
while(done != true) {
switch(MyVariable) {
case 0:
done = true;
break; //Exits while loop because done is now true
}
}
Am I the only one to think that the method including the goto is easier to read?
Considering what actually gets executed, the goto is also more efficient, unless you have a compiler capable of transforming the boolean flag method into the goto method.
The main thrust of gotoless programming is that it makes programs easier to verify. If you want to be able to verify a program, however, a functional language is a better idea.
Goto considered harmful:
http://www.acm.org/classics/oct95/
Structured Programming with goto statements:
http://pplab.snu.ac.kr/courses/PL2001/papers/p261-knuth.pdf
Considering what actually gets executed, the goto is also more efficient, unless you have a compiler capable of transforming the boolean flag method into the goto method.
The main thrust of gotoless programming is that it makes programs easier to verify. If you want to be able to verify a program, however, a functional language is a better idea.
Goto considered harmful:
http://www.acm.org/classics/oct95/
Structured Programming with goto statements:
http://pplab.snu.ac.kr/courses/PL2001/papers/p261-knuth.pdf
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement