Pull Lecture Materials and Open PollEv. Poll Everywhere: pollev.com/comp110. Lecture 12. else-if and while loops. Once in a while

Similar documents
Controls Structure for Repetition

Chapter 1: Introduction to Computers, Programs, and Java

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

CS125 : Introduction to Computer Science. Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Real World. static methods and Console & JavaFX App Intros. Lecture 16. Go ahead and PULL Lecture Materials & Sign-in on PollEv

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

Changing an Object s Properties

CS1150 Principles of Computer Science Loops (Part II)

Introduction to the Java Basics: Control Flow Statements

COMP110 Jump Around. Go ahead and get today s code in Eclipse as shown on next few slides. Kris Jordan

COMPUTER PROGRAMMING LOOPS

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Following is the general form of a typical decision making structure found in most of the programming languages:

Handout 5 cs180 - Programming Fundamentals Spring 15 Page 1 of 8. Handout 5. Loops.

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Loops. CSE 114, Computer Science 1 Stony Brook University

CS 106 Introduction to Computer Science I

Introduction to Programming Using Java (98-388)

Open and respond to this poll: PollEv.com/comp110. Calling Methods Mr. Roboto

Repetition CSC 121 Fall 2014 Howard Rosenthal

CS111: PROGRAMMING LANGUAGE II

CS 101 Fall 2005 Midterm 2 Name: ID:

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Condi(onals and Loops

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

Java Review. Fundamentals of Computer Science

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Text Input and Conditionals

CONDITIONAL EXECUTION

CSE 142 Su 04 Computer Programming 1 - Java. Objects

TESTING AND DEBUGGING

Mobile App:IT. Methods & Classes

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

APCS Semester #1 Final Exam Practice Problems

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2016

Basic Java Syntax, cont d. COMP 401, Fall 2015 Lecture 3 1/15/2015

Programming in the Small II: Control

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Mr. Monroe s Guide to Mastering Java Syntax

Tutorials. Inf1-OP. Learning Outcomes for this week. A Foundation for Programming. Conditionals and Loops 1

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

Programming with Java

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Getting started with Java

Repetition with for loops

Scope of this lecture. Repetition For loops While loops

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

Java Control Statements

Basic Java Syntax. COMP 401, Fall 2015 Lecture 2 1/13/2014

Functions. x y z. f (x, y, z) Take in input arguments (zero or more) Perform some computation - May have side-effects (such as drawing)

Java Programming Basics. COMP 401, Spring 2017 Lecture 2

Chapter 7. Iteration. 7.1 Multiple assignment

8. Control statements

CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8

Lec 3. Compilers, Debugging, Hello World, and Variables

Computer Programming I - Unit 5 Lecture page 1 of 14

Repetition, Looping. While Loop

Flow Control. CSC215 Lecture

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

CS 231 Data Structures and Algorithms, Fall 2016

COMP 110/L Lecture 5. Kyle Dewey

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

CS313D: ADVANCED PROGRAMMING LANGUAGE

Decision Making in C

CS 177 Week 5 Recitation Slides. Loops

Course Outline. Introduction to java

Introduction to Java

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2015

CS 106 Introduction to Computer Science I

COMP 202 Java in one week

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

CS 102 / CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2010

Object-Oriented Programming

About this exam review

University of Palestine. Mid Exam Total Grade: 100

Java Programming Basics. COMP 401, Fall 2017 Lecture 2

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2013

Midterms Save the Dates!

CS1004: Intro to CS in Java, Spring 2005

Java Bytecode (binary file)

Java Basic Programming Constructs

Michele Van Dyne Museum 204B CSCI 136: Fundamentals of Computer Science II, Spring

COMP-202: Foundations of Programming. Lecture 6: Conditionals Jackie Cheung, Winter 2016

1.00 Lecture 6. Java Methods

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Transcription:

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