Slides 3 - If Statements, Relations, Logical Operators
Slides 3 - If Statements, Relations, Logical Operators
1. Sequence:
• Unless specified otherwise, the order of statement execution is linear/sequential
• one statement after the other, in sequence
2. Conditional statements:
• a statement may or may not be executed depending on some condition
n 2
Contents
1. The if statement
2. The if-else statement
3. Relations Operators
4. Logical operators
n 3
Conditional statements
n 4
Flowchart
Assignment Start/end
Decision Connector
n 5
1- The if statement
§ syntax:
The condition is a boolean expression.
if is a Java (evaluates to either true or false)
reserved word
if ( condition )
statement;
n 6
Logic of an if statement
condition
evaluated
true false
statement
n 7
Example: Age.java
n 8
Example: Age.java
Output
n 9
2- The if-else statement
§ An else clause can be added to an if statement to
make an if-else statement
if ( condition )
statement1;
else
statement2;
n 10
Logic of an if-else statement
condition
evaluated
true false
statement1 statement2
n 11
Example: Wages.java
final double RATE = 10.0; // regular pay rate
final int STANDARD = 40; // standard hours
double pay = 0;
System.out.print("Number of hours worked: ");
int hours = myKeyboard.nextInt();
n 12
What is output?
int speed = 25;
else
Output
n 13
What is output?
n 14
What is output?
A. line A
int num = 4;
line B
if (num > 5) line C
System.out.println("line A"); line D
else B. line A
line C
System.out.println("line B"); line D
if (num < 10) C. line B
System.out.println("line C"); line C
System.out.println("line D"); line D
D. line B
line C
See how much harder it is to read if not indented
properly … E. line B
line D
n 15
What is output?
n 16
What is output?
== equal to
!= not equal to
< less than
> greater than
<= less than or equal to
>= greater than or equal to
if (age == 18)
System.out.println("you are 18");
else
System.out.println("you are not 18");
Output
if (age = 18)
System.out.println("you are 18");
else
System.out.println("you are not 18");
Output
n 19
A note on comparing floats
n 20
Part of the Unicode Character Set (ASCII)
n 21
A note on comparing characters
n 22
A note on comparing strings
• ex: firstString.equals(secondString)
• returns a boolean:
o true if firstString has the same content as
secondString
o false otherwise
n 23
A note on comparing strings
n 24
Example 1 StringComparison.java
if (s1.equals(s2))
System.out.println("The two lines are equal.");
else
System.out.println("The two lines are not equal.");
if (s2.equals(s1))
System.out.println("The two lines are equal.");
else
System.out.println("The two lines are not equal.");
n 25
Example 1 StringComparison.java
…
if (s1.equalsIgnoreCase(s2))
System.out.println("But the lines are equal, "
+ " ignoring case.");
else
System.out.println("Lines are not equal, "
+ " even ignoring case.");
n 26
Example 2
Syntax Output?
Error?
System.out.println("aBcD".equals("abcd"));
n 27
Example 2 …
Syntax Output?
Error?
System.out.println("aBcD".equalsIgnoreCase("aBcD"));
System.out.println("aBcD".compareTo("aBcD"));
System.out.println("aBcD".compareTo("aBcC"));
System.out.println("abc".compareTo("ab"));
System.out.println("abc".compareTo("abcd"));
n 28
4- Logical operators
false
n 29
4- Logical operators
a b a && b
true true
true false
false true
false false
n 30
4- Logical operators
a b a || b
true true
true false
false true
false false
n 31
Just checking …
When using a compound Boolean expression joined by an && (AND) in
an if statement:
A. p && p
B. p || p
C. p && !p
D. p || !p
E. b and d above
n 33
Precedence and Associativity Rules
n 34
Precedence and Associativity Rules
int a = 10;
int b = ++a++;
Which is the correct option?
A. a = 12 and b = 12
B. b = 12 and b = 11
C. Compiler error
D. Syntax error
n 36
Logical operators
Precedence:
q all logical operators have lower precedence than the relational
or arithmetic operators
q logical NOT has higher precedence than logical AND and logical
OR
n 37
Short-circuit evaluation
int j = -1;
if ((j > 0) && (1/(j+1) > 10))
System.out.println(j);
n 39
Complete evaluation
§ Rarely used …
n 40