Pull Lecture Materials and Open PollEv Poll Everywhere: pollev.com/comp110 Lecture 12 else-if and while loops Once in a while Fall 2016
if-then-else Statements General form of an if-then-else statement: if (<test condition>) { <then block - runs if test condition is true> else { <else block - runs if test condition is false> We re going to learn a handy special case of if-then-else
if s Optional Curly Brace Syntax When you do not use curly braces with an if, Java implicitly wraps curly braces around your SINGLE next statement: if (<condition A>) System.out.println( Hello ); else System.out.println( World ); Is equivalent to: if (<condition A>) { System.out.println( Hello ); else { System.out.println( World );
if s Optional Curly Brace Syntax Even though this can lead to terse code, it is dangerous and discouraged because it leads to mistakes A common innocent mistake: if (<condition A>); System.out.println( Hello ); This is equivalent to: if (<condition A>) { ; System.out.println( Hello ); The scary thing is: both are completely valid Java programs.
if s Optional Curly Brace Syntax It s also harder to maintain. Say your code looks like this: if (<condition A>) somefile.delete(); For some reason, the file isn t deleting. So you print a message: if (<condition A>) System.out.println( somefile is deleted ); somefile.delete(); Your print message doesn t show but your file is gone. Why? if (<condition A>) { System.out.println( somefile is deleted ); somefile.delete();
if s Optional Curly Brace Syntax COMP110 Rule of thumb: ALWAYS use curly braces except in one special case, of course.
Nested if-else Statements if (<condition A>) { <runs when condition A is true> else { if (<condition B>) { <runs when condition B is true AND A is false> else { <runs when conditions A AND B are false> When there are more than 2 paths our program can take we ve used if-else statements nested within an else
Nested If Else Flow if (<condition A>) { <A s truth body> else { if (<condition B>) { <B s truth body> else { <B s else> A false B false B s else true true B s truth body A s truth body
else-if Statements The ONE exception to the ALWAYS use curly brace rule: if (<condition A>) { <runs when condition A is true> else { if (<condition B>) { <runs when condition B is true AND A is false> else { <runs when conditions A AND B are false>
else-if Statements The ONE exception to the ALWAYS use curly brace rule: if (<condition A>) { <runs when condition A is true> else if (<condition B>) { <runs when condition B is true AND A is false> else { <runs when conditions A AND B are false>
else-if Statements The ONE exception to the ALWAYS use curly brace rule: if (<condition A>) { <runs when condition A is true> else if (<condition B>) { <runs when condition B is true AND A is false> else { <runs when conditions A AND B are false> Dropping the curly braces of an else-block containing a nested if-then statement is encouraged Especially if there s a lot of nesting This is commonly called an else-if statement. Try it in PS2!
Follow Along: 8 Ball Pull lecture materials Open Magic8BallRunner.java and Magic8Ball In readanswer, let s change one nested if-then-else to an else-if together
Hands-on: 8 Ball 1.In the readanswer method 2.Convert each of the nested if-then-else statements into an else-if statement 3.Make sure your code still runs 4.Check in at pollev.com/comp110
Changing Gears
PollEv: How many times does the word print? public class Example { public static void main(string[] args) { public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3);
Preview: Recursion public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); You can call a method from within the same method. This is called recursion. The recursive call must occur in an if-then-else statement that eventually becomes false. Otherwise? Infinite recursion.
How does this work? public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); CPU Example e = new Example(); e.pushups(3); Output: Call Stack:
How does this work? CPU public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(3) n: 3
How does this work? CPU true public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(3) n: 3
How does this work? CPU public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(3) n: 3
How does this work? CPU public void pushups(int n) { 2 if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(3) n: 3
How does this work? CPU public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(2) n: 2 pushups(3) n: 3
How does this work? CPU true public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(2) n: 2 pushups(3) n: 3
How does this work? CPU public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(2) n: 2 pushups(3) n: 3
How does this work? CPU public void pushups(int n) { 1 if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(2) n: 2 pushups(3) n: 3
How does this work? CPU public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(1) pushups(2) n: 1 n: 2 pushups(3) n: 3
How does this work? CPU public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(1) pushups(2) n: 1 n: 2 pushups(3) n: 3
How does this work? CPU true public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(1) pushups(2) n: 1 n: 2 pushups(3) n: 3
How does this work? CPU public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(1) pushups(2) n: 1 n: 2 pushups(3) n: 3
How does this work? public void pushups(int n) { 0 if (n > 0) { System.out.println( "); CPU this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(1) pushups(2) n: 1 n: 2 pushups(3) n: 3
How does this work? CPU public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(0) pushups(1) pushups(2) n: 0 n: 1 n: 2 pushups(3) n: 3
How does this work? CPU false public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(0) pushups(1) pushups(2) n: 0 n: 1 n: 2 pushups(3) n: 3
How does this work? CPU public void pushups(int n) { if done (n > with 0) { System.out.println( "); pushups(0) this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(0) pushups(1) pushups(2) n: 0 n: 1 n: 2 pushups(3) n: 3
How does this work? CPU public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(1) pushups(2) notice we re back in pushups(1) n: 1 n: 2 pushups(3) n: 3
How does this work? CPU public void pushups(int n) { if done (n > with 0) { System.out.println( "); pushups(1) this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(1) pushups(2) n: 1 n: 2 pushups(3) n: 3
How does this work? CPU public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(2) notice we re back in pushups(2) n: 2 pushups(3) n: 3
How does this work? CPU public void pushups(int n) { if done (n > with 0) { System.out.println( "); pushups(2) this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(2) n: 2 pushups(3) n: 3
How does this work? CPU public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: notice we re back in pushups(3) pushups(3) n: 3
How does this work? CPU public void pushups(int n) { if (n > 0) { done System.out.println( "); with pushups(3) this.pushups(n - 1); Example e = new Example(); e.pushups(3); Output: Call Stack: pushups(3) n: 3
How does this work? public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); CPU Example e = new Example(); e.pushups(3); Output: Call Stack:
Recursion is Powerful public void pushups(int n) { if (n > 0) { System.out.println( "); this.pushups(n - 1); We ll explore recursion in more depth after Fall Break Typically when we want to run a block of code repeatedly in Java: We ll use a loop Today: the while loop! Computer Science Fun(damentals) Fact: With recursion we don t actually need loops. You ll appreciate having them, though.
while Loop Statement
while Loop Statement General form of a while loop statement: while (<boolean expression>) { <while-block: code to repeat> You can use while loops inside any method or constructor Like if-then, the test condition must be a boolean expression A while loop repeatedly runs the code in the while-block while the test condition is true 46
while Loop Semantics General form of a while loop statement: while (<boolean test condition>) { <while block: code to repeat> Step 1. Test whether the test condition is true or false. If true, go to step 2. If false, go to step 4. Step 2. Run the code within the while block. Step 3. Go back to Step 1. Step 4. Continue running code after the closing curly brace. 47
while Statement Flow In a method, somewhere while Test Condition is false Continue after end of while body true Run code in while block 48
Forever and Ever!!" $%&'()*+ Humans: a diamond is forever. while(true) { // Code Computers: A while(true) loop is forever. Computers <3 looping. 49
Follow Along Let s Tinker with while Loops Open up lecture comp110.lecture12 s WhileLoopRunner Then open up WhileLoopExamples.java and I ll walk through Example 1 50
To End an Infinite Loop Click the RED STOP button in Eclipse. This will force kill your program. Otherwise your battery will be gone real quick. 51
How do we write a while loop that will not loop forever? 52
How do we write a while loop that will not loop forever? The test condition must eventually become false due to actions taken from within while-block. Here is a common pattern for looping (repeating some set of instructions) N times: int i = 0; while (i < N) { <code to repeat> i = i + 1; 53
Let s Tinker with Example 2 54
cpu Let s CPU Hat Ex.2 int i = 0; while (i < 3) { System.out.println("i:" + i); i = i + 1; System.out.println("Done! i:" + i); English: declare an local integer variable named i and assign it 0. 0 Output i 55
cpu Let s CPU Hat Ex.2 int i = 0; while (i < 3) { System.out.println("i:" + i); i = i + 1; System.out.println("Done! i:" + i); English: is i s value less than 3? If so, enter while loop body. 0 Output i 56
cpu Let s CPU Hat Ex.2 int i = 0; while (i < 3) { System.out.println("i:" + i); i = i + 1; System.out.println("Done! i:" + i); English: Concatenate the current value of i to a debug string and print to the console. 0 Output i: 0 i 57
cpu Let s CPU Hat Ex.2 int i = 0; while (i < 3) { System.out.println("i:" + i); i = i + 1; System.out.println("Done! i:" + i); English: take i s value and add 1 to it. Assign the result back to i. 1 Output i: 0 i 58
cpu Let s CPU Hat Ex.2 int i = 0; while (i < 3) { System.out.println("i:" + i); i = i + 1; System.out.println("Done! i:" + i); English: We ve reached the end of the while loop. What do we do!!? 1 Output i: 0 i 59
cpu Let s CPU Hat Ex.2 int i = 0; while (i < 3) { System.out.println("i:" + i); i = i + 1; System.out.println("Done! i:" + i); English: Go back to the start of the while loop! is i s value less than 3? If so, enter while loop body. If not, skip past it. 1 Output i: 0 i 60
cpu Let s CPU Hat Ex.2 int i = 0; while (i < 3) { System.out.println("i:" + i); i = i + 1; System.out.println("Done! i:" + i); English: Concatenate the current value of i to a debug string and print to the console. 1 Output i: 0 i: 1 i 61
cpu Let s CPU Hat Ex.2 int i = 0; while (i < 3) { System.out.println("i:" + i); i = i + 1; System.out.println("Done! i:" + i); English: take i s value and add 1 to it. Assign the result back to i. 2 Output i: 0 i: 1 i 62
cpu Let s CPU Hat Ex.2 int i = 0; while (i < 3) { System.out.println("i:" + i); i = i + 1; System.out.println("Done! i:" + i); English: We ve reached the end of the while loop. What do we do!!? 2 Output i: 0 i: 1 i 63
cpu Let s CPU Hat Ex.2 int i = 0; while (i < 3) { System.out.println("i:" + i); i = i + 1; System.out.println("Done! i:" + i); English: Go back to the start of the while loop! is i s value less than 3? If so, enter while loop body. If not, skip past it. 2 Output i: 0 i: 1 i 64
cpu Let s CPU Hat Ex.2 int i = 0; while (i < 3) { System.out.println("i:" + i); i = i + 1; System.out.println("Done! i:" + i); English: Concatenate the current value of i to a debug string and print to the console. 2 Output i: 0 i: 1 i: 2 i 65
cpu Let s CPU Hat Ex.2 int i = 0; while (i < 3) { System.out.println("i:" + i); i = i + 1; System.out.println("Done! i:" + i); English: take i s value and add 1 to it. Assign the result back to i. 3 Output i: 0 i: 1 i: 2 i 66
cpu Let s CPU Hat Ex.2 int i = 0; while (i < 3) { System.out.println("i:" + i); i = i + 1; System.out.println("Done! i:" + i); English: We ve reached the end of the while loop. What do we do!!? 3 Output i: 0 i: 1 i: 2 i 67
cpu Let s CPU Hat Ex.2 int i = 0; while (i < 3) { System.out.println("i:" + i); i = i + 1; System.out.println("Done! i:" + i); English: Go back to the start of the while loop! is i s value less than 3? If so, enter while loop body. If not, skip past it. 3 Output i: 0 i: 1 i: 2 i 68
cpu Let s CPU Hat Ex.2 int i = 0; while (i < 3) { System.out.println("i:" + i); i = i + 1; System.out.println( Done:" + i); English: Go back to the start of the while loop! is i s value less than 3? If so, enter while loop body. If not, skip past it. 3 i Output 69 i: 0 i: 1 i: 2 Done!
Follow-along #3: 8 Ball In Magic8BallRunner Let s add the following if-then statement. Substitute your lover / crush name We ll add this after an8ball.shake(); if (question.contains( <lover> )) { // TODO
Hands-on #2: 8 Ball Within the if-then statement: 1. Write a while loop whose test condition is true when The String returned by an8ball.readanswer() does not contain the String definitely Hint #1: This: an8ball.readanswer().contains( definitely ) evaluates to true if readanswer() does contain the word, otherwise false. Hint #2: What can you check to see if hint #1 is equal or not equal to using == or!= 2. Within the while-block, call an8ball.shake(); 3. Run your program a few times asking the 8Ball questions that do and don t contain your lovers name. Does your program behave as expected? Check-in on PollEv.com/comp110 when done or stuck.
if (question.contains("carol")) { while (an8ball.readanswer().contains("definitely") == false) { an8ball.shake();
Follow-along #2: 8 Ball Let s make it so that we can run the 8Ball program forever without having to restart it each time
public static void main(string[] args) { Console userinput = new Console(); Magic8Ball an8ball = new Magic8Ball(); while (true) { System.out.println("Ask a yes or no question..."); String question = userinput.askforastring(); an8ball.ask(question); an8ball.shake(); if (question.contains("kris")) { while (an8ball.readanswer().contains("definitely") == false) { an8ball.shake(); System.out.println("====================="); System.out.println(" The 8 Ball Says... "); System.out.println(an8Ball.readAnswer()); System.out.println("=====================");
while Loop Recap