280 likes | 529 Views
The if-else statement. The if-else statement in Java. The if-else statement is the second conditional statement in Java The if-else statement selects one of the two possible statements to be executed based on a given condition Example:
E N D
The if-else statement in Java • The if-else statement is the second conditional statement in Java • The if-else statementselectsone 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 ) • ONE-statement • else • ONE-statement • The keywordif announces (to the Java compiler) that we started an if-else-statement • A conditional clause( CONDITION ) follows the keywordif • Following the condition clause, you can write (only) one statement • Following the then-part, you must specify the keywordelse followed by (only) one statement
Programming example: find maximum of 2 numbers (2) • import java.util.Scanner; • public class Max01 • { • public static void main(String[] args) • { • double a, b, max; • Scanner in = new Scanner(System.in); // Construct Scanner object • a = in.nextDouble(); // Read in next number into a • b = in.nextDouble(); // 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 (2) • import java.util.Scanner; • public class Max01 • { • public static void main(String[] args) • { • double a, b, max; • Scanner in = new Scanner(System.in); // Construct Scanner object • a = in.nextDouble(); // Read in next number into a • b = in.nextDouble(); // Read in next number into b • c = in.nextDouble(); // 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 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 (3) • import java.util.Scanner; • public class LeapYear01 • { • public static void main(String[] args) • { • int year; • boolean leap; • Scanner in = new Scanner(System.in); // Construct Scanner object • year = in.nextInt(); // 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 LeapYear02 • { • 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.nextInt(); // 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 to each other: • Two values are equal if they are equal in all digits • Consequently: • 4.00000000000001 != 3.9999999999999
Comparing floating point values on equality (and inequality) (2) • public class FloatEq1 • { • public static void main(String[] args) • { • double a, b; • int i; • a = 4.0; • b = 4.0/7.0 + 4.0/7.0 + 4.0/7.0 + 4.0/7.0 + 4.0/7.0 + 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 values a and b are approximately equal to each other, we use this test: • if ( absoluteValue( 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 FloatEq2 • { • public static void main(String[] args) • { • double a, b; • int i; • a = 4.0; • b = 4.0/7.0 + 4.0/7.0 + 4.0/7.0 + 4.0/7.0 + 4.0/7.0 + 4.0/7.0 + 4.0/7.0; • System.out.println("a = " + a); • System.out.println("b = " + b); • if ( Math.abs( b - a ) < 0.000000001 ) • { • System.out.println("a is (approximately) equal to b"); • } • else • { • System.out.println("a is NOT (approximately) equal to b"); • } • } • }
Nested conditional statements • A conditional statement (i.e., an if-statement or an if-else-statement) is also a statement • We can use an if-statement or an if-else-statement in the then-part (and in the else-part) of a conditional statement !!! • Nested conditional statement = a conditional statement where the then-part and/or the else-part contains another conditional statement