Title: The if-else statement
1The if-else statement
2Introducing the if-else statement
- Re-write the a,b,c-formula program to solve for
complex number solutions when b2 - 4ac lt 0
3Introducing the if-else statement (cont.)
input a, b, c Det bb - 4ac //
Compute the determinant if ( Det gt 0 )
print -b/(2a) sqrt(Det)/(2a) // Real
number solutions print -b/(2a) -
sqrt(Det)/(2a) if ( Det lt 0 )
print -b/(2a) "" (sqrt(-Det)/(2a) "i") //
Complex number solutions print -b/(2a)
"-" (sqrt(-Det)/(2a) "i")
4Introducing the if-else statement (cont.)
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
5Introducing the if-else statement (cont.)
Det bb - 4ac if ( Det
gt 0 ) System.out.println( (-b
Math.sqrt( Det ) ) / (2a) )
System.out.println( (-b - Math.sqrt( Det ) ) /
(2a) ) if ( Det lt 0 )
re -b/(2a) //
Compute real part im Math.sqrt( -Det
)/(2a) // Compute imaginary part
System.out.println( re "" im "i" )
System.out.println( re "-" im "i" )
6Introducing the if-else statement (cont.)
- Example Program (Demo above code)
- Prog file http//mathcs.emory.edu/cheung/Courses
/170/Syllabus/06/Progs/Abc3.java - How to run the program
- Right click on link and save in a scratch
directory - To compile javac Abc3.java
- To run java Abc3
7Introducing the if-else statement (cont.)
Enter a1 Enter b2 Enter c5 -1.02.0i
-1.0-2.0i
8Introducing the if-else statement (cont.)
- We have to negate the if-condition ourselves...
- This can introduce unnecessary errors
9Introducing the if-else statement (cont.)
- Extend the if-statement with an alternative
statement - The alternative statement is only executed when
the if-condition is false
10The if-else statement in Java
- The if-else statement is the second conditional
statement in Java - The if-else statement selects one of two
statements to be executed based on a given
condition
11Syntax and meaning of the if-else-statement
- Syntax of the if-else-statement
if ( CONDITION ) ONE- statement else
ONE-statement
12Syntax and meaning of the if-else-statement
(cont.)
- 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
13Syntax and meaning of the if-else-statement
(cont.)
- 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
- This statement will only be executed if the
condition is true
- This is the condition of the if-else-statement
- This statement will only be executed if the
condition is false
14Syntax and meaning of the if-else-statement
(cont.)
The way that the Java compiler decide whether a
conditional statement is is by the
presence/absence of the keyword else.
- An if-statement
- An if-else-statement
15Computer 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)
16Computer Jargon else-part (cont.)
17The a,b,c-formula using an if-else-statement
- Programming problem
- Algorithm
- Re-write the a,b,c-formula program to solve for
complex number solutions when b2 - 4ac lt 0
18The a,b,c-formula using an if-else-statement
(cont.)
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 bb - 4ac
19The a,b,c-formula using an if-else-statement
(cont.)
- if ( Det gt 0 )
-
- System.out.println( (-b Math.sqrt(
Det ) ) / (2a) ) - System.out.println( (-b - Math.sqrt(
Det ) ) / (2a) ) -
- else
-
- re -b/(2a) //
Compute real part - im Math.sqrt( -Det )/(2a) // Compute
imaginary part - System.out.println( re "" im
"i" ) - System.out.println( re "-" im
"i" ) -
-
-
20The a,b,c-formula using an if-else-statement
(cont.)
- Example Program (Demo above code)
- Prog file http//mathcs.emory.edu/cheung/Courses
/170/Syllabus/06/Progs/Abc4.java - How to run the program
- Right click on link and save in a scratch
directory - To compile javac Abc4.java
- To run java Abc4
21Programming example find maximum of 2 numbers
- Read in 2 number a and b
- Assign to the variable max the largest value of
a and b
22Programming example find maximum of 2 numbers
(cont.)
23Programming example find maximum of 2 numbers
(cont.)
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 gt b )
max a else max b
System.out.println( "max value " max )
24Programming example find maximum of 2 numbers
(cont.)
- Example Program (Demo above code)
- Prog file http//mathcs.emory.edu/cheung/Courses
/170/Syllabus/06/Progs/Max01.java - How to run the program
- Right click on link and save in a scratch
directory - To compile javac Max01.java
- To run java Max01
25Program example find maximum of 3 numbers
- Read in 3 number a, b and c
- Assign to the variable max the largest value of
a, b and c
26Program example find maximum of 3 numbers (cont.)
27Program example find maximum of 3 numbers (cont.)
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
28Program example find maximum of 3 numbers (cont.)
- if ( a gt b ) // Find max(a,b)
- max a
- else
- max b
- if ( c gt max ) // Check c gt max ?
- max c
- System.out.println( "max value " max
) -
-
29Program example find maximum of 3 numbers (cont.)
- Example Program (Demo above code)
- Prog file http//mathcs.emory.edu/cheung/Courses
/170/Syllabus/06/Progs/Max02.java - How to run the program
- Right click on link and save in a scratch
directory - To compile javac Max02.java
- To run java Max02
30Programming 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
31Programming example leap year (cont.)
32Programming example leap year (cont.)
import java.util.Scanner public class
LeapYear01 public static void
main(String args) int year
boolean leap
33Programming example leap year (cont.)
- 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) -
-
34Programming example leap year (cont.)
- Example Program (Demo above code)
- Prog file http//mathcs.emory.edu/cheung/Courses
/170/Syllabus/06/Progs/LeapYear01.java - How to run the program
- Right click on link and save in a scratch
directory - To compile javac LeapYear01.java
- To run java LeapYear01
35Common errors in if-else-statements
- Common error 1 bogus semicolon after the
if-condition - Example
if ( a gt b ) // Bogus
max a else max
b
36Common errors in if-else-statements (cont.)
Error01.java18 'else' without 'if'
else
37Common errors in if-else-statements (cont.)
- because the compiler "reads" the program as
follows
if ( a gt 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
38Common errors in if-else-statements (cont.)
- Common error 2 forgetting to use statement block
in the then-part - Example
if ( Det gt 0 ) System.out.println(
(-b Math.sqrt( Det ) ) / (2a) )
System.out.println( (-b - Math.sqrt( Det ) ) /
(2a) ) else re -b/(2a)
// Compute real part im Math.sqrt(
-Det )/(2a) // Compute imaginary part
System.out.println( re "" im "i"
) System.out.println( re "-" im
"i" )
39Common errors in if-else-statements (cont.)
- The Java compiler will report the error 'else'
without 'if' because syntactically, the program
is read as follows
if ( Det gt 0 ) System.out.println(
(-b Math.sqrt( Det ) ) / (2a) ) //
If-statement System.out.println( (-b -
Math.sqrt( Det ) ) / (2a) ) // Print
else
// Else without if re
-b/(2a) // Compute real part
im Math.sqrt( -Det )/(2a) // Compute
imaginary part System.out.println(
re "" im "i" )
System.out.println( re "-" im "i" )
40Common errors in if-else-statements (cont.)
- Common error 3 missing semicolon after the
then-part - Example
if ( a gt b ) max a //
Missing semicolon !!! else
max b
41Common errors in if-else-statements (cont.)
Error03.java17 '' expected
max a
42Common errors in if-else-statements (cont.)
- The then-part is ONE statement
- A statement must be ended with a semicolon
43Common errors in if-else-statements (cont.)
- Common error 4 bogus semicolon after the block
in the then-part - Example
if ( a gt b ) max a
// bogus semicolon
!!! else max b
44Common errors in if-else-statements (cont.)
Error04.java20 'else' without 'if'
else
45Common errors in if-else-statements (cont.)
- The then-part is ONE statement
- A statement must be ended with a semicolon
46Common errors in if-else-statements (cont.)
- Common error 4 bogus semicolon after the block
in the then-part - Example
if ( a gt b ) max a
// bogus semicolon
!!! else max b
47Common errors in if-else-statements (cont.)
Error04.java20 'else' without 'if'
else
48Common errors in if-else-statements (cont.)
- Because syntactically, the program is read as
follows
if ( a gt b ) // An if-statement
max a
// An empty statement !!!
else // Else without if...
max b
49Programming advice
- As you can see, forgetting a "" and adding an
extra "" can cause serious syntax errors - The best thing is to stick to one useful form
use block !!!
50Programming advice (cont.)
- I always write if-statements and
if-else-statements using blocks first
if ( ..... ) (leave empty first)
--------------------------------------
-------- if ( ..... ) (leave empty
first) else (leave empty
first)
51Programming advice (cont.)
- I fill in the statements in the then-part and
else-part later. - So even when the if-part and/or else-part
consist of 1 statement, I write them as blocks.