The ifelse statement The ifelse statement in Java

  • Slides: 20
Download presentation
The if-else statement

The if-else statement

The if-else statement in Java The if-else statement is the seconditional statement in Java

The if-else statement in Java The if-else statement is the seconditional statement in Java The if-else statement selects one of the two possible statements to be executed based on a given condition Example: if ( condition is true ) then execute this statement; otherwise execute the other statement;

Syntax and meaning of the if-else-statement Syntax of the if-else-statement: if ( CONDITION )

Syntax and meaning of the if-else-statement Syntax of the if-else-statement: if ( CONDITION ) ONE-statement else ONE-statement The keyword if announces (to the Java compiler) that we started an ifelse-statement A conditional clause ( CONDITION ) follows the keyword if Following the condition clause, you can write (only) one statement Following then-part, you must specify the keyword else followed by (only) one statement

Programming example: find maximum of 2 numbers (1)

Programming example: find maximum of 2 numbers (1)

Programming example: find maximum of 2 numbers (2) import java. util. Scanner; public class

Programming example: find maximum of 2 numbers (2) import java. util. Scanner; public class Max 01 { public static void main(String[] args) { double a, b, max; Scanner in = new Scanner(System. in); // Construct Scanner object a = in. next. Double(); // Read in next number into a b = in. next. Double(); // Read in next number into b if ( a >= b ) max = a; else max = b; System. out. println( "max value = " + max ); } }

Program example: find maximum of 3 numbers (1)

Program example: find maximum of 3 numbers (1)

Program example: find maximum of 3 numbers (2) import java. util. Scanner; public class

Program example: find maximum of 3 numbers (2) import java. util. Scanner; public class Max 01 { public static void main(String[] args) { double a, b, max; Scanner in = new Scanner(System. in); // Construct Scanner object a = in. next. Double(); // Read in next number into a b = in. next. Double(); // Read in next number into b c = in. next. Double(); // Read in next number into c if ( a >= b ) // Find max(a, b) max = a; else max = b; if ( c > max ) // Check c > max ? max = c; System. out. println( "max value = " + max ); } }

Programming example: leap year (1) In the Gregorian calendar, the current standard calendar in

Programming example: leap year (1) In the Gregorian calendar, the current standard calendar in most of the world, most years that are evenly divisible by 4 are leap years. Years that are evenly divisible by 100 are not leap years, unless they are also evenly divisible by 400, in which case they are leap years Year Leap year ? Reason --------------- ========= 1904 Yes Divisible by 4 1900 No Divisible by 100 2000 Yes Divisible by 400

Programming example: leap year (2)

Programming example: leap year (2)

Programming example: leap year (3) import java. util. Scanner; public class Leap. Year 01

Programming example: leap year (3) import java. util. Scanner; public class Leap. Year 01 { public static void main(String[] args) { int year; boolean leap; Scanner in = new Scanner(System. in); // Construct Scanner object year = in. next. Int(); // Read in year if ( year % 4 == 0 ) leap = true; else leap = false; if ( year % 100 == 0 ) leap = false; if ( year % 400 == 0 ) leap = true; System. out. println("Year is leap year ? " + leap); } }

Programming example: leap year (4) import java. util. Scanner; public class Leap. Year 02

Programming example: leap year (4) import java. util. Scanner; public class Leap. Year 02 { public static void main(String[] args) { int year; boolean leap; Scanner in = new Scanner(System. in); // Construct Scanner object System. out. print("Enter year: "); year = in. next. Int(); // Read in year if ( (year % 4 == 0) && !(year % 100 == 0) || (year % 400 == 0) ) { System. out. println("It is a leap year"); } else { System. out. println("It is NOT a leap year"); } } }

Comparing floating point values on equality (and inequality) (1) When are 2 values equal

Comparing floating point values on equality (and inequality) (1) When are 2 values equal to each other: Two values are equal if they are equal in all digits Consequently: 4. 00000001 != 3. 9999999

Comparing floating point values on equality (and inequality) (2) public class Float. Eq 1

Comparing floating point values on equality (and inequality) (2) public class Float. Eq 1 { public static void main(String[] args) { double a, b; int i; a = 4. 0; b = 4. 0/7. 0 + 4. 0/7. 0; System. out. println("a = " + a); System. out. println("b = " + b); if ( a == b ) { System. out. println("a is equal to b"); } else { System. out. println("a is NOT equal to b"); } } }

Testing equality within a given tolerance (1) When we want to test if 2

Testing equality within a given tolerance (1) When we want to test if 2 values a and b are approximately equal to each other, we use this test: if ( absolute. Value( b − a ) < some-very-small-value ) { a and b are equal } else { a and b are not equal (too far apart) }

Testing equality within a given tolerance (2) public class Float. Eq 2 { public

Testing equality within a given tolerance (2) public class Float. Eq 2 { public static void main(String[] args) { double a, b; int i; a = 4. 0; b = 4. 0/7. 0 + 4. 0/7. 0; System. out. println("a = " + a); System. out. println("b = " + b); if ( Math. abs( b - a ) < 0. 00001 ) { System. out. println("a is (approximately) equal to b"); } else { System. out. println("a is NOT (approximately) equal to b"); } } }

Nested conditional statements

Nested conditional statements

Nested conditional statements A conditional statement (i. e. , an if-statement or an if-elsestatement)

Nested conditional statements A conditional statement (i. e. , an if-statement or an if-elsestatement) is also a statement We can use an if-statement or an if-else-statement in thenpart (and in the else-part) of a conditional statement !!! Nested conditional statement = a conditional statement where then-part and/or the else-part contains another conditional statement

Programming example: determine the price for a hair cut (1)

Programming example: determine the price for a hair cut (1)

Programming example: determine the price for a hair cut (2)

Programming example: determine the price for a hair cut (2)

Programming example: determine the price for a hair cut (3)

Programming example: determine the price for a hair cut (3)