©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.

Slides:



Advertisements
Similar presentations
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Advertisements

Repeating Actions While and For Loops
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
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.
CS 117 Spring 2002 Repetition Hanly Chapter 4 Friedman-Koffman Chapter 5.
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.
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
©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 STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
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 Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
Chapter 5 Loops.
Chapter 4: Loops and Files. The Increment and Decrement Operators  There are numerous times where a variable must simply be incremented or decremented.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Loops George Mason University. Loop Structure Loop- A structure that allows repeated execution of a block of statements Loop body- A block of statements;
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.
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.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson
Chapter Looping 5. The Increment and Decrement Operators 5.1.
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.
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
1 Fall 2009ACS-1903 Ch 4 Loops and Files while loop do-while loop for loop Other topics later on …
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/19 Outline The if Statement and Conditions Other Conditional.
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Lesson #5 Repetition and Loops.
Chapter 4 Repetition Statements (loops)
Chapter 5: Control Structures II
Lesson #5 Repetition and Loops.
Chapter 5: Control Structures II
Loop Structures.
CiS 260: App Dev I Chapter 4: Control Structures II.
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Outline Altering flow of control Boolean expressions
Lesson #5 Repetition and Loops.
Loops A loop is a repetition control structure.
Repetition Control Structure
Lab5 PROGRAMMING 1 Loop chapter4.
Week 6 CPS125.
Lesson #5 Repetition and Loops.
Repetition Statements
PROGRAM FLOWCHART Iteration Statements.
Looping and Repetition
Presentation transcript:

©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 of data. More useful is to be able to process many sets of data in the same way Repetition statements allow us to do this. Java has three repetition statements –while –do … while –for

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 The while statement Syntax while ( ) Semantics –test the condition –if true, execute the body and go back to test –if false, continue to next statement in program

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Anatomy of a while Loop

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Loop Example int count = 1; while (count <= 10) { System.out.println( count + " "); count++; } Body of loop needs to update the count variable –Otherwise, you will have an infinite loop Above is example of counting loop

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Loop Example Schematically, a while looks like the following var = initVar(); while (testVar()) { doBody(); var = updateVar(); } Update can be any kind of change to var –increment, decrement, input, …

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Interactive Input Reading a fixed number of values

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Accumulating Input Values Suppose we want to average the values we read using a loop. –Since we use the same variable for all the input values, we need to add each value to the total as we enter it. Later we will learn to use arrays to save the values we read in.

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Accepting and Adding a Number to a Total

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Accumulation Flow of Control AddNumbers.java AverageNumbers.java

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sentinel Loops Sometimes, we don't know how many values need to be entered ahead of time. In this case, we can read values until a specified sentinel value is entered. Example: Sentinels.java –A number greater than 100 will signal the end of the input

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Interrupting loops Sometimes you need to exit a loop before it is finished –The break statement jumps to next statement after the loop –loop and a half The continue statement causes a jump to the next iteration of the loop

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 For loops for syntax for(init; condition; update) statement; Equivalent while init; while (condition) { statement; update; } Counting loops are common enough that Java provides a special syntax for them

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 The for Statement’s Flow of Control 1.init is done before the loop starts 2.condition is tested 3.statement is executed 4.update is executed 5.go back to 2

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Nested loops The body of a loop can include any valid Java statement If the body includes another loop, we say the loops are nested

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Nested loops The inner loop is executed completely for each iteration of the outer loop The body of the inner loop is executed 3*4=12 times

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 do - while loops Sometimes, it is more convenient to test the loop condition after the loop body has executed. do { statement; } while (condition); The body of the do-while loop always executes at least once

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 The do Statement’s Flow of Control

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Input validation Loops are often used to validate input do readData; while (dataNotValid);

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Testing for valid data with Scanner The Scanner class has some methods you can use to check for the right kind of data. boolean hasNext() boolean hasNextInt() boolean hasNextDouble() You can use a loop to read for as long as there is data of the proper type.

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Reading to end of valid input Scanner kbd = new Scanner( system.in); int value; while ( Scanner.hasNextInt()) { value = kbd.nextInt(); process data }