Skip to content

Instantly share code, notes, and snippets.

@chankok
Last active June 26, 2017 04:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chankok/657e668e47db9e726a29a47e9b957dd6 to your computer and use it in GitHub Desktop.
Save chankok/657e668e47db9e726a29a47e9b957dd6 to your computer and use it in GitHub Desktop.
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