Repetition Statements Recitation – 02/20/2009 CS 180 Department of Computer Science, Purdue University.

Slides:



Advertisements
Similar presentations
Intro to CS – Honors I Control Flow: Loops GEORGIOS PORTOKALIDIS
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
COMP 14 Introduction to Programming Mr. Joshua Stough February 16, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
©2004 Brooks/Cole Chapter 5 Repetition. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Repetition So far, our programs processed a single set.
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
FIT Objectives By the end of this lecture, students should: understand iteration statements understand the differences of for and while understand.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Chapter 5 Loops.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
Repetition Statements while and do while loops
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/29 The switch Statement The switch statement provides another way.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
Flow of Control: Loops Module 4. Objectives Design a loop Use while, do, and for in a program Use the for-each with enumerations Use assertion checks.
COMP Loop Statements Yi Hong May 21, 2015.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
While ( number
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
Feedback  Lab2, Hw1  Groups  Group Project Requirements.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Lecture 4 CS140 Dick Steflik. Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using.
Repetition Statements
Chapter 4 Repetition Statements (loops)
Chapter 6 More Conditionals and Loops
Java's for Statement.
CiS 260: App Dev I Chapter 4: Control Structures II.
Chapter 5: Control Structures II
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
Flow Control in Java.
Looping and Repetition
Outline Altering flow of control Boolean expressions
More Loops Topics Counter-Controlled (Definite) Repetition
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011
Repetition Statements
More Loops Topics Counter-Controlled (Definite) Repetition
Outline Boolean Expressions The if Statement Comparing Data
Chap 7. Advanced Control Statements in Java
CSS161: Fundamentals of Computing
More Loops Topics Counter-Controlled (Definite) Repetition
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Looping and Repetition
Presentation transcript:

Repetition Statements Recitation – 02/20/2009 CS 180 Department of Computer Science, Purdue University

Announcement Grades are out: Project 2, 3 Lab 2, 3, 4, 5 Project 4 is due on Feb 25 th

while loop do-while loop for loop 3 Syntax for Three Forms of Loops while ( ) while ( ) do while ( ) ; do while ( ) ; for ( ; ; ) for ( ; ; )

4 Control Flow of while next statement; true false sum = sum + number; number = number + 1; int sum = 0, number = 1 previous statement; number <= 100 ? while ( number <= 100 ) { sum = sum + number; number = number + 1; } Statement (loop body) Boolean Expression

5 Control Flow of do-while next statement; true false int sum = 0, number = 1 previous statement; sum += number; number++; number <= 100 ? do { sum += number; number++; } while ( number <= 100 ); Boolean Expression Statement (loop body)

6 Control flow of for next statement; true false number=1,sum=0; previous statement; sum += number; number++; for ( number=1,sum=0 ; number<=100 ; number++ ) { sum += number; } number<=100? Initialization Statement (loop body) Boolean Expression Increment

If you know how many times the loop will be iterated, use a for loop. If you don’t know how many times the loop will be iterated, but it could be zero, use a while loop it will be at least once, use a do-while loop. 7 Choosing a Loop

8 Example 1 The charAt( int ) method of the String class returns the character at the given index of an instance. public String someMeth1( String str ) { String result = ""; for ( int index = str.length() - 1; index >= 0; --index ) { result += str.charAt( index ); } return result; } What is the result of: someMeth1( "somewhere i have never traveled" )

9 public void someMeth2() { String name = ""; Scanner keyboard = new Scanner( System.in ); do { System.out.print( "Please enter a name or \"quit\" to exit: " ); name = keyboard.next(); System.out.println( "Howdy "+name+"!" ); } while ( !name.equals( "quit" ) ); } How does the above compare to using a while loop? Is a for loop appropriate? Example 2

Does the above loop terminate? How does this compare to someMeth2 ? 10 public void someMeth3() { String name = ""; Scanner keyboard = new Scanner( System.in ); while ( true ) { System.out.print( "Please enter a name or \"quit\" to exit: " ); name = keyboard.next(); if ( !name.equals( "quit" ) ) System.out.println( "Howdy "+name+"!" ); else break; } Example 3

11 public void someMeth4() { String name = ""; Scanner keyboard = new Scanner( System.in ); S.o.p( "Please enter a name or \"quit\" to exit: " ); name = keyboard.next(); while ( !name.equals("quit") ) { S.o.pln( "Howdy "+name+"!" ); S.o.p( "Please enter a name or \"quit\" to exit: " ); name = keyboard.next(); } Notice that this has a cleaner flow than example 3. Priming the loop can avoid extra cases and bugs. Example 4

12 public void someMeth5() { Scanner keyboard = new Scanner( System.in ); while ( true ) { switch( keyboard.nextInt() ) { case 0: case 1: case 2: System.out.println( "In here" ); break; case 3: case 4: case 5: System.out.println( "Somewhere else" ); continue; } System.out.println( "After Switch" ); } How does this loop behave when 7 is entered? 3? 0? Example 5

13 public void someMeth6() { for ( int i = 0; i < 5; ++i ) { for ( int j = 5-i; j > 0; --j ) System.out.print( "*" ); System.out.println(); } Notice how loops within loops can interact. What does each loop correspond to? Notice how using a table can help you execute this manually. Example 6

14 public void someMeth7() { for ( int i = 0; i < 5; ++i ) { for ( int j = 5-i; j > 0; --i ) System.out.print( "*" ); System.out.println(); } What happens here? Where is the mistake? Example 7

15 public void someMeth8() { for ( int i = 0; i < 5; ++i ) { for ( int j = 5-i; j > 0; --j ) System.out.print( "*" ); --i; System.out.println(); } How does this behave? Example 8

16 public void someMeth9() { int sum = 0; for (int count=1; count<5; count++) System.out.println(“Count:” + count); sum = sum + count; System.out.println(“Sum: ” + sum); } How does this behave? What have we forgotten? Example 9

17 public void someMeth10() { int sum = 0; for (int count=1; count<5; count++); { System.out.println(“Count:” + count); sum = sum + count; } System.out.println(“Sum: ” + sum); } How about this one? Example 10

18 Quiz What is the difference between break and continue ? Are the different forms of loops equivalent?