CompSci 125 Lecture 08
Chapter 5: Conditional Statements
Chapter 4: return Statement
Homework Update
§ HW3 Due 9/20
§ HW4 Due 9/27
§ Exam-1 10/2
Programming Assignment Update
§ p1: Traffic Applet due Sept 21 (Submit problem fixed)
§ p2: Find Parking due Oct 5
Boolean Expressions
Review: Variables of the boolean Primitive Data Type have values true or false!
boolean p;!
p = true;!
p = false;!
boolean q=false;!
p = q;!
A boolean Expression
Evaluates to true or false!
boolean p=true, q=false, x;!
x = !p; ! ! !//NOT p!
x = p || q; ! !//p OR q!
x = p && q; ! !//p AND q!
x = (p || q) && (!q);
§ Logical Operators AKA Conditional Operators
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html
Constructing a boolean Expression with Equality/Relational Operators!
int x=3, y=4;!
boolean p;!
p = (x<y);! ! !//Less Than!
p = (x<=y); ! !//Less Than or Equal!
p = (x==y); ! !//Equal!
p = (x!=y); ! !//Not equal!
p = (x>=y); ! !//Greater Than or Equal!
p = (x>y);! ! !//Greater Than!
Complex Boolean Expressions !
int x=3, y=4;!
boolean p;!
!
p = (x>=3) && (y!=0);
Advanced Topic:
“Short-Circuit” Evaluation of Expressions!
int x=3, y=0;!
boolean p;!
!
p = (y!=0) && ((x/y) > 1);!
!
§ If the left-hand side of the && evaluates to false
§ Then the right-hand side is never evaluated, avoiding the
division-by-zero
Avoid Reference Variables in boolean Expressions
(Unless You Really Want to Compare Memory Addresses)
import java.util.Scanner;!
public class MyClass {!
public static void main(String[] args) {!
!Scanner stdin = new Scanner(System.in);!
!String u = stdin.next();!
!String v = stdin.next();!
!boolean p = (u==v); !//Always false!!!!
}!
}!
How to compare String content
import java.util.Scanner;!
public class MyClass {!
public static void main(String[] args) {!
!Scanner stdin = new Scanner(System.in);!
!String u = stdin.next();!
!String v = stdin.next();!
!boolean p = u.equals(v); //Compares String content!
}!
}!
Decision-making with the if
Statement
The if Statement
§ So far… our programs have (well… mostly) executed statements sequentially in the order of their appearance in a method!
§ “Real” programs make decisions, executing different blocks of
statements depending upon the value of a boolean expression
Syntax of the if Statement
§ if (condition) Statement;!
!
§ Statement is executed only if condition evaluates true, otherwise it’s skipped
§ A condition is just a Boolean Expression used in a conditional
statement
Very Simple Example
boolean p;!
.!
. ! //Calculate a value for p!
.!
if (p) System.out.println(“p is true!”);!
.!
.!
.!
!
Example if Statements
int x = stdin.nextInt();!
int y = stdin.nextInt();!
if (x == y) System.out.println(“x equals y);!
if (x < y) System.out.println(“x less than y”);!
if (x > y) System.out.println(“x greater than y”);!
More if Examples
int x = stdin.nextInt();!
String s = “”;!
if (x < 0) s = “Negative”;!
if (x == 0) s = “Zero”;!
if (x > 0) s = “Positive”;!
System.out.printf(“x is %s\n”, s);!
The if Statement with a Code Block
int x = stdin.nextInt();!
int y = stdin.nextInt();!
if (x <= y) {!
!System.out.println(“x equals y);!
!System.out.printf(“x = %d\n”, x);!
!System.out.printf(“y = %d\n”, y);!
}!
!
§ Code block is a set of statements enclosed by braces
§ The entire code block is executed when condition is true
§ The entire code block is skipped when condition is false
Nested if Statements
.!
.!
.!
int x = stdin.nextInt();!
String s = stdin.next();!
int y = stdin.nextInt();!
int z = 0;!
if (y!=0) {!
!System.out.println(“y is non-zero”);!
!if (s.equals(“/”)) {!
! !z = x / y;!
!}!
}!
if … else
The if…else Statement
if (condition) statement1;
else statement2;
§ Condition is just a Boolean Expression
§ Statement1 executed only if condition evaluates true
§ Statement2 executed only if condition evaluates false
Code Blocks Work Fine
if (condition) { .
. //Multiple statements may appear here .
} else { .
. //And/or here .
}
§ You may use a code block for either or both statements
Example if…else Statement
if (y == 0) {!
!System.out.println(“Error: y == 0”);!
!System.exit(-1); !//Stop this program!!
} else {!
!double z = x/y;!
!System.out.printf(“z = %f\n”,z);!
}!
Nested if…else Statement
if (condition1)
statement1;
else
if (condition2) statement2;
§ Either statement may be another if statement
§ Statement1 executed only if condition1 is true
§ Statement2 executed only if condition1 is false and
condition2 is true
Beware the Dangling else !!!
if (condition1)
if (condition2) statement1;
else statement2;
§ Java always associates an else with the most recent if!
§ The else above is associated with the second if!
§ Thus, statement2 is executed only if condition1 is true and
condition2 is false!!!
Avoid the Dangling else by always using Code Blocks
if (condition1) {
if (condition2) statement1;
} else {
statement2;
}
§ Use code-blocks to avoid death by dangling else!
§ Here, statement2 is executed only if condition1 is false
§ The code blocks communicate your intent to Java and other
programmers reading your code
If you insist on using the dangling else, then code it neatly!
if (condition1)
if (condition2)
statement1;
else
statement2;
§ Use indentation to avoid confusing other programmers
§ This does what it looks like it might do… statement2 is
executed only if condition1 is true and condition2 is false
Dangling else Sometimes Appears on Exams
if (condition1)
if (condition2) statement1;
else {
statement2;
}
§ While you won’t code a dangling else…
§ You will encounter it written by other programmers
§ The else above is associated with the second if
§ This indentation is misleading!!! Java ignores the indentation!!!
The return Statement
A Method May Return a Value to its “Caller”
class Dog {!
private boolean goodDog; !//Instance variable!
!.!
!.!
!.!
public boolean getGoodDog() {!
!return goodDog;!
}!
}!
!
Caller’s May Use Returned Values in Expressions
!Dog katy = new Dog(“Katy”);!
!.!
!.!
!.!
!if (!katy.getGoodDog()) katy.train();!
!.!
!.!
!.!
A void Method Never Returns a Value
class Dog {!
!.!
!.!
!.!
public void speak() {!
!System.out.println(“Woof!”);!
}!
}!
The return Statement is Useful Even in void Methods
class Dog {!
boolean goodDog;!
!.!
!.!
!.!
public void bark() {!
!if (goodDog) {!
! System.out.println(“Wag more, bark less.”);!
return;!
!}!
!System.out.println(“Woof!”);!
}!
}!