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) {! } else {! }!!System.out.println( Error: y == 0 );!!System.exit(-1);!//Stop this program!!!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; public boolean getgooddog() {!!return gooddog;! }!!//Instance variable!
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! );! }! }!
Invoking (calling) a void method!dog katy = new Dog( Katy );!!katy.train();!
Review: Invoking (calling) static methods!!double x = Math.sqrt(3.14);!System.out.println(x);!!//Returns double! //void!