6 - Selections - Part 1
6 - Selections - Part 1
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.
⮚ Example
boolean isJavaFun = true;
boolean isFishTasty = false;
System.out.println(isJavaFun); // Outputs true
System.out.println(isFishTasty); // Outputs false
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(
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
Java Logical Operators
operator Name Description Example
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:
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
}
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;
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");
} }
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):
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
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
31