TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
201707 CSE110 Lecture 07
1. CSE110
Principles of Programming
with Java
Lecture 07:
Conditional Statements
Javier Gonzalez-Sanchez
javiergs@asu.edu
javiergs.engineering.asu.edu
Office Hours: By appointment
2. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 2
Flow of Control
Unless specified otherwise, the order of statement
execution through a method is linear: one statement after
the other in sequence (top down order).
public static void main (String [] args) {
System.out.println(“one”);
System.out.println(“two”);
System.out.println(“three”);
}
The order of statement execution is called the flow of control
3. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 3
Flow of Control
Some programming statements modify that order
(flow of control), allowing us to:
• decide whether or not to execute a particular
statement, or
• perform a statement over and over, repetitively
These decisions are based on a boolean expression
(also called a condition) that evaluates to true or
false.
4. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 4
Topics
class
global
variables
methods statements
instructions
local
variables
conditional
Statements
loop
Statements
6. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 6
Conditional Statements
• A conditional statement lets us choose which
statement will be executed next
• Java's conditional statements are
o the if statement
o the if-else statement
o the switch statement
o the operator ?
7. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 7
if statement
• The if statement has the following syntax:
8. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 8
Example
int MAX = 5;
int sum = 30;
int delta = 0;
if (sum > MAX) {
delta = sum - MAX;
}
System.out.println("The delta is " + delta);
10. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 10
Boolean expressions
A condition uses relational operators, which all return
boolean results:
== equal to
!= not equal to
< less than
> greater than
<= less than or equal to
>= greater than or equal to
Note the difference between the equality operator
(==) and the assignment operator (=)
11. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 11
if-else statement
An else clause can be added to an if statement to
make an if-else statement
if ( condition ) {
statement1;
} else {
statement2;
}
• If the condition is true, statement1 is executed; if the
condition is false, statement2 is executed
• One or the other will be executed, but not both
13. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 13
Nested if Statements
• The statement executed as a result of an if
statement or else clause could be another if
statement
• These are called nested if statements
• An else clause is matched to the last unmatched if
(no matter what the indentation implies)
• Braces can be used to specify the if statement to
which an else clause belongs
14. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 14
if-else if-else
You can also have multiple conditions to be verified:
if (temp > 100) {
System.out.println("It is hot!");
} else if (temp > 80) {
System.out.println("It is warm");
} else if (temp > 50) {
System.out.println("It is chilly");
} else {
System.out.println("It is cold!");
}
15. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 15
if-else if-else
The code from the previous page is equivalent to:
if (temp > 100) {
System.out.println("It is hot!");
else {
if (temp > 80) {
System.out.println("It is warm");
} else {
if (temp > 50){
System.out.println("It is chilly");
} else {
System.out.println("It is cold!");
}
}
}
16. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 16
Block Statements
• Several statements are grouped together into a
block statement
• A block is delimited by braces: {...}
• For example, in an if-else statement, the if portion,
or the else portion, or both, could be block
statements
• There is no need to use braces if there is only one
statement or one set of “if-else” within the outer “if”
statement
18. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 18
Logical Operators
• Boolean expressions can use the following logical
operators:
! Logical NOT
&& Logical AND
|| Logical OR
• They all take boolean operands and produce boolean
results
• Logical NOT is a unary operator (it operates on one
operand)
• Logical AND and logical OR are binary operators (each
operates on two operands)
19. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 19
Logical NOT
• The logical NOT operation is also called logical
negation or logical complement
• If some boolean condition a is true, then !a is false; if
a is false, then !a is true
• Logical expressions can be shown using truth tables
20. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 20
Logical AND and Logical OR
• The logical AND expression
a && b
is true if both a and b are true, and false otherwise
• The logical OR expression
a || b
is true if a or b or both are true, and false otherwise
21. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 21
Logical AND and Logical OR
• Since && and || each have two operands, there
are four possible combinations of conditions a and
b
22. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 22
Logical Operators
Conditions can use logical operators to form complex
expressions
if (total < MAX+5 && !found)
System.out.println ("Processing...");
Logical operators have precedence relationships among
themselves and with other operators
• The relational or arithmetic operators have higher
precedence than logical AND and logical OR
• logical NOT has higher precedence than logical AND.
Logical AND has higher precedence than logical OR
23. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 23
Example
int examGrade = 90;
int assignmentGrade = 80;
int quizGrade = 85;
if (examGrade > 85 && assignmentGrade > 85)
System.out.println(“Well done!”);
else if (quizGrade < 70 || assignmentGrade < 85)
System.out.println(“Houston, we have a problem”);
25. CSE110 - Principles of Programming
Javier Gonzalez-Sanchez
javiergs@asu.edu
Summer 2017
Disclaimer. These slides can only be used as study material for the class CSE110 at ASU. They cannot be distributed or used for another purpose.