Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using The if-else Statement in Java http://www.chankok.com/java-use-the-if-else-statement/
public class IfElseStatementExample {
public static void main(String[] args) {
// Initialization
int score = 0;
String today = "";
// Example 1: if Statement
score = 150;
if (score > 100) {
System.out.println("The score is more than 100");
}
// Example 2: if-else Statement
score = 30;
if (score >= 40) {
System.out.println("The result is passed");
} else {
System.out.println("The result is failed");
}
// Example 3: if-else-if-else Statement
today = "Wednesday";
if (today.equals("Monday")) {
System.out.println("Today is Monday");
} else if (today.equals("Tuesday")) {
System.out.println("Today is Tuesday");
} else if (today.equals("Wednesday")) {
System.out.println("Today is Wednesday");
} else if (today.equals("Thursday")) {
System.out.println("Today is Thursday");
} else if (today.equals("Friday")) {
System.out.println("Today is Friday");
} else {
System.out.println("Today is weekend");
}
// Example 4: Nested if Statement
score = 55;
if (score >= 40) {
if (score < 60) {
System.out.println("The result is grade C");
}
} else {
System.out.println("The result is failed");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment