Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use goto with switch?

It seems it's possible with C#, but I need that with C++ and preferably cross platform.

Basically, I have a switch that sorts stuff on single criteria, and falls back to default processing on everything else.

Say:

switch(color) { case GREEN: case RED: case BLUE:     Paint();     break; case YELLOW:     if(AlsoHasCriteriaX)         Paint();     else         goto default;     break; default:     Print("Ugly color, no paint.")     break; } 
like image 886
Coder Avatar asked Nov 20 '11 14:11

Coder


People also ask

Can goto be used in switch?

A label is a valid C# identifier followed by colon. In this case, case and default statements of a Switch are labels thus they can be targets of a goto. However, the goto statement must be executed from within the switch. that is we cannot use the goto to jump into switch statement .

Can we use goto statement in switch case?

@The Smartest: A switch statement in itself can already have plenty of goto s, but they are named differently: Each break is a goto. @chiccodoro That's nothing, actually each case is a label and switch essentially is a goto . Each break is a goto , but also each continue is a goto too.

Can goto statement Go outside switch?

Yes, it does.

Why goto is forbidden?

"The GOTO statement is generally considered to be a poor programming practice that leads to unwieldy programs. Its use should be avoided."


1 Answers

Ahmed's answer is good, but there's also:

switch(color) case YELLOW:     if(AlsoHasCriteriaX) case GREEN: case RED: case BLUE:         Paint();     else default:         Print("Ugly color, no paint."); 

people tend to forget how powerful switches are

like image 50
Steve Cox Avatar answered Sep 19 '22 21:09

Steve Cox