Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Loops and Input COMP 102 #

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Chapter 7 - Iteration. Chapter Goals Program repitiation statements – or loops – with the for, while, and do-while statements Program repitiation statements.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Topic 17 assertions and program logic
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
Introduction to Computing Science and Programming I
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
1 Loops. 2 Often we want to execute a block of code multiple times. Something is always different each time through the block. Typically a variable is.
Computer Science 1620 Loops.
CS 106 Introduction to Computer Science I 02 / 04 / 2008 Instructor: Michael Eckmann.
Announcements The first graded lab will be posted Sunday 2/15 and due Friday 2/27 at midnight It is brand new, so please don’t hand in last semester’s.
Loops – While, Do, For Repetition Statements Introduction to Arrays
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
1 File Input. 2 Reading Files We have already seen that writing files is (can be?) similar to using System.out Not surprisingly, reading from files is.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Review COMP 102 #
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Files COMP 102.
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington “For each” & Patterns with.
Repetition and Iteration ANSI-C. Repetition We need a control instruction to allows us to execute an statement or a set of statements as many times as.
Copyright © 2002, Department of Systems and Computer Engineering, Carleton University CONTROL STRUCTURES Simple If: if (boolean exp) { statements.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Classes, Objects, Fields,
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Fields, Constructors.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
CS 106 Introduction to Computer Science I 09 / 26 / 2007 Instructor: Michael Eckmann.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Loops and Input COMP 102 #
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods Calling Methods Return.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Event-driven Input COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Programs with Choice Booleans,
COMP Loop Statements Yi Hong May 21, 2015.
2011-T1 Lecture 12 School of Engineering and Computer Science, Victoria University of Wellington  Rashina Hoda and Peter Andreae COMP 102 Rashina Hoda.
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Creating and using Objects.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with While loops.
© Peter Andreae Java Programs COMP 102 # T1 Peter Andreae Computer Science Victoria University of Wellington.
Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Dealing with Files COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More on ArrayLists COMP 102.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Event-driven Input COMP.
Python: Control Structures
Selected Topics From Chapter 6 Iteration
getline() function with companion ignore()
Outline Altering flow of control Boolean expressions
Building Java Programs
Indefinite loop variations
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Presentation transcript:

Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Loops and Input COMP 102 #

© Peter Andreae COMP : 2 Menu CartoonStrip answers Understanding input More on Loops Admin Test Monday, 5-6pm Abdust - Gibbs: HM LT205 Giles - Pasa: KK LT303 Patel - Zhu: MC LT103 Students in PHYS114: MC LT pm.

© Peter Andreae COMP : 3 CartoonStrip: core public void animate(){ CartoonFigure face1 = new CartoonFigure(150, 100, "yellow"); face1.lookRight(); face1.lookLeft(); face1.frown(); face1.speak("Is anyone here?"); CartoonFigure face2 = new CartoonFigure(450, 100, “blue"); face2.smile(); face2.lookLeft(); face2.speak("Hello"); face1.lookRight(); : face2: face1: CartoonFigure-16CartoonFigure-17 face1: CartoonFigure-16 face2: CartoonFigure-17

© Peter Andreae COMP : 4 CartoonStrip: Completion public void threeDancers(){ CartoonFigure fig1 = new CartoonFigure( 150, 100, “blue"); CartoonFigure fig2 = new CartoonFigure( 150, 100, “blue"); this.dance( fig1 ); this.dance( fig2 ); } public void dance( CartoonFigure fig ){ fig.smile(); fig.lookLeft(); fig.walk(20); fig.lookRight(); fig.walk(40); fig.lookLeft(); fig.walk(20); }

© Peter Andreae COMP : 5 Text Input: Asking for one value To ask user for a single value: UI.askString("Enter name: ") UI.askInt("How old are you: ") UI.askDouble("How tall are you: ") All of these: clear all the current input, print out the prompt read the value they want from the user (repeating the question if the answer is the wrong type) clear all remaining input eg How old are you: 57 years old ↩

© Peter Andreae COMP : 6 Text Input: reading multiple values When you type into the text pane: nothing happens until you type a newline (“enter”) Then all the characters on the line are put into a buffer that the program can access The program can access the buffer using the “UI.next…” methods: UI.next()reads next “token” as a string UI.nextInt()reads next “token” as an integer UI.nextDouble()reads next token as a double UI.nextLine() reads up to next ↩ as a string all the methods move the cursor forward, past what was read. This 20 pt text has 3 ↩ ↩ ↩ This 20 pt text has 3 ↩ numbers, words, and 6% spam. ↩  “This”  20  ERROR!  “pt text has 3” This 20 pt text has 3 numbers, words, and 6% spam

© Peter Andreae COMP : 7 Text Input: reading multiple values If there is no input yet, the UI.next…() methods will just wait. ⇒ Always print a prompt to the user before you try to read! It is not safe to call UI.nextInt() or UI.nextDouble() unless you can be certain the next token is an integer / double! How can you tell? UI.hasNextInt()  booleantrue if next token is an integer UI.hasNextDouble()  booleantrue if next token is a double UI.hasNext()  booleantrue if there is a next token (always true for text pane)

© Peter Andreae COMP : 8 next vs. nextLine() next(), nextInt(), nextDouble() picks up any spaces, discards them, and picks up next value (till it reaches a space), then returns the value next() returns it as a String nextInt() returns it as an int, nextDouble() returns it as a double. nextLine() Picks up all the values (including spaces) until it reaches end-of-line character, throws away end-of-line, and returns all the values (including spaces) as a String.

© Peter Andreae COMP : 9 Input with "next" methods MethodWhat it doesReturns next()Read and return next token of user’s inputString hasNext()Returns true if there is another token in the user input. Waits for the user to type something if necessary. boolean nextInt() nextDouble() Read the next token of the user's input. Return it as a integer if it is a number. Throws an exception if it is not a number. int double hasNextInt() hasNextDouble() Returns true if next token in the input is an int / double. Waits for user to type something if necessary. boolean nextBoolean()Read the next token of the user's input. Return true if it is "yes", "y", or "true", return false if it is "no", "n", or "false" (case insensitive). Throws an exception if it is anything else. boolean nextLine()Read the remaining characters of the user's input up to (but not including) the next end-of-line and return them as a string. Reads and throws away the end-of-line character. If there are no characters on the line, then it returns an empty string (""). String

© Peter Andreae COMP : 10 next methods...how they work /** sum up all numbers entered by user */ : UI.print(“Enter numbers: end with ‘done’:”); double sum = 0; while (UI.hasNextDouble() ) {//peeking at next value or “token” double amt = UI.nextDouble(); //getting the next value and move pointer sum = sum + amt; } UI.next(); UI.printf(“Total of all numbers entered: %.2f %n”, sum); Enter numbers: end with ‘done’: done Enter numbers: end with ‘done’: done * MENU *

© Peter Andreae COMP : 11 Reading words from user public void countWordsBeforeThe( ) { int count = 0; UI.print("Enter some words: ”); // loop, stopping when you get to 'the' // read next token // increment count UI.printf(“You had %d words before 'the'. %n", count); }

© Peter Andreae COMP : 12 Reading words from user: BAD public void countWordsBeforeThe( ) { int count = 0; UI.print("Enter some words: ”); while ( ! word.equalsIgnoreCase("the”) ) { String word = UI.next(); count = count + 1; } UI.printf(“You had %d words before 'the'. %n", count); } Too late for the condition!!!

© Peter Andreae COMP : 13 Reading words from user: BAD public void countWordsBeforeThe( ) { int count = 0; UI.print("Enter some words: ”); String word = UI.next(); while ( ! word.equalsIgnoreCase("the”) ) { count = count + 1; } UI.printf(“You had %d words before 'the'. %n", count); } never reads the next word !!!

© Peter Andreae COMP : 14 Reading words from user: Fixed public void countWordsBeforeThe( ) { int count = 0; UI.print("Enter some words: ”); String word = UI.next(); while ( ! word.equalsIgnoreCase("the”) ) { count = count + 1; word = UI.next(); } UI.printf(“You had %d words before 'the'. %n", count); } read next word at end of loop ("increment") read first word before loop

© Peter Andreae COMP : 15 Alternate design: using break. public void countWordsBeforeThe( ) { int count = 0; UI.print("Enter some words: ”); while ( true ) { String word = UI.next(); if ( word.equalsIgnoreCase("the”) ){ break; } count = count + 1; } UI.printf(“You had %d words before 'the'. %n", count); } Note: Textbook does not like this style; I do Only use when the test has to be in the middle of the loop Typically only use with a while (true) {….. The condition is an exit condition, not a keep going condition gets out of the enclosing while loop

© Peter Andreae COMP : 16 Designing loops Is the number of steps determined at the beginning? int i = 0;int i = 1; while ( i < number) {while ( i <= number) {  do actions 〉 i = i + 1;i++;} Note, can count from 0 or from 1! Otherwise 〈 intialise 〉 while ( 〈 condition-to-do it again 〉 ) { 〈 body 〉 〈 increment 〉 } Shorthand for i = i+1

© Peter Andreae COMP : 17 Designing loops Does exiting the loop depend on the actions? if soor set up for the test while ( true ) { while ( test ) {set up for the test do actionsif ( exit-test ) { set up for the next test break; } do actions } Write out the steps for a couple of iterations including the tests to determine when to quit/keep going Identify the section that is repeated preferably starting with the test Wrap it with a while ( ) { } Identify the condition for repeating.