Skip to content
thesarfo

Concept

Conditional Statements

If/else ladders and switch-case statements in Java.

views 0
public class Main {
public static void main(String[] args) {
if (condition) {
// do this
} else if (another_condition) {
// do this
} else {
// do this
}
}
}

You can have multiple else if statements and nested if statements, but they can become too verbose and complex. There’s a simpler way called switch-case that can do the same thing as a complicated if-else ladder.

public class Main {
public static void main(String[] args) {
switch (variable_you_wanna_check) {
case "first":
// do something
break;
case "second":
// do something
break;
case "third":
// do something
break;
default: {
// do this if none of the above cases are met
}
}
}
}