Chapter 6 Selections
Part (1)
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
● The boolean Type and Operators.
• Relational Operators.
• Logical Operators.
● Boolean Expression.
● conditional statements.
● Common Errors
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.
The boolean Type and Operators
Often in a program you need to compare two
values, such as whether i is greater than j.
Java provides six comparison operators (also
known as relational operators) “will follow”
The result of the comparison is a Boolean value:
true or false.
boolean b = (1 > 2);
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
5
Boolean Values
⮚ A boolean type is declared with the boolean keyword
and can only take the values true or false.
⮚ Example
boolean isJavaFun = true;
boolean isFishTasty = false;
System.out.println(isJavaFun); // Outputs true
System.out.println(isFishTasty); // Outputs false
⮚ However, it is more common to return boolean values
from boolean expressions, for conditional testing
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
6
Relational Operators
Java Mathematics Name Example Result
Operator Symbol )radius is 5(
< < less than radius < 0 false
=< ≤ less than or equal to radius <= 0 false
> > greater than radius > 0 true
=> ≥ greater than or equal to radius >= 0 true
== = equal to radius == 0 false
=! ≠ not equal to radius != 0 True
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
Java Logical Operators
operator Name Description Example
Returns true if both statements are
&& Logical and x < 5 && x < 10
true
Returns true if one of the statements
|| Logical or x<5||x<4
is true
Reverse the result ,returns false if
! Logical not )x < 5 && x < 10(!
the result is true
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
8
Boolean Expression Example (1)
int x = 10;
int y = 9;
System.out.println(x > y); // returns true, because 10 is higher than 9
Or even easier:
Example
System.out.println(10 > 9); // returns true, because 10 is higher than
9
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
9
Boolean Expression Example (2)
⮚ In the examples below, we use the equal to (==) operator to
evaluate an expression:
⮚ Example
int x = 10;
System.out.println(x == 10);
// returns true, because the value of x is equal to 10
⮚ Example
System.out.println(10 == 15);
// returns false, because 10 is not equal to 15
⮚ Note: The Boolean value of an expression is the basis for all Java
comparisons and conditions.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
10
Boolean Expression Example (3)
⮚ In the examples below, we use the not equal to (!=) operator to
evaluate an expression:
⮚ Example
int x = 10;
System.out.println(x != 10);
// returns false, because the value of x is equal to 10
⮚ Example
System.out.println(10 != 15);
// returns true, because 10 is not equal to 15
Note: The Boolean value of an expression is the basis for all Java
comparisons and conditions.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
11
Condition (Boolean Expression)
So the condition (x > y) is a boolean-expression
which results in either true or false
We use conditions to perform different actions for
different decisions
Some actions will be executed when condition is
true and others when condition is false:
(x>y) : If the condition is true: do something here
If it is not true (false) : do another thing
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
12
Java conditional statements
Java has the following conditional statements:
•Use if to specify a block of code to be executed, if a
specified condition is true
•Use else to specify a block of code to be executed, if
the same condition is false
•Use else if to specify a new condition to test, if the
first condition is false
•Use switch to specify many alternative blocks of code
to be executed
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
13
The Branching Types
Therefore we have three main types of branching
(if statements):
-One-way if statement
-Two-way if statement
-Multiple Alternative if statements
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
14
One-way if Statements
Use the if statement to specify a block of Java code
to be executed if a condition is true.
Syntax:
if (condition) {
// block of code to be executed if the condition is
true
}
Note that if is in lowercase letters. Uppercase letters (If or IF) will
generate an error.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
15
One-way if Statements
if (score >= 60) {
if (boolean-expression) { System.out.println(“Success");
statement(s);
} }
This part:
{System.out.println(“Success");}
boolean - false Known as then-clause
expression
We first check condition:
- If the condition is true, we execute
true
then-clause.
Statement(s)
- If it is false we do nothing
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
16
One-way if Statements
if (boolean-expression) {
statement(s); if (score >= 60) {
} System.out.println(“Success");
}
Since we have only one
boolean - false statement, we can omit { }
expression
It is important to surround
condition with ( )
true
if (score >= 60)
Statement(s)
System.out.println(“Success");
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
17
int score = 59;
if (score >= 60) {
System.out.println(“Success");
}
System.out.println(“End the program");
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
18
(a)
Note (b)
if i > 0 { if (i > 0) {
System.out.println("i is positive"); System.out.println("i is positive");
} }
(a) Wrong (b) Correct
if (i > 0) { if (i > 0)
System.out.println("i is positive"); Equivalent System.out.println("i is positive");
}
(a) (b)
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
19
One-way if Statements
int x = 20;
int y = 18;
if (x > y) {
System.out.println("x is greater than y");
}
Output:
x is greater than y
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
20
One-way if Statements
int x = 20;
int y = 18;
if (x > y) {
System.out.println("x is greater than y");
}
System.out.println(“End the program");
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
21
One-way if Statements
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println( "Input a number:");
int x = input.nextInt();
if (x > 0)
System.out.println( x + ": is a positive number");
}
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
22
One-way if Statements
We can have many (one-way Statements):
public class Main {
public static void main(String[] args){
int m = 89;
if (m >= 60) System.out.println(“Pass");
if (m < 60) System.out.println(“Fail");
}
}
In this case we they each if has its own then-clause
BUT, Why writing two ifs like this? We will use:
two-way if Statement
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
23
The Two-way if Statement
Use the else statement to specify a block of code to
be executed if the condition is false.
Syntax
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
24
Condition (Boolean Expression)
If the condition is true, execute then-clause and
don’t execute else clause
If the condition is false, execute else-clause and
don’t execute then clause
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
25
if-else Example
if (mark>= 60) {
System.out.println("Pass");
}
else {
System.out.println(“Fail");
}
Note: if then-clause or else-clause
contains one statement, then no need
for: {}
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.
26
if-else :Possible scenarios
if (condition) if (condition) System.out.println(“----");
{ else
System.out.println(“----"); {
x=y; System.out.println(“-----");
} z= y;
else System.out.println(“-----"); }
if (condition) if (condition)
{ System.out.println(“----");
else
System.out.println(“----"); System.out.println(“----");
x= y;
}
else
{
System.out.println(“----");
z= y;
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
27
if-else Example
import java.util.Scanner;
public class Main {
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) System.out.println( x + ": is an even
number");
else System.out.println( x + ": is an odd number");
}
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
28
Common Errors
Adding a semicolon at the end of an if clause is a common
mistake.
if (mark>= 0); Wrong
{
mark = mark +1;
System.out.println(" Passs ");
}
This mistake is hard to find, because it is not a compilation error
or a runtime error, it is a logic error.
This error often occurs when you use the next-line block style.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
29
Summary
● The boolean type in Java can only take the values true or false and is used
for logical operations and conditional testing.
● Boolean variables can be declared and assigned using the boolean
keyword.
● Comparison operators in Java, also known as relational operators, are
used to compare two values and result in a boolean value.
● Boolean expressions return boolean values based on the evaluation of the
expression, and logical operators such as AND (&&), OR (||), and NOT
(!) can be used to combine boolean values.
● Conditions are used to perform different actions or make decisions in a
program.
● Java provides three main types of branching using if statements: one-way
if statement, two-way if statement, and multiple alternative if statements.
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.
31