7 - Selections - Part 2
7 - Selections - Part 2
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.");
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");
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);
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);
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:
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:
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
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
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
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
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");
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
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
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
29