### The if-else statement

• Introducing the if-else statement

• Programming problem:

 Re-write the a,b,c-formula program to solve for complex number solutions when b2 - 4ac < 0

• Algorithm:

 ``` input a, b, c; Det = b*b - 4*a*c; // Compute the determinant if ( Det >= 0 ) { print -b/(2a) + sqrt(Det)/(2a); // Real number solutions print -b/(2a) - sqrt(Det)/(2a); } if ( Det < 0 ) { print -b/(2a) "+" (sqrt(-Det)/(2a) + "i"); // Complex number solutions print -b/(2a) "-" (sqrt(-Det)/(2a) + "i"); } ```

• Java program:

 ``` import java.util.Scanner; public class Abc3 { public static void main(String[] args) { double a, b, c, Det, re, im; 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 Det = b*b - 4*a*c; if ( Det >= 0 ) { System.out.println( (-b + Math.sqrt( Det ) ) / (2*a) ); System.out.println( (-b - Math.sqrt( Det ) ) / (2*a) ); } if ( Det < 0 ) { re = -b/(2*a); // Compute real part im = Math.sqrt( -Det )/(2*a); // Compute imaginary part System.out.println( re + "+" + im + "i" ); System.out.println( re + "-" + im + "i" ); } } } ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac Abc3.java To run:          java Abc3

Example:

 ``` Enter a:1 Enter b:2 Enter c:5 -1.0+2.0i -1.0-2.0i ```

Shortcoming:

 We have to negate the if-condition ourselves... This can introduce unnecessary errors

• Solution:

 Extend the if-statement with an alternative statement The alternative statement is only executed when the if-condition is false

• The if-else statement in Java

• The if-else statement:

• 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; ```

• Syntax and meaning of the if-else-statement

• Syntax of the if-else-statement:

 ``` if ( CONDITION ) ONE-statement else ONE-statement ```

Explanation:

• The keyword if announces (to the Java compiler) that we started an if-else-statement

• A conditional clause ( CONDITION ) follows the keyword if

 This is the condition of the if-else-statement

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

 This statement will only be executed if the condition is true

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

 This statement will only be executed if the condition is false

Note:

• The way that the Java compiler decide whether a conditional statement is:

 An if-statement An if-else-statement

is by the presence/absence of the keyword else.

• Computer Jargon: else-part

• The statement following the keyword else in an if-else-statement is called

 The else-part of the if-else-statement (Or else-part for short)

Schematically:

• The a,b,c-formula using an if-else-statement

• Programming problem:

 Re-write the a,b,c-formula program to solve for complex number solutions when b2 - 4ac < 0

• Algorithm:

• Java program:

 ``` import java.util.Scanner; public class Abc4 { public static void main(String[] args) { double a, b, c, Det, re, im; 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 Det = b*b - 4*a*c; if ( Det >= 0 ) { System.out.println( (-b + Math.sqrt( Det ) ) / (2*a) ); System.out.println( (-b - Math.sqrt( Det ) ) / (2*a) ); } else { re = -b/(2*a); // Compute real part im = Math.sqrt( -Det )/(2*a); // Compute imaginary part System.out.println( re + "+" + im + "i" ); System.out.println( re + "-" + im + "i" ); } } } ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac Abc4.java To run:          java Abc4

• Programming example: find maximum of 2 numbers

• Programming problem:

 Read in 2 number a and b Assign to the variable max the largest value of a and b

• Algorithm:

• Java program:

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

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac Max01.java To run:          java Max01

• Program example: find maximum of 3 numbers

• Programming problem:

 Read in 3 number a, b and c Assign to the variable max the largest value of a, b and c

• Algorithm:

• Java program:

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

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac Max02.java To run:          java Max02

• Programming example: leap year

• Leap year description (Wikipedia):

 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

• Examples:

 ``` Year Leap year ? Reason ---------- --------------- ================= 1904 Yes Divisible by 4 1900 No Divisible by 100 2000 Yes Divisible by 400 ```

• Algorithm:

• Program in Java:

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

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac LeapYear01.java To run:          java LeapYear01

• Common errors in if-else-statements

• Common error 1: bogus semicolon after the if-condition

Example:

 ``` if ( a >= b ) ; // Bogus ; max = a; else max = b; ```

Compiler message:

 ``` Error01.java:18: 'else' without 'if' else ^ ```

because the compiler "reads" the program as follows:

 ``` if ( a >= b ) // A correct if-statement ; // Note: the if-statement ended here ! max = a; // A correct assignment statement else // else ? Where is the if ??? max = b; ```

• Common error 2: forgetting to use statement block in the then-part

Example:

 ``` if ( Det >= 0 ) System.out.println( (-b + Math.sqrt( Det ) ) / (2*a) ); System.out.println( (-b - Math.sqrt( Det ) ) / (2*a) ); else re = -b/(2*a); // Compute real part im = Math.sqrt( -Det )/(2*a); // Compute imaginary part System.out.println( re + "+" + im + "i" ); System.out.println( re + "-" + im + "i" ); ```

The Java compiler will report the error 'else' without 'if' because syntactically, the program is read as follows:

 ``` if ( Det >= 0 ) System.out.println( (-b + Math.sqrt( Det ) ) / (2*a) ); // If-statement System.out.println( (-b - Math.sqrt( Det ) ) / (2*a) ); // Print else // Else without if re = -b/(2*a); // Compute real part im = Math.sqrt( -Det )/(2*a); // Compute imaginary part System.out.println( re + "+" + im + "i" ); System.out.println( re + "-" + im + "i" ); ```

• Common error 3: missing semicolon after the then-part

Example:

 ``` if ( a >= b ) max = a // Missing semicolon !!! else max = b; ```

Compiler message:

 ``` Error03.java:17: ';' expected max = a ^ ```

Reason:

 The then-part is ONE statement A statement must be ended with a semicolon

• Common error 4: bogus semicolon after the block in the then-part

Example:

 ``` if ( a >= b ) { max = a; }; // bogus semicolon !!! else { max = b; } ```

Compiler message:

 ``` Error04.java:20: 'else' without 'if' else ^ ```

Because syntactically, the program is read as follows:

 ``` if ( a >= b ) // An if-statement { max = a; } ; // An empty statement !!! else // Else without if... { max = b; } ```

 ``` if ( ..... ) { (leave empty first) } if ( ..... ) { (leave empty first) } else { (leave empty first) } ```