Lecture 5 Ed
Lecture 5 Ed
Chapter 4
Conditional Execution
• Example:
double gpa = console.nextDouble();
if (gpa >= 2.0) {
System.out.println("Application accepted.");
}
The if/else statement
Executes one block if a test is true, another if false
if (test) {
statement(s);
} else {
statement(s);
}
• Example:
double gpa = console.nextDouble();
if (gpa >= 2.0) {
System.out.println("Welcome to Mars University!");
} else {
System.out.println("Application denied.");
}
Relational expressions
• if statements and for loops both use logical tests.
for (int i = 1; i <= 10; i++) { ...
if (i <= 10) { ...
• These are boolean expressions, seen in Ch. 5.
Operator Meaning Example Value
• Tests use relational
== operators:
equals 1 + 1 == 2 true
!= does not equal 3.2 != 2.5 true
< less than 10 < 5 false
> greater than 10 > 5 true
<= less than or equal to 126 <= 100 false
>= greater than or equal to 5.0 >= 5.0 true
Misuse of if
• What's wrong with the following code?
Scanner console = new Scanner(System.in);
System.out.print("What percentage did you earn? ");
int percent = console.nextInt();
if (percent >= 90) {
System.out.println("You got an A!");
}
if (percent >= 80) {
System.out.println("You got a B!");
}
if (percent >= 70) {
System.out.println("You got a C!");
}
if (percent >= 60) {
System.out.println("You got a D!");
}
if (percent < 60) {
System.out.println("You got an F!");
}
...
Nested if/else
Chooses between outcomes using many tests
if (test) {
statement(s);
} else if (test) {
statement(s);
} else {
statement(s);
}
• Example:
if (x > 0) {
System.out.println("Positive");
} else if (x < 0) {
System.out.println("Negative");
} else {
System.out.println("Zero");
}
Nested if/else/if
• If it ends with else, exactly one path must be taken.
• If it ends with if, the code might not execute any path.
if (test) {
statement(s);
} else if (test) {
statement(s);
} else if (test) {
statement(s);
}
• Example:
if (place == 1) {
System.out.println("Gold medal!");
} else if (place == 2) {
System.out.println("Silver medal!");
} else if (place == 3) {
System.out.println("Bronze medal.");
}
Nested if structures
• exactly 1 path (mutually exclusive) • 0 or 1 path (mutually exclusive)
if (test) { if (test) {
statement(s); statement(s);
} else if (test) { } else if (test) {
statement(s); statement(s);
} else { } else if (test) {
statement(s); statement(s);
} }
• Whether you made the dean's list (GPA ≥ 3.8) or honor roll (3.5-3.8).
• (3) nested if / else if
p q p && q p || q p !p
• "Truth tables"
truefortrue
each,true
used with
true logical valuestrue
p andfalse
q:
true false false true false true
false true false true
false false false false
Evaluating logic expressions
• Relational operators have lower precedence than math.
5 * 7 >= 3 + 5 * (7 - 1)
5 * 7 >= 3 + 5 * 6
35 >= 3 + 30
35 >= 33
true
• Exercise: Write a program that prompts for information about a person and uses
it to decide whether to befriend them.
Factoring if/else code
• factoring: Extracting common/redundant code.
• Can reduce or eliminate redundancy from if/else code.
• Example:
if (a == 1) {
System.out.println(a);
x = 3;
b = b + x; System.out.println(a);
} else if (a == 2) { x = 3 * a;
System.out.println(a); if (a == 2) {
x = 6;
y = y + 10; y = y + 10;
b = b + x; }
} else { // a == 3 b = b + x;
System.out.println(a);
x = 9;
b = b + x;
}