20
The if-else statement

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

Embed Size (px)

Citation preview

Page 1: 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

The if-else statement

Page 2: 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

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:

if ( condition is true ) then

execute this statement;

otherwise

execute the other statement;

Page 3: 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

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 if-else-statement

A conditional clause ( CONDITION ) follows the keyword if

Following the condition clause, you can write (only) one statement

Following the then-part, you must specify the keyword else followed by (only) one statement

Page 4: 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

Programming example: find maximum of 2 numbers (1)

Page 5: 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

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 ); } }

Page 6: 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

Program example: find maximum of 3 numbers (1)

Page 7: 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

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 ); } }

Page 8: 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

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

Page 9: 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

Programming example: leap year (2)

Page 10: 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

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); } }

Page 11: 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

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"); } } }

Page 12: 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

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

Page 13: 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

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"); } } }

Page 14: 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

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)

}

Page 15: 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

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"); } } }

Page 16: 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

Nested conditional statements

Page 17: 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

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

Page 18: 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

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

Page 19: 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

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

Page 20: 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

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