How to use Switch from within a loop?
How to use Switch from within a loop?
Ok, I have switch (random) within a for loop. The problem is, when I use 'break' to prevent the case falling through, it also stops the loop. Is there a way to prevent this?
-
- Admin
- Posts: 3590
- Joined: Mon Oct 09, 2006 9:36 am
- Location: Scotland - gonnae no slag aff mah Engleesh
- Contact:
Please post your code.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Re: How to use Switch from within a loop?
I'm pretty sure the break statement only exits out of its enclosing for/while/do loop or switch statement. You must have something else going on for the loop to be ending prematurely.Tiranasta wrote:Ok, I have switch (random) within a for loop. The problem is, when I use 'break' to prevent the case falling through, it also stops the loop. Is there a way to prevent this?
Re: How to use Switch from within a loop?
I think so too. This proves it.zeno60 wrote:I'm pretty sure the break statement only exits out of its enclosing for/while/do loop or switch statement. You must have something else going on for the loop to be ending prematurely.
Code: Select all
#include <iostream>
using namespace::std;
int main()
{
int a=0;
for (0;a<10;a++)
{
switch (a)
{
case 0: break;
case 1: break;
default : break;
}
}
cout << "a = " << a << "\n";
return 0;
}
Exactly, he was trying to prove that it executed 10 times, and did not exit the for loop. It just exited the brackets that it was contained in.vitek wrote:No, this proves that the loop executed 10 times. If you got 'a = 0' then you'd be right.
Travis
Either way that it how break is supposed to work.
TheQuestion = 2B || !2B
Re: How to use Switch from within a loop?
It doesn't stop the loop..Tiranasta wrote:Ok, I have switch (random) within a for loop. The problem is, when I use 'break' to prevent the case falling through, it also stops the loop. Is there a way to prevent this?
You even said it yourself, it prints "a = 10" meaning the loop reached the point where it checks "10 < 10 ?" - which is the end of the loop.
@MasterGod: you're talking about 2 different people...
while(!asleep) sheep++;
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Re: How to use Switch from within a loop?
Code: Select all
#include <iostream>
using namespace::std;
int main()
{
int a=0;
for (0;a<10;a++)
{
switch (a)
{
case 0: goto loopend;
case 1: goto loopend;
default : break;
}
}
loopend:
cout << "a = " << a << "\n";
return 0;
}
Yes, most programmers will say that code is bad because it uses goto. This alone doesn't really bad, but it is easy to write goto code that becomes difficult to follow. Especially since there are other ways to do the same thing that are easier on the eyes. I mean you could implement a for loop using goto, but you don't see people doing that anymore. It's just to hard to follow when the code gets complicated.
The example is pretty convoluted though. The loop and switch would be completely optimized out because the first case bails out of the loop without any side effects.
Travis
The example is pretty convoluted though. The loop and switch would be completely optimized out because the first case bails out of the loop without any side effects.
Travis
Goto has its uses and is sometimes the most elegant solution for a problem. But the example code provided is bad in itself.
Loop-switch over a constant range is something very commonly seen, but mostly it is useless. The code is then better readable if the switch's case statements are just executed in order.
See http://en.wikipedia.org/wiki/Loop-switch_sequence
Loop-switch over a constant range is something very commonly seen, but mostly it is useless. The code is then better readable if the switch's case statements are just executed in order.
See http://en.wikipedia.org/wiki/Loop-switch_sequence
Re: How to use Switch from within a loop?
I uploaded an avatar to prevent further confusion.
1) It is generally not regarded as a good way to program.
2) The code will not be easy to read if you use is a lot.
Imo there are two reasons why one should not use goto:Daaark wrote: This will work fine, and is perfectly okay, despite what some very uptight people may tell you.
1) It is generally not regarded as a good way to program.
2) The code will not be easy to read if you use is a lot.