SlideShare a Scribd company logo
1 of 25
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

Similar to 201707 CSE110 Lecture 07

Similar to 201707 CSE110 Lecture 07 (20)

201707 CSE110 Lecture 09
201707 CSE110 Lecture 09   201707 CSE110 Lecture 09
201707 CSE110 Lecture 09
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
slides03.ppt
slides03.pptslides03.ppt
slides03.ppt
 
Ch05.pdf
Ch05.pdfCh05.pdf
Ch05.pdf
 
Ch05-converted.pptx
Ch05-converted.pptxCh05-converted.pptx
Ch05-converted.pptx
 
Bsit1
Bsit1Bsit1
Bsit1
 
05. Conditional Statements
05. Conditional Statements05. Conditional Statements
05. Conditional Statements
 
Decision Control Structure If & Else
Decision Control Structure If & ElseDecision Control Structure If & Else
Decision Control Structure If & Else
 
201707 CSE110 Lecture 23
201707 CSE110 Lecture 23   201707 CSE110 Lecture 23
201707 CSE110 Lecture 23
 
05 Conditional statements
05 Conditional statements05 Conditional statements
05 Conditional statements
 
201707 CSE110 Lecture 03
201707 CSE110 Lecture 03  201707 CSE110 Lecture 03
201707 CSE110 Lecture 03
 
03a control structures
03a   control structures03a   control structures
03a control structures
 
201801 CSE240 Lecture 03
201801 CSE240 Lecture 03201801 CSE240 Lecture 03
201801 CSE240 Lecture 03
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
Eo gaddis java_chapter_04_5e
Eo gaddis java_chapter_04_5eEo gaddis java_chapter_04_5e
Eo gaddis java_chapter_04_5e
 
Eo gaddis java_chapter_04_5e
Eo gaddis java_chapter_04_5eEo gaddis java_chapter_04_5e
Eo gaddis java_chapter_04_5e
 
201801 CSE240 Lecture 06
201801 CSE240 Lecture 06201801 CSE240 Lecture 06
201801 CSE240 Lecture 06
 
JavaScript Session 2
JavaScript Session 2JavaScript Session 2
JavaScript Session 2
 
201506 CSE340 Lecture 18
201506 CSE340 Lecture 18201506 CSE340 Lecture 18
201506 CSE340 Lecture 18
 

More from Javier Gonzalez-Sanchez (20)

201804 SER332 Lecture 01
201804 SER332 Lecture 01201804 SER332 Lecture 01
201804 SER332 Lecture 01
 
201801 SER332 Lecture 03
201801 SER332 Lecture 03201801 SER332 Lecture 03
201801 SER332 Lecture 03
 
201801 SER332 Lecture 04
201801 SER332 Lecture 04201801 SER332 Lecture 04
201801 SER332 Lecture 04
 
201801 SER332 Lecture 02
201801 SER332 Lecture 02201801 SER332 Lecture 02
201801 SER332 Lecture 02
 
201801 CSE240 Lecture 26
201801 CSE240 Lecture 26201801 CSE240 Lecture 26
201801 CSE240 Lecture 26
 
201801 CSE240 Lecture 25
201801 CSE240 Lecture 25201801 CSE240 Lecture 25
201801 CSE240 Lecture 25
 
201801 CSE240 Lecture 24
201801 CSE240 Lecture 24201801 CSE240 Lecture 24
201801 CSE240 Lecture 24
 
201801 CSE240 Lecture 23
201801 CSE240 Lecture 23201801 CSE240 Lecture 23
201801 CSE240 Lecture 23
 
201801 CSE240 Lecture 22
201801 CSE240 Lecture 22201801 CSE240 Lecture 22
201801 CSE240 Lecture 22
 
201801 CSE240 Lecture 21
201801 CSE240 Lecture 21201801 CSE240 Lecture 21
201801 CSE240 Lecture 21
 
201801 CSE240 Lecture 20
201801 CSE240 Lecture 20201801 CSE240 Lecture 20
201801 CSE240 Lecture 20
 
201801 CSE240 Lecture 19
201801 CSE240 Lecture 19201801 CSE240 Lecture 19
201801 CSE240 Lecture 19
 
201801 CSE240 Lecture 18
201801 CSE240 Lecture 18201801 CSE240 Lecture 18
201801 CSE240 Lecture 18
 
201801 CSE240 Lecture 17
201801 CSE240 Lecture 17201801 CSE240 Lecture 17
201801 CSE240 Lecture 17
 
201801 CSE240 Lecture 16
201801 CSE240 Lecture 16201801 CSE240 Lecture 16
201801 CSE240 Lecture 16
 
201801 CSE240 Lecture 15
201801 CSE240 Lecture 15201801 CSE240 Lecture 15
201801 CSE240 Lecture 15
 
201801 CSE240 Lecture 14
201801 CSE240 Lecture 14201801 CSE240 Lecture 14
201801 CSE240 Lecture 14
 
201801 CSE240 Lecture 13
201801 CSE240 Lecture 13201801 CSE240 Lecture 13
201801 CSE240 Lecture 13
 
201801 CSE240 Lecture 12
201801 CSE240 Lecture 12201801 CSE240 Lecture 12
201801 CSE240 Lecture 12
 
201801 CSE240 Lecture 11
201801 CSE240 Lecture 11201801 CSE240 Lecture 11
201801 CSE240 Lecture 11
 

Recently uploaded

AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAnitaRaj43
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceIES VE
 
JavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuideJavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuidePixlogix Infotech
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...caitlingebhard1
 
Design and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data ScienceDesign and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data SciencePaolo Missier
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMKumar Satyam
 
Top 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTop 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTopCSSGallery
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxMarkSteadman7
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringWSO2
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard37
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxFIDO Alliance
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc
 

Recently uploaded (20)

AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational Performance
 
JavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuideJavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate Guide
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
 
Design and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data ScienceDesign and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data Science
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
Top 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTop 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development Companies
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptx
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software Engineering
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptx
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
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);
  • 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.