Chapter 7 Selections
Part (2)
Liang, Introduction to Java
Programming, Tenth Edition, (c)
1
2015 Pearson Education, Inc. All
Introduction
● This lecture will cover boolean variables ,how
write Boolean expressions using relational
operators ,implement selection control using one-
way if statements , two-way if-else statements,
multi-way if statements and switch statements.
● how write expressions using the conditional
expression.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
Outline
● Short Hand If...Else
● Compound Conditions
● Common Errors
● The & and | Operators
● Multiple Alternative if Statements
● switch Statements
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
Objectives
● To declare boolean variables and write Boolean expressions using
relational operators .
● To implement selection control using one-way if statements .
● To implement selection control using two-way if-else statements .
● To implement selection control using nested if and multi-way if
statements .
● To avoid common errors and pitfalls in if statements .
● To combine conditions using logical operators (!, &&, and ||).
● To implement selection control using switch statements .
● To write expressions using the conditional expression .
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
Short Hand If...Else
(Ternary Operator)
It can be used to replace multiple lines of code with a single line.
int time = 20;
if (time < 18) System.out.println("Good day.");
else System.out.println("Good evening.");
Can be written as:
int time = 20;
String result = (time < 18) ? "Good day." : "Good evening.";
System.out.println(result);
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
5
Example(1)
int mark = 90;
if (mark >= 60) System.out.println("Pass");
else System.out.println("Fail");
Can be written as:
int mark = 90;
String result = (mark >= 60) ? "Pass" : "Fail";
System.out.println(result);
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
6
Example(2)
int x = 10; int y = 5;
if (x >= y) System.out.println(x);
else System.out.println(y);
Can be written as:
int x = 10; int y = 5;
int max = (x >= y) ? x : y;
System.out.println(max);
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
7
Example(3)
int x = 100;
if (x == 10) System.out.println(true);
else System.out.println(false);
Can be written as:
int x = 100;
boolean result = (x == 10) ? true : false;
System.out.println(result);
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
8
Compound Conditions
A condition may be composite or compound: contains two
sub-conditions:
how to express the following condition:
If (mark >=60) and (mark<=100) output “Pass”?
In this case we will use: logical operators
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
9
Compound Conditions
Write a program that checks whether a number is divisible by
both 2 and 3? For You Info: any number that is divisible by 2 and 3, then it is divisible by 6
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
10
import java.util.Scanner;
public class Test {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println( "Input a number:");
int x = input.nextInt();
if ((x % 2== 0) && (x %3==0))
System.out.println( x + ": is divisible by both 2 and 3 ");
else
System.out.println( x + ": is not divisible by both 2 and 3 ");
}
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
11
Compound Conditions
A condition may be composite or compound: contains two sub-
conditions:
how to express the following condition:
int mark = 75;
if ((mark >=60) && (mark<=100) )
System.out.println("Pass");
In this case we will use: logical operators
char gender = 'f';
if ((gender == 'f') || (gender == 'm') )
System.out.println("Correct");
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
12
Common Errors
Try removing the parenthesis in the compound condition that
follows to be as:
(mark >=60) && (mark<=100)
instead of
( (mark >=60) && (mark<=100) )
(gender == 'f') || (gender == 'm')
( (gender == 'f') || (gender == 'm') )
(x % 2== 0) && (x %3==0)
instead of
( (x % 2== 0) && (x %3==0) )
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
13
The & and | Operators
&&, || known as short circuit evaluation
& , | known as long circuit evaluation
If x is 1, what is x after this
expression?
(x > 1) & (x++ < 10)
If x is 1, what is x after this
expression?
(1 > x) && ( 1 > x++)
How about (1 == x) | (10 > x++)?
(1 == x) || (10 > x++)?
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
14
The & and | Operators
&&, || known as short circuit evaluation
& , | known as long circuit evaluation
A short circuit happens because the result is clear even
before the complete evaluation of the expression
Short circuit evaluation avoids unnecessary work and
leads to efficient processing
long circuit evaluation is exactly opposite. It continues
unnecessary work even if result is clear
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
15
Multiple Alternative if Statements
Multiple Alternative if Statements known as Nested Ifs:
Nested if statement is another if (or ifs) but it appears in:
then-clause of the first if statement
if (condition 1) s1 will be executed if c1 and c2 and c3 are all true
if (condition 2) s2 will be executed if c1 and c2 are true but c3 is
if (condition 3) s1; false
else s2; s3 will be executed if c1 is true and c2 is false
else s3;
s4 will be executed if c3 is false
else s4;
Note that else is attributed to last if (which is not
being closed by another else)
then-clause is an if statement
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
16
The else if Statement
Use the else if statement to specify a new condition if
the first condition is false.
Syntax
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and
condition2 is true
} else {
// block of code to be executed if the condition1 is false and
condition2 is false
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
17
Multiple Alternative if Statements
Multiple Alternative if Statements known as Nested Ifs:
Nested if statement is another if (or ifs) but it appears in:
else-clause of the first if statement
s1 will be executed if c1 is true
s2 will be executed if c1 is false and c2 is true
if (condition 1) s1; s3 will be executed if both c1 and c2 are false but c3
else if (condition 2) s2; is true
else if (condition 3) s3;
else s4; s4 will be executed if all conditions are false
else-clause is an if statement
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
18
Note: else-clause is an if statement
The else clause matches the most recent if clause in the
same block.
int i = 1, j = 2, k = 3; Equivalent int i = 1, j = 2, k = 3;
if (i > j) if (i > j)
if (i > k) if (i > k)
System.out.println("A"); System.out.println("A");
This is better
else with correct
else
System.out.println("B"); indentation System.out.println("B" );
(a) (b)
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
19
Trace this example
if (score >= 90.0) if (score >= 90.0)
System.out.print("A"); System.out.print("A");
else Equivalent else if (score >= 80.0)
if (score >= 80.0) System.out.print("B");
System.out.print("B"); else if (score >= 70.0)
else System.out.print("C");
if (score >= 70.0) else if (score >= 60.0)
System.out.print("C"); System.out.print("D");
else else
if (score >= 60.0) System.out.print("F");
System.out.print("D");
This is better
else
System.out.print("F");
(b)
(a)
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
20
animation
Trace if-else statement
Suppose score is 70.0 The condition is false
if (score >= 90.0)
System.out.print("A");
else if (score >= 80.0)
System.out.print("B");
else if (score >= 70.0)
System.out.print("C");
else if (score >= 60.0)
System.out.print("D");
else
System.out.print("F");
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
21
animation
Trace if-else statement
Suppose score is 70.0 The condition is true
if (score >= 90.0)
System.out.print("A");
else if (score >= 80.0)
System.out.print("B");
else if (score >= 70.0)
System.out.print("C");
else if (score >= 60.0)
System.out.print("D");
else
System.out.print("F");
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
22
Note, cont.
Nothing is printed from the preceding statement. To force
the else clause to match the first if clause, you must add a
pair of braces:
int i = 1;
int j = 2;
int k = 3;
if (i > j) {
if (i > k)
System.out.println("A");
}
else
System.out.println("B");
This statement prints B.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
23
Note, cont.
Given this piece of code:
int i=10;
if (i == 1)System.out.println(“1");
else if (i == 2)System.out.println(“2");
else if (i == 3)System.out.println(“3");
else System.out.println(“greater than 3");
Whenever the variable that we check (i in the exmaple)
appears in all ifs, the comparison operators are equal
(==) and the check against constants of the same type
(int), then we can use switch case to select one of many
code blocks to be executed.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
24
switch Statements
int day = 7;
switch (day) {
case 6: System.out.println("Today is Saturday"); break;
case 7: System.out.println("Today is Sunday"); break;
default: System.out.println("Looking forward to the Weekend");
}
// Outputs: Today is Sunday
The variable to be tested should take a value before the switch
begins check.
The variable to be tested can be of type: char, byte, short, int or
String
It is not important to sort cases values. Any order is correct
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
25
switch Statements
String name = "Ali";
switch(name) {
case "Ali": System.out.println("First Name"); break;
case "Nasir": System.out.println("Middle Name"); break;
default: System.out.println("not in our list");
} // both deault and its break are optional (depending on Prob.)
For the default: it is not important to be at the end. But in
this case, you have to add: break
switch(name) {
case "Ali": System.out.println("First Name"); break;
default: System.out.println("not in our list"); break;
case "Nasir": System.out.println("Middle Name"); break;
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
26
animation
Trace switch statement
Suppose day is 2:
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 0:
case 6: System.out.println("Weekend");
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
27
Summary
● The short-hand if...else statement, allows replacing multiple lines of code
with a single line.
● Compound conditions involve combining multiple sub-conditions using
logical operators like && (AND) and || (OR).
● Nested if statements and else if statements are used to create multiple
alternative conditions and provide additional conditions to test when the
previous conditions are false.
● The switch statement in Java provides a way to select one of many code
blocks to be executed based on the value of a variable.
● The variable being tested in the switch statement can be of type char,
byte, short, int, or String.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
References
● Y. Daniel Liang, 2019, Intro to Java Programming, Comprehensive
Version, Student Value Edition 12th Edition. Pearson, ISBN-10 :
0136520154 ISBN-13: 978-0136520153.
● Introduction to Java, https://www.w3schools.com/java/java_intro.asp
, Last Updated 2024, Last Accessed March 2024
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
29