SlideShare a Scribd company logo
CSE110
Principles of Programming
with Java
Lecture 08:
Nested if-else Statements
Javier Gonzalez-Sanchez
javiergs@asu.edu
javiergs.engineering.asu.edu
Office Hours: By appointment
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 2
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 | 3
Block Statements
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 4
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 | 5
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 | 6
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 | 7
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 | 8
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 | 9
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 | 10
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 | 11
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 | 12
Example
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 13
Example
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 14
Full set of Operators
• Arithmetic
• Relational
• Logical (Boolean)
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 15
Comparing Characters
• We can use the relational operators on character
data
• The results are based on the Unicode character set
• The following condition is true because the
character + comes before the character J in the
Unicode character set:
if ('+' < 'J') System.out.println ("+ is less than J");
• The uppercase alphabet (A-Z) followed by the
lowercase alphabet (a-z) appear in alphabetical
order in the Unicode character set
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 16
Comparing Characters
• Comparing characters is based on a character set,
it is called a lexicographic ordering
• It uses Unicode of each character.
• Therefore it distinguishes upper and lower cases. i.e.,
“Apple” and “apple” are considered to be different.
(unicode for ‘a’ is 97, and unicode for ‘A’ is 65)
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 17
Comparing Strings
• Remember that a character string in Java is not a
primitive data type.
• We cannot use the relational operators to compare
strings
• Do NOT do this
String str1 = "apple";
String str2 = "banana";
if (str1 == str2){//we are not supposed to do this
}
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 18
Comparing Strings
String str1 = "apple";
String str2 = "banana";
//do NOT do this
if (str1 == str2){
}
// Instead we need to do:
if (str1.equals(str2)){
}
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 19
Comparing Strings
• The following does not also compare two strings:
String str1 = "apple";
String str2 = "banana";
//do NOT do this
if (str1 < str2){
}
// Instead we need to do:
if (str1.compareTo(str2)){
}
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 20
Example
int result = str1.compareTo(str2);
if (result < 0) {
//if str1 is smaller than str2
} else if (result > 0){
//if str1 is larger than str2
} else if (result == 0)
//if str1 is identical to str2
}
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 21
Comparing Strings
• Comparing strings is based on a character set, it is
called a lexicographic ordering
• It uses Unicode of each character in strings.
• Also, short strings come before longer strings with
the same prefix (lexicographically)
Therefore "book" comes before "bookcase"
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 22
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 07
PPTX
Java Chapter 05 - Conditions & Loops: part 2
PPTX
Java Chapter 05 - Conditions & Loops: part 4
PPT
Java Programmin: Selections
PPT
Ap Power Point Chpt3 B
PPTX
Lewis_Cocking_AP_Decision_Making_For_Coding
PPT
Java™ (OOP) - Chapter 3: "Selections"
PPT
Java-operators.ppt
201707 CSE110 Lecture 07
Java Chapter 05 - Conditions & Loops: part 2
Java Chapter 05 - Conditions & Loops: part 4
Java Programmin: Selections
Ap Power Point Chpt3 B
Lewis_Cocking_AP_Decision_Making_For_Coding
Java™ (OOP) - Chapter 3: "Selections"
Java-operators.ppt

Similar to 201707 CSE110 Lecture 08 (20)

PPT
Chaptfffffuuer05.PPT
PPT
Eo gaddis java_chapter_04_5e
PPT
Eo gaddis java_chapter_04_5e
PPTX
AP COmputer Science Review if and else condition
PPTX
Java Foundations: Basic Syntax, Conditions, Loops
PPT
Control structure and Looping statements
PDF
201707 CSE110 Lecture 04
PPT
Comp102 lec 5.1
PPTX
Programming fundamentals through javascript
PPT
PPTX
PPT ON JAVA AND UNDERSTANDING JAVA'S PRINCIPLES
PDF
Bootcamp - Team TEAL - Day 4
PPT
Ap Power Point Chpt3
PDF
Week 5
PDF
Week 5
PDF
Programming for Problem Solving
PPT
chap8-decisionstructuresinpython123 .ppt
PPTX
Programming note C#
PPTX
ICSE Class X Conditional Statements in java
Chaptfffffuuer05.PPT
Eo gaddis java_chapter_04_5e
Eo gaddis java_chapter_04_5e
AP COmputer Science Review if and else condition
Java Foundations: Basic Syntax, Conditions, Loops
Control structure and Looping statements
201707 CSE110 Lecture 04
Comp102 lec 5.1
Programming fundamentals through javascript
PPT ON JAVA AND UNDERSTANDING JAVA'S PRINCIPLES
Bootcamp - Team TEAL - Day 4
Ap Power Point Chpt3
Week 5
Week 5
Programming for Problem Solving
chap8-decisionstructuresinpython123 .ppt
Programming note C#
ICSE Class X Conditional Statements in java
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
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Understanding_Digital_Forensics_Presentation.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Machine learning based COVID-19 study performance prediction
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
sap open course for s4hana steps from ECC to s4
PPT
Teaching material agriculture food technology
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Cloud computing and distributed systems.
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Understanding_Digital_Forensics_Presentation.pptx
The AUB Centre for AI in Media Proposal.docx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Advanced methodologies resolving dimensionality complications for autism neur...
MIND Revenue Release Quarter 2 2025 Press Release
Machine learning based COVID-19 study performance prediction
“AI and Expert System Decision Support & Business Intelligence Systems”
sap open course for s4hana steps from ECC to s4
Teaching material agriculture food technology
Mobile App Security Testing_ A Comprehensive Guide.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Cloud computing and distributed systems.
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Spectral efficient network and resource selection model in 5G networks

201707 CSE110 Lecture 08

  • 1. CSE110 Principles of Programming with Java Lecture 08: Nested if-else Statements Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu Office Hours: By appointment
  • 2. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 2 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
  • 3. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 3 Block Statements
  • 4. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 4 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!"); } } }
  • 5. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 5 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!"); }
  • 6. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 6 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)
  • 7. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 7 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
  • 8. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 8 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
  • 9. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 9 Logical AND and Logical OR • Since && and || each have two operands, there are four possible combinations of conditions a and b
  • 10. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 10 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
  • 11. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 11 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”);
  • 12. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 12 Example
  • 13. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 13 Example
  • 14. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 14 Full set of Operators • Arithmetic • Relational • Logical (Boolean)
  • 15. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 15 Comparing Characters • We can use the relational operators on character data • The results are based on the Unicode character set • The following condition is true because the character + comes before the character J in the Unicode character set: if ('+' < 'J') System.out.println ("+ is less than J"); • The uppercase alphabet (A-Z) followed by the lowercase alphabet (a-z) appear in alphabetical order in the Unicode character set
  • 16. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 16 Comparing Characters • Comparing characters is based on a character set, it is called a lexicographic ordering • It uses Unicode of each character. • Therefore it distinguishes upper and lower cases. i.e., “Apple” and “apple” are considered to be different. (unicode for ‘a’ is 97, and unicode for ‘A’ is 65)
  • 17. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 17 Comparing Strings • Remember that a character string in Java is not a primitive data type. • We cannot use the relational operators to compare strings • Do NOT do this String str1 = "apple"; String str2 = "banana"; if (str1 == str2){//we are not supposed to do this }
  • 18. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 18 Comparing Strings String str1 = "apple"; String str2 = "banana"; //do NOT do this if (str1 == str2){ } // Instead we need to do: if (str1.equals(str2)){ }
  • 19. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 19 Comparing Strings • The following does not also compare two strings: String str1 = "apple"; String str2 = "banana"; //do NOT do this if (str1 < str2){ } // Instead we need to do: if (str1.compareTo(str2)){ }
  • 20. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 20 Example int result = str1.compareTo(str2); if (result < 0) { //if str1 is smaller than str2 } else if (result > 0){ //if str1 is larger than str2 } else if (result == 0) //if str1 is identical to str2 }
  • 21. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 21 Comparing Strings • Comparing strings is based on a character set, it is called a lexicographic ordering • It uses Unicode of each character in strings. • Also, short strings come before longer strings with the same prefix (lexicographically) Therefore "book" comes before "bookcase"
  • 22. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 22 Reference Textbook – Section 3.1, 3.2, and 3.3
  • 23. 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.