Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translate goto statements to if, switch, while, break, etc

Tags:

java

syntax

goto

Is there a way to mechanically translate goto statements to if, switch, while, break, and continue statements, etc, or with function calls, objects, anything?

like image 202
unknown Avatar asked Nov 28 '22 23:11

unknown


1 Answers

While it is not a good idea, it is possible using loops and swith-case. In the following example the goto variable decides what label (0, 1, 2 or default) to goto when you get to a continue.

int goTo=0;
while(true){
  switch(goTo){
  case 0:
    doSomething();
    goTo = 1;
    continue;
  case 1:
    doSomethingElse();
    goTo = 2;
    continue;
  case 2:
    doSOmethingDifferent();
    goTo = 0;
    continue;
  default:
    return;
  }
}
like image 161
Marius Avatar answered Dec 09 '22 18:12

Marius