How to use Switch from within a loop?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Tiranasta
Posts: 4
Joined: Tue May 15, 2007 7:31 am

How to use Switch from within a loop?

Post by Tiranasta »

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?
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

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
shogun
Posts: 162
Joined: Wed Sep 05, 2007 11:02 am
Location: inside

Post by shogun »

You could just use "if (val == 1) ... else if (val == 2) ... else if (val == 3) ... else ..." instead of "switch(val) { case 1: ... break; case 2: ... break; case 3: ... break; default: ... break; }".
zeno60
Posts: 342
Joined: Sun May 21, 2006 2:48 am
Location: NC, USA
Contact:

Re: How to use Switch from within a loop?

Post by zeno60 »

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?
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.
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Please post the whole loop code in here.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Pörzi
Posts: 9
Joined: Wed Aug 08, 2007 8:16 am

Re: How to use Switch from within a loop?

Post by Pörzi »

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.
I think so too. This proves it.

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;
}
--> a = 10
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

No, this proves that the loop executed 10 times. If you got 'a = 0' then you'd be right.

Travis
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

vitek wrote:No, this proves that the loop executed 10 times. If you got 'a = 0' then you'd be right.

Travis
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.

Either way that it how break is supposed to work.
TheQuestion = 2B || !2B
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Re: How to use Switch from within a loop?

Post by MasterGod »

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?
It doesn't stop the loop..
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.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

@MasterGod: you're talking about 2 different people... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Daaark
Posts: 19
Joined: Sun Jan 14, 2007 11:14 pm
Location: IUnknown

Re: How to use Switch from within a loop?

Post by Daaark »

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;
}
This will work fine, and is perfectly okay, despite what some very uptight people may tell you.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

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
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Acki wrote:@MasterGod: you're talking about 2 different people... ;)
The hour did its thing.. :)

About the goto, its low programming, IMO you should look for any other way then using goto.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Saturn
Posts: 418
Joined: Mon Sep 25, 2006 5:58 pm

Post by Saturn »

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
Pörzi
Posts: 9
Joined: Wed Aug 08, 2007 8:16 am

Re: How to use Switch from within a loop?

Post by Pörzi »

I uploaded an avatar to prevent further confusion.
Daaark wrote: This will work fine, and is perfectly okay, despite what some very uptight people may tell you.
Imo there are two reasons why one should not use goto:

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.
Post Reply