A statement at the end of the last case in switch statement

Pages: 12
As for you question why use a switch? there are cases you will want to use a switch and only have some but not all cases break, some you will want to continue and execute the next case of commands.
edit: However this is completely irrelevant.

What do you mean by iterate? I'm not iterating in my code.
Last edited on
closed account (j3bk4iN6)
ok thanks, sorry if I seem a little argumentative.
with switch statements, lets pretend the conditional is an integer and is 1.

lets say our switch looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
switch(opt)
	{
	case 1: 
		//... do some stuff
	case 2: 
		//... do some more
	case 3: opt++; break;
		//...
        case 4: 
                // do something else
        case 5:
               // do some more
               break;
    default: 
           errorCode();
	}


in the case of 1, it will execute commands in 1 2 and 3, the condition is not read for the other cases, the commands are simply executed.
in the case of 2, it will execute commands 2 and 3.
in the case of 3, it will execute commands in 3.
in the case of 4, will execute 4 and 5.
in the case of 5, will execute 5.
in the case of something else will execute errorCode which happens to be a function that prints out an error to log and exits the program.

There's nothing wrong with using a switch statement to achieve this. Although it may seem awkward and hard to follow...
closed account (j3bk4iN6)
then why not put the comands 1 2 and 3 in case 1:
and commands 4 5 in case 3:

I guess its up to the programmer if it makes code easier to read, or the commands are longer.
Last edited on
well in that case then you would need to have repeat calls depending on the input:
so if we go by what your saying it would look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if (opt == 1)
   doCMD1();
   doCMD2();
   doCMD3();
else if (opt == 2)
   doCMD2();
   doCMD3();
else if (opt == 3)
   doCMD3();
else if (opt == 4)
   doCMD4();
   doCMD5();
else if (opt == 5)
   doCMD5();
else
   doErrorOut_Exit();


really what I just wrote is no different to the switch, but in the case of 3 I don't want command 1 and 2 executed only 3.
Last edited on
closed account (j3bk4iN6)
Oh I see, thanks for your patience
gcampton wrote:
the last case whether that be default, or case 27387237, needs to have a command, function, or break something anything that does something... It CANNOT BE BLANK.


Thx a lot, now I get it :)
Topic archived. No new replies allowed.
Pages: 12