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

Similar to 201707 CSE110 Lecture 07 (20)

201707 CSE110 Lecture 09
201707 CSE110 Lecture 09   201707 CSE110 Lecture 09
201707 CSE110 Lecture 09
Javier Gonzalez-Sanchez
 
Lewis_Cocking_AP_Decision_Making_For_Coding
Lewis_Cocking_AP_Decision_Making_For_CodingLewis_Cocking_AP_Decision_Making_For_Coding
Lewis_Cocking_AP_Decision_Making_For_Coding
GeorgeTsak
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
Mohammed Khan
 
slides03.ppt
slides03.pptslides03.ppt
slides03.ppt
Anjali127411
 
Ch05.pdf
Ch05.pdfCh05.pdf
Ch05.pdf
ShivamChaturvedi67
 
Ch05-converted.pptx
Ch05-converted.pptxCh05-converted.pptx
Ch05-converted.pptx
ShivamChaturvedi67
 
Bsit1
Bsit1Bsit1
Bsit1
jigeno
 
05. Conditional Statements
05. Conditional Statements05. Conditional Statements
05. Conditional Statements
Intro C# Book
 
Decision Control Structure If & Else
Decision Control Structure If & ElseDecision Control Structure If & Else
Decision Control Structure If & Else
Abdullah Bhojani
 
201707 CSE110 Lecture 23
201707 CSE110 Lecture 23   201707 CSE110 Lecture 23
201707 CSE110 Lecture 23
Javier Gonzalez-Sanchez
 
05 Conditional statements
05 Conditional statements05 Conditional statements
05 Conditional statements
maznabili
 
03a control structures
03a   control structures03a   control structures
03a control structures
Manzoor ALam
 
201801 CSE240 Lecture 03
201801 CSE240 Lecture 03201801 CSE240 Lecture 03
201801 CSE240 Lecture 03
Javier Gonzalez-Sanchez
 
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
alish sha
 
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
alish sha
 
Eo gaddis java_chapter_04_5e
Eo gaddis java_chapter_04_5eEo gaddis java_chapter_04_5e
Eo gaddis java_chapter_04_5e
Gina Bullock
 
Eo gaddis java_chapter_04_5e
Eo gaddis java_chapter_04_5eEo gaddis java_chapter_04_5e
Eo gaddis java_chapter_04_5e
Gina Bullock
 
201801 CSE240 Lecture 06
201801 CSE240 Lecture 06201801 CSE240 Lecture 06
201801 CSE240 Lecture 06
Javier Gonzalez-Sanchez
 
JavaScript Session 2
JavaScript Session 2JavaScript Session 2
JavaScript Session 2
Muhammad Ehtisham Siddiqui
 
201506 CSE340 Lecture 18
201506 CSE340 Lecture 18201506 CSE340 Lecture 18
201506 CSE340 Lecture 18
Javier Gonzalez-Sanchez
 

More from Javier Gonzalez-Sanchez (20)

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

Recently uploaded (20)

MULTI-STAKEHOLDER CONSULTATION PROGRAM On Implementation of DNF 2.0 and Way F...
MULTI-STAKEHOLDER CONSULTATION PROGRAM On Implementation of DNF 2.0 and Way F...MULTI-STAKEHOLDER CONSULTATION PROGRAM On Implementation of DNF 2.0 and Way F...
MULTI-STAKEHOLDER CONSULTATION PROGRAM On Implementation of DNF 2.0 and Way F...
ICT Frame Magazine Pvt. Ltd.
 
Best 10 Free AI Character Chat Platforms
Best 10 Free AI Character Chat PlatformsBest 10 Free AI Character Chat Platforms
Best 10 Free AI Character Chat Platforms
Soulmaite
 
Google DeepMind’s New AI Coding Agent AlphaEvolve.pdf
Google DeepMind’s New AI Coding Agent AlphaEvolve.pdfGoogle DeepMind’s New AI Coding Agent AlphaEvolve.pdf
Google DeepMind’s New AI Coding Agent AlphaEvolve.pdf
derrickjswork
 
In-App Guidance_ Save Enterprises Millions in Training & IT Costs.pptx
In-App Guidance_ Save Enterprises Millions in Training & IT Costs.pptxIn-App Guidance_ Save Enterprises Millions in Training & IT Costs.pptx
In-App Guidance_ Save Enterprises Millions in Training & IT Costs.pptx
aptyai
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
AI and Gender: Decoding the Sociological Impact
AI and Gender: Decoding the Sociological ImpactAI and Gender: Decoding the Sociological Impact
AI and Gender: Decoding the Sociological Impact
SaikatBasu37
 
Longitudinal Benchmark: A Real-World UX Case Study in Onboarding by Linda Bor...
Longitudinal Benchmark: A Real-World UX Case Study in Onboarding by Linda Bor...Longitudinal Benchmark: A Real-World UX Case Study in Onboarding by Linda Bor...
Longitudinal Benchmark: A Real-World UX Case Study in Onboarding by Linda Bor...
UXPA Boston
 
Mastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B LandscapeMastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B Landscape
marketing943205
 
Who's choice? Making decisions with and about Artificial Intelligence, Keele ...
Who's choice? Making decisions with and about Artificial Intelligence, Keele ...Who's choice? Making decisions with and about Artificial Intelligence, Keele ...
Who's choice? Making decisions with and about Artificial Intelligence, Keele ...
Alan Dix
 
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
SOFTTECHHUB
 
Middle East and Africa Cybersecurity Market Trends and Growth Analysis
Middle East and Africa Cybersecurity Market Trends and Growth Analysis Middle East and Africa Cybersecurity Market Trends and Growth Analysis
Middle East and Africa Cybersecurity Market Trends and Growth Analysis
Preeti Jha
 
Top 5 Qualities to Look for in Salesforce Partners in 2025
Top 5 Qualities to Look for in Salesforce Partners in 2025Top 5 Qualities to Look for in Salesforce Partners in 2025
Top 5 Qualities to Look for in Salesforce Partners in 2025
Damco Salesforce Services
 
Slack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teamsSlack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teams
Nacho Cougil
 
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdfICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
Eryk Budi Pratama
 
accessibility Considerations during Design by Rick Blair, Schneider Electric
accessibility Considerations during Design by Rick Blair, Schneider Electricaccessibility Considerations during Design by Rick Blair, Schneider Electric
accessibility Considerations during Design by Rick Blair, Schneider Electric
UXPA Boston
 
How Top Companies Benefit from Outsourcing
How Top Companies Benefit from OutsourcingHow Top Companies Benefit from Outsourcing
How Top Companies Benefit from Outsourcing
Nascenture
 
Refactoring meta-rauc-community: Cleaner Code, Better Maintenance, More Machines
Refactoring meta-rauc-community: Cleaner Code, Better Maintenance, More MachinesRefactoring meta-rauc-community: Cleaner Code, Better Maintenance, More Machines
Refactoring meta-rauc-community: Cleaner Code, Better Maintenance, More Machines
Leon Anavi
 
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Christian Folini
 
Right to liberty and security of a person.pdf
Right to liberty and security of a person.pdfRight to liberty and security of a person.pdf
Right to liberty and security of a person.pdf
danielbraico197
 
Understanding SEO in the Age of AI.pdf
Understanding SEO in the Age of AI.pdfUnderstanding SEO in the Age of AI.pdf
Understanding SEO in the Age of AI.pdf
Fulcrum Concepts, LLC
 
MULTI-STAKEHOLDER CONSULTATION PROGRAM On Implementation of DNF 2.0 and Way F...
MULTI-STAKEHOLDER CONSULTATION PROGRAM On Implementation of DNF 2.0 and Way F...MULTI-STAKEHOLDER CONSULTATION PROGRAM On Implementation of DNF 2.0 and Way F...
MULTI-STAKEHOLDER CONSULTATION PROGRAM On Implementation of DNF 2.0 and Way F...
ICT Frame Magazine Pvt. Ltd.
 
Best 10 Free AI Character Chat Platforms
Best 10 Free AI Character Chat PlatformsBest 10 Free AI Character Chat Platforms
Best 10 Free AI Character Chat Platforms
Soulmaite
 
Google DeepMind’s New AI Coding Agent AlphaEvolve.pdf
Google DeepMind’s New AI Coding Agent AlphaEvolve.pdfGoogle DeepMind’s New AI Coding Agent AlphaEvolve.pdf
Google DeepMind’s New AI Coding Agent AlphaEvolve.pdf
derrickjswork
 
In-App Guidance_ Save Enterprises Millions in Training & IT Costs.pptx
In-App Guidance_ Save Enterprises Millions in Training & IT Costs.pptxIn-App Guidance_ Save Enterprises Millions in Training & IT Costs.pptx
In-App Guidance_ Save Enterprises Millions in Training & IT Costs.pptx
aptyai
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
AI and Gender: Decoding the Sociological Impact
AI and Gender: Decoding the Sociological ImpactAI and Gender: Decoding the Sociological Impact
AI and Gender: Decoding the Sociological Impact
SaikatBasu37
 
Longitudinal Benchmark: A Real-World UX Case Study in Onboarding by Linda Bor...
Longitudinal Benchmark: A Real-World UX Case Study in Onboarding by Linda Bor...Longitudinal Benchmark: A Real-World UX Case Study in Onboarding by Linda Bor...
Longitudinal Benchmark: A Real-World UX Case Study in Onboarding by Linda Bor...
UXPA Boston
 
Mastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B LandscapeMastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B Landscape
marketing943205
 
Who's choice? Making decisions with and about Artificial Intelligence, Keele ...
Who's choice? Making decisions with and about Artificial Intelligence, Keele ...Who's choice? Making decisions with and about Artificial Intelligence, Keele ...
Who's choice? Making decisions with and about Artificial Intelligence, Keele ...
Alan Dix
 
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
SOFTTECHHUB
 
Middle East and Africa Cybersecurity Market Trends and Growth Analysis
Middle East and Africa Cybersecurity Market Trends and Growth Analysis Middle East and Africa Cybersecurity Market Trends and Growth Analysis
Middle East and Africa Cybersecurity Market Trends and Growth Analysis
Preeti Jha
 
Top 5 Qualities to Look for in Salesforce Partners in 2025
Top 5 Qualities to Look for in Salesforce Partners in 2025Top 5 Qualities to Look for in Salesforce Partners in 2025
Top 5 Qualities to Look for in Salesforce Partners in 2025
Damco Salesforce Services
 
Slack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teamsSlack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teams
Nacho Cougil
 
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdfICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
Eryk Budi Pratama
 
accessibility Considerations during Design by Rick Blair, Schneider Electric
accessibility Considerations during Design by Rick Blair, Schneider Electricaccessibility Considerations during Design by Rick Blair, Schneider Electric
accessibility Considerations during Design by Rick Blair, Schneider Electric
UXPA Boston
 
How Top Companies Benefit from Outsourcing
How Top Companies Benefit from OutsourcingHow Top Companies Benefit from Outsourcing
How Top Companies Benefit from Outsourcing
Nascenture
 
Refactoring meta-rauc-community: Cleaner Code, Better Maintenance, More Machines
Refactoring meta-rauc-community: Cleaner Code, Better Maintenance, More MachinesRefactoring meta-rauc-community: Cleaner Code, Better Maintenance, More Machines
Refactoring meta-rauc-community: Cleaner Code, Better Maintenance, More Machines
Leon Anavi
 
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Christian Folini
 
Right to liberty and security of a person.pdf
Right to liberty and security of a person.pdfRight to liberty and security of a person.pdf
Right to liberty and security of a person.pdf
danielbraico197
 
Understanding SEO in the Age of AI.pdf
Understanding SEO in the Age of AI.pdfUnderstanding SEO in the Age of AI.pdf
Understanding SEO in the Age of AI.pdf
Fulcrum Concepts, LLC
 

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.
  翻译: