Decisions
In this section we shall study how we can implement control based on certain conditions.
if
File: if1.java
import java.util.* ; public class if1 { public static void main( String args[] ) { int x1 = 5 ; int y1 = 5 ; System.out.println( "Enter a value between 1 and 10" ) ; Scanner scanner = new Scanner(System.in); x1 = scanner.nextInt(); ; if ( x1 <= 5 ) System.out.println( "Value was less than or equal to 5." ) ; //Common error the ; is the empty statement associated //if . The statement following it, is //executed every time. if ( x1 > 5 ); System.out.println( "Value was greater than 5." ) ; //Correct way if ( x1 > 5 ) System.out.println( "Value was greater than 5." ) ; if ( x1 <= 5 ) { //Block associate multiple statement with if System.out.println( "Inside block 1st line:Value was less than or equal to 5." ) ; System.out.println( "Inside block 2nd line: Value was less than or equal to 5." ) ; } if ( x1 <= 5 ) System.out.println( "Inside if else: Value was less than or equal to 5." ) ; else System.out.println( "Inside if else: Value was greater than 5." ) ; //Nested if We can have an if statement inside another if if ( x1 <= 5 ) { System.out.println( "Inside nested if: Value was less than or equal to 5." ) ; if ( x1 <= 3 ) System.out.println( "Inside nested if: Value was less than or equal to 3." ) ; else System.out.println( "Inside nested if: Value was between 3 and 5." ) ; } else System.out.println( "Inside if else: Value was greater than 5." ) ; //Multi way if else if if ( x1 <= 3 ) System.out.println( "Inside multi-way if: Value was less than or equal to 3." ) ; else if ( x1 <= 5 ) System.out.println( "Inside multi-way if: Value was between 4 and 5." ) ; else if ( x1 <= 7 ) System.out.println( "Inside multi-way if: Value was between 6 and 7." ) ; else System.out.println( "Inside multi-way if: Value was between 8 and 10." ) ; } } //class C:\WebSite\Learn\2\Java\decisions>java if1 Enter a value between 1 and 10 3 Value was less than or equal to 5. Value was greater than 5. Inside block 1st line:Value was less than or equal to 5. Inside block 2nd line: Value was less than or equal to 5. Inside if else: Value was less than or equal to 5. Inside nested if: Value was less than or equal to 5. Inside nested if: Value was less than or equal to 3. Inside multi-way if: Value was less than or equal to 3.
Ternary
The ternary expression is a shorthand form for the "if else" statement.File: Ternary.java
import java.util.* ; public class Ternary { public static void main( String args[] ) { int age = 5 ; int y1 = 5 ; System.out.println( "Enter an age: " ) ; Scanner scanner = new Scanner(System.in); age = scanner.nextInt() ; String text = (age < 18) ? "Minor" : "Adult"; System.out.println( "Person is a " + text ) ; //Not necessary to assign to something System.out.println( (age < 18) ? "Minor" : "Adult" ) ; } } //class C:\WebSite\Learn\2\Java\decisions>java Ternary Enter an age: 12 Person is a Minor MinorExercise 1)
File: Ex1.java
Convert the below "if/else" statement into an equivalent ternary statement.
import java.util.* ; public class Ex1 { public static void main( String args[] ) { int x1 ; System.out.println( "Enter a number: " ) ; Scanner scanner = new Scanner(System.in); x1 = scanner.nextInt() ; if ( x1 < 5 ) System.out.println("The number is less than 5" ) ; else System.out.println("The number is greater than 5" ) ; } } //class
Switch
The switch is like a multi-way if else statement. It is more readable and concise and is useful if the statements are small.File: Switch.java
import java.util.* ; import java.time.* ; public class Switch { public static void main( String args[] ) { int x1 = 5 ; int y1 = 5 ; //System.out.println( "Enter a value between 1 and 10" ) ; //Scanner scanner = new Scanner(System.in); // Get today's date LocalDate today = LocalDate.now(); // Get the DayOfWeek enum instance int dayNo = today.getDayOfWeek().getValue() ; System.out.println( "dayNo: " + dayNo ) ; String day = "" ; switch ( dayNo ) { case 1: day = "Monday"; break; case 2: day = "Tuesday"; break; case 3: day = "Wednesday"; break; case 4: day = "Thursday"; break; case 5: day = "Friday"; break; case 6: day = "Saturday"; case 7: day = "Sunday"; break; } System.out.println( "Today's day is :" + day ) ; //System.out.println( "----" ) //Without break the control continues x1 = 0 ; switch ( x1 ) { case 0: System.out.println( "Sunday" ) ; case 1: System.out.println( "Monday" ) ; case 2: System.out.println( "Tuesday" ) ; break; } System.out.println( "----" ) ; //multiple case statements //share same code block switch ( x1 ) { case 0: case 1: System.out.println( "x1 is 0" ) ; System.out.println( "x1 is 1" ) ; break ; case 2: System.out.println( "x1 is 2" ) ; break; } System.out.println( "----" ) ; //default case statement is executed //if there is no match. x1 = 5 ; switch ( x1 ) { case 0: System.out.println( "x1 is 0" ) ; break ; case 1: System.out.println( "x1 is 1" ) ; break ; case 2: System.out.println( "x1 is 2" ) ; break; default: System.out.println( "x1 is something else." ) ; } } } //class C:\WebSite\Learn\2\Java\decisions>java Switch dayNo: 1 Today's day is :Monday Sunday Monday Tuesday ---- x1 is 0 x1 is 1 ---- x1 is something else.