mardi 18 août 2020

If statement with no curly braces works with multiline if statement inside it? [duplicate]

public class Main {
    public static void main(String[] args) {
        int x = 1;
        if (x < 10)
            if (x == 0)
                System.out.println("Zero");
            else if (x == 1)
                System.out.println("One");
            else
                System.out.println("Other but smaller than 10");
        else
            System.out.println("Greater than 10");
    }
}

I have just tried this and it worked exactly the same as if I surrounded the inner conditions with curly braces.
I know that it's possible to do one line statements with no curly braces, but why does it work in this case?

Thanks and I'm sorry if this is an obvious question.

Aucun commentaire:

Enregistrer un commentaire