Java if-if-else behavior

后端 未结 7 672
夕颜
夕颜 2020-12-11 21:18

I wrote a simple if/else in my code which worked fine. Later I added another level of if under the first, and was baffled by its behavior. Here\'

相关标签:
7条回答
  • 2020-12-11 21:43

    Is there a way to make the above code do what the indentation makes you expect without adding braces?

    if (a)
        if (b)
            System.out.println("a=true, b=true");
        else;
    else
        System.out.println("a=false");
    

    else; will definitively finish the innermost if.

    I would like to note that, if I ever came across this code in production, I would hunt through the commit logs until I found out who wrote it. This is absolutely an unacceptable thing to do. but it answers the quoted question

    0 讨论(0)
提交回复
热议问题