- the if-else statement, to choose between two alternatives;
- the switch statement, to choose between multiple alternatives.
if-else Statement
The if-then statement is the most basic of all the control flow and most use conditional statements. It tells your program to execute a certain section of code only if a particular test evaluates to true.
1 2 3 4 5 6 7 8 9 10 11 12 |
class IfStatement { public static void main(String[] args) { int number = 10; if (number > 0) { System.out.println("Number is positive."); } System.out.println("This statement is always executed."); } } |
if…else (if-then-else) Statement
The if statement executes a certain section of code if the test expression is evaluated to true. The if statement can have an optional else statement. Codes inside the body of else statements are executed if the test expression is false.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class IfElse { public static void main(String[] args) { int number = 10; if (number > 0) { System.out.println("Number is positive."); } else { System.out.println("Number is not positive."); } System.out.println("This statement is always executed."); } } |
if..else..if Statement
The if statements are executed from the top towards the bottom. As soon as the test expression is true, codes inside the body of that if the statement is executed. Then, the control of the program jumps outside the if-else-if ladder. If all test expressions are false, codes inside the body of else are executed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class Ladder { public static void main(String[] args) { int number = 0; if (number > 0) { System.out.println("Number is positive."); } else if (number < 0) { System.out.println("Number is negative."); } else { System.out.println("Number is 0."); } } } |
Nested if…else Statement
It’s possible to have if..else statements inside an if..else statement in Java. It’s called a nested if…else statement. Here’s a program to find largest of 3 numbers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
class Number{ public static void main(String[] args) { Double n1 = -1.0, n2 = 4.5, n3 = -5.3, largestNumber; if (n1 >= n2) { if (n1 >= n3) { largestNumber = n1; } else { largestNumber = n3; } } else { if (n2 >= n3) { largestNumber = n2; } else { largestNumber = n3; } } System.out.println("Largest number is " + largestNumber); } } |
The switch Statement
Unlike if-then and if-then-else statements, the switch statement can have a number of possible execution paths. A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types (discussed in Enum Types), the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer (discussed in Numbers and Strings). The switch statement is most uses conditional statements like if-else.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
public class SwitchDemo { public static void main(String[] args) { int month = 8; String monthString; switch (month) { case 1: monthString = "January"; break; case 2: monthString = "February"; break; case 3: monthString = "March"; break; case 4: monthString = "April"; break; case 5: monthString = "May"; break; case 6: monthString = "June"; break; case 7: monthString = "July"; break; case 8: monthString = "August"; break; case 9: monthString = "September"; break; case 10: monthString = "October"; break; case 11: monthString = "November"; break; case 12: monthString = "December"; break; default: monthString = "Invalid month"; break; } System.out.println(monthString); } } |
Ref: docs.oracle.com
Conditional Statements in Java
The article was published on August 1, 2017 @ 12:59 AM
Leave a Comment