Last active
June 26, 2017 04:32
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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