Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
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
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”);
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"
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.