Is there a goto statement in Java?

前端 未结 23 1949
醉酒成梦
醉酒成梦 2020-11-22 04:50

I\'m confused about this. Most of us have been told that there isn\'t any goto statement in Java.

But I found that it is one of the keywords in Java. Where can it be

23条回答
  •  孤独总比滥情好
    2020-11-22 05:36

    I'm not a fan of goto either, as it usually makes code less readable. However I do believe that there are exceptions to that rule (especially when it comes to lexers and parsers!)

    Of Course you can always bring your program into Kleene Normalform by translating it to something assembler-like and then write something like

    int line = 1;
    boolean running = true;
    while(running)
    {
        switch(line++)
        {
            case 1: /* line 1 */
                    break;
            case 2: /* line 2 */
                    break;
            ...
            case 42: line = 1337; // goto 1337
                    break;
            ...
            default: running = false;
                    break;
        }
    }
    

    (So you basically write a VM that executes your binary code... where line corresponds to the instruction pointer)

    That is so much more readable than code that uses goto, isn't it?

提交回复
热议问题