SlideShare a Scribd company logo
CSE110
Principles of Programming
with Java
Lecture 07:
Conditional Statements
Javier Gonzalez-Sanchez
javiergs@asu.edu
javiergs.engineering.asu.edu
Office Hours: By appointment
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
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.
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 4
Topics
class
global
variables
methods statements
instructions
local
variables
conditional
Statements
loop
Statements
Conditional Statements
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 ?
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 7
if statement
• The if statement has the following syntax:
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);
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 9
Flow Chart of an if statement
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 (=)
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
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 12
Flow chart of an if-else statement
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
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!");
}
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!");
}
}
}
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
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 17
Block Statements
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)
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
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
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
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
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”);
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 24
Reference
Textbook – Section 3.1, 3.2, and 3.3
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.

More Related Content

PDF
201707 CSE110 Lecture 20
PDF
201707 CSE110 Lecture 08
PDF
201707 CSE110 Lecture 10
PDF
201707 CSE110 Lecture 12
PDF
201707 CSE110 Lecture 04
PDF
201707 CSE110 Lecture 11
PDF
201707 CSE110 Lecture 05
PPTX
Chapter 6 - More conditionals and loops
201707 CSE110 Lecture 20
201707 CSE110 Lecture 08
201707 CSE110 Lecture 10
201707 CSE110 Lecture 12
201707 CSE110 Lecture 04
201707 CSE110 Lecture 11
201707 CSE110 Lecture 05
Chapter 6 - More conditionals and loops

Similar to 201707 CSE110 Lecture 07 (20)

PDF
201707 CSE110 Lecture 09
PPTX
Lewis_Cocking_AP_Decision_Making_For_Coding
PPTX
Lecture 2
PPT
slides03.ppt
PDF
PPTX
Ch05-converted.pptx
PPSX
Bsit1
PPTX
05. Conditional Statements
PPTX
Decision Control Structure If & Else
PDF
201707 CSE110 Lecture 23
PPT
05 Conditional statements
PDF
201707 CSE110 Lecture 03
PPT
03a control structures
PDF
201801 CSE240 Lecture 03
PPTX
Dti2143 chap 4 control structures aka_selection
PPTX
Dti2143 chap 4 control structures aka_selection
PPT
Eo gaddis java_chapter_04_5e
PPT
Eo gaddis java_chapter_04_5e
PDF
201801 CSE240 Lecture 06
PPTX
JavaScript Session 2
201707 CSE110 Lecture 09
Lewis_Cocking_AP_Decision_Making_For_Coding
Lecture 2
slides03.ppt
Ch05-converted.pptx
Bsit1
05. Conditional Statements
Decision Control Structure If & Else
201707 CSE110 Lecture 23
05 Conditional statements
201707 CSE110 Lecture 03
03a control structures
201801 CSE240 Lecture 03
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
Eo gaddis java_chapter_04_5e
Eo gaddis java_chapter_04_5e
201801 CSE240 Lecture 06
JavaScript Session 2
Ad

More from Javier Gonzalez-Sanchez (20)

PDF
201804 SER332 Lecture 01
PDF
201801 SER332 Lecture 03
PDF
201801 SER332 Lecture 04
PDF
201801 SER332 Lecture 02
PDF
201801 CSE240 Lecture 26
PDF
201801 CSE240 Lecture 25
PDF
201801 CSE240 Lecture 24
PDF
201801 CSE240 Lecture 23
PDF
201801 CSE240 Lecture 22
PDF
201801 CSE240 Lecture 21
PDF
201801 CSE240 Lecture 20
PDF
201801 CSE240 Lecture 19
PDF
201801 CSE240 Lecture 18
PDF
201801 CSE240 Lecture 17
PDF
201801 CSE240 Lecture 16
PDF
201801 CSE240 Lecture 15
PDF
201801 CSE240 Lecture 14
PDF
201801 CSE240 Lecture 13
PDF
201801 CSE240 Lecture 12
PDF
201801 CSE240 Lecture 11
201804 SER332 Lecture 01
201801 SER332 Lecture 03
201801 SER332 Lecture 04
201801 SER332 Lecture 02
201801 CSE240 Lecture 26
201801 CSE240 Lecture 25
201801 CSE240 Lecture 24
201801 CSE240 Lecture 23
201801 CSE240 Lecture 22
201801 CSE240 Lecture 21
201801 CSE240 Lecture 20
201801 CSE240 Lecture 19
201801 CSE240 Lecture 18
201801 CSE240 Lecture 17
201801 CSE240 Lecture 16
201801 CSE240 Lecture 15
201801 CSE240 Lecture 14
201801 CSE240 Lecture 13
201801 CSE240 Lecture 12
201801 CSE240 Lecture 11
Ad

Recently uploaded (20)

PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
Spectroscopy.pptx food analysis technology
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Empathic Computing: Creating Shared Understanding
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Approach and Philosophy of On baking technology
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
sap open course for s4hana steps from ECC to s4
Spectroscopy.pptx food analysis technology
Machine learning based COVID-19 study performance prediction
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Dropbox Q2 2025 Financial Results & Investor Presentation
Empathic Computing: Creating Shared Understanding
Network Security Unit 5.pdf for BCA BBA.
MYSQL Presentation for SQL database connectivity
Encapsulation_ Review paper, used for researhc scholars
Programs and apps: productivity, graphics, security and other tools
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Advanced methodologies resolving dimensionality complications for autism neur...
Unlocking AI with Model Context Protocol (MCP)
Reach Out and Touch Someone: Haptics and Empathic Computing
Approach and Philosophy of On baking technology
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Chapter 3 Spatial Domain Image Processing.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
The AUB Centre for AI in Media Proposal.docx

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);
  • 9. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 9 Flow Chart of an if statement
  • 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
  • 12. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 12 Flow chart of an if-else statement
  • 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
  • 17. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 17 Block Statements
  • 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”);
  • 24. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 24 Reference Textbook – Section 3.1, 3.2, and 3.3
  • 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.