java – If branches and my example

Question:

friends! We were given the task to make branches in Java, so I would like to ask if my code is done correctly or not:

public static void main(String[] args) {
    int age = 13;
    if (age == 25) {
        if (age == 33) {
            if (age == 50) {
                System.out.println("Igor");
            }
        }
    } else {
        System.out.println("Error");
    }
}

Answer:

The "loop" is done incorrectly. Replace it with much simpler code:

    if (age != 25) {
        System.out.println("Error");
    }
Scroll to Top