© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.

Slides:



Advertisements
Similar presentations
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
Computer Science 1620 Loops.
Topic 6 – Repetition and Loops. CISC 105 – Topic 6 Program Repetition Repetition refers to the repeats of certain program statements within a program.
Chapter 2: Algorithm Discovery and Design
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Loops – While, Do, For Repetition Statements Introduction to Arrays
1 Lab Session-6 CSIT-121 Spring 2005 Structured Choice The do~While Loop Lab Exercises.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
1 Lab Session-7 CSIT-121 Fall Introducing Structured Choice 4 The do~While Loop 4 Lab Exercises.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Chapter 4 Program Control Statements
Chapter 4: Looping. Resource: Starting Out with C++, Third Edition, Tony Gaddis 5.1 The Increment and Decrement Operators ++ and -- are operators that.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
While Loops Indefinite Iteration. Last lesson we looked at definite loops using the ‘For’ statement. The while loop keeps going while some condition is.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
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.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
ANALYSIS AND ALGORITHM DESIGN - IV. Repeat statements/looping/counting/iterations It is often necessary to repeat certain parts of a program a number.
Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Chapter 4: Control Structures II
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)
Intro to Loops 1.General Knowledge 2.Two Types of Loops 3.The WHILE loop 1.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
1 Week 9 Loops. 2 Repetition: Loops l Structure: »Usually some initialization code »body of loop »loop termination condition l Several logical organizations.
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.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Selection Using IF THEN ELSE CASE Introducing Loops.
UCT Department of Computer Science Computer Science 1015F Iteration
Topic 4: Looping Statements
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Chapter 5: Control Structures II (Repetition)
Loop Structures.
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.
Chapter 8 Repetition Statements
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Outline Altering flow of control Boolean expressions
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Coding Concepts (Basics)
Looping III (do … while statement)
Topics Introduction to Repetition Structures
CprE 185: Intro to Problem Solving (using C)
ICS103: Programming in C 5: Repetition and Loop Statements
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Looping and Repetition
Presentation transcript:

© The McGraw-Hill Companies, 2006 Chapter 3 Iteration

© The McGraw-Hill Companies, 2006 Iteration Iteration is the form of program control that allows us to repeat a section of code. For this reason this form of control is often also referred to as repetition. The programming structure that is used to control this repetition is often called a loop. There are three types of loops in Java: –for loop; –while loop; –do…while loop.

© The McGraw-Hill Companies, 2006 When to use a loop? Display a square of stars (five by five) on the screen as follows: * * * * * This could be achieved with five output statements executed in sequence. Better to repeat one output statement five times with a loop.

© The McGraw-Hill Companies, 2006 The ‘for’ loop If we wish to repeat a section of code a fixed number of times (five in the example above) we would use Java's for loop. The for loop is usually used in conjunction with a counter to keep track of how many times we have been through the loop so far:

© The McGraw-Hill Companies, 2006 Using a ‘for’ loop to display a 5x5 square of stars

© The McGraw-Hill Companies, 2006 The loop counter Very common way of using a for loop: –start the counter at 1; –add 1 to the counter each time the loop repeats. However –you may start your counter at any value and –you may change the counter in anyway you choose.

© The McGraw-Hill Companies, 2006 A different way of using the loop counter

© The McGraw-Hill Companies, 2006 Nested loops

© The McGraw-Hill Companies, 2006 Non-fixed repetitions The for loop is an often used construct to implement fixed repetitions. Sometimes, however, a repetition is required that is not fixed: –a racing game that repeatedly moves a car around a track until the car crashes; –a ticket issuing program that repeatedly offers tickets for sale until the user chooses to quit the program; –a password checking program that does not let a user into an application until he or she enters the right password. The while loop offers one type of non-fixed iteration.

© The McGraw-Hill Companies, 2006 The ‘while’ loop The syntax for constructing this loop in Java is as follows: As this loop is not repeating a fixed number of times, there is no need to create a counter to keep track of the number of repetitions.

© The McGraw-Hill Companies, 2006 Input validation checking input data for errors is referred to as input validation; example: asking the user to enter an exam mark (as a percentage); the mark should never be greater than 100 or less than 0

© The McGraw-Hill Companies, 2006 Using the ‘while’ loop

© The McGraw-Hill Companies, 2006 The ‘do…while’ loop the do…while loop is another variable loop construct unlike the while loop, the do…while loop has its test at the end of the loop rather than at the beginning. the syntax of a do…while loop is given below:

© The McGraw-Hill Companies, 2006 Implications of having the test at the end of the loop if the test is at the end of the loop, the loop will iterate at least once; if the test is at the beginning of the loop, however, there is a possibility that the condition will be false to begin with, and the loop is never executed; a while loop therefore executes zero or more times; a do...while loop executes one or more times.

© The McGraw-Hill Companies, 2006 Using the ‘do…while’ loop SO FAR: programs you have written stop as soon as they have done their job; BETTER SOLUTION: put whole program in a loop that keeps repeating until the user chooses to quit your program; involves asking the user each time if he or she would like to continue repeating your program, or to stop; a while loop would be difficult to use, as the test that checks the user's response to a question cannot be carried out at the beginning of the loop; the answer is to move the test to the end of the loop and use a do…while loop.

© The McGraw-Hill Companies, 2006 An example program

© The McGraw-Hill Companies, 2006

Sample run of program 3.4 *** Product Price Check *** Enter initial price: 50 Enter tax rate: 10 Cost after tax = 55.0 Would you like to enter another product (y/n)?: y *** Product Price Check *** Enter initial price: 70 Enter tax rate: 5 Cost after tax = 73.5

© The McGraw-Hill Companies, 2006 Sample run of program 3.4 continued Would you like to enter another product (y/n)?: Y *** Product Price Check *** Enter initial price: 200 Enter tax rate: 15 Cost after tax = Would you like to enter another product (y/n)?: n

© The McGraw-Hill Companies, 2006 Menu driven programs another way to allow a program to be run repeatedly using a do...while loop is to include a menu of options within the loop the options themselves are processed by a switch statement. one of the options in the menu list would be the option to quit and this option is checked in the while condition of the loop.

© The McGraw-Hill Companies, 2006 A complete menu driven program

© The McGraw-Hill Companies, 2006

Sample run of program 3.5 ***Lab Times*** [1] TIME FOR GROUP A [2] TIME FOR GROUP B [3] TIME FOR GROUP C [4] QUIT PROGRAM enter choice [1,2,3,4]: p.m [1] TIME FOR GROUP A [2] TIME FOR GROUP B

© The McGraw-Hill Companies, 2006 Sample run of program 3.5 continued [3] TIME FOR GROUP C [4] QUIT PROGRAM enter choice [1,2,3,4]: 5 Options 1-4 only! [1] TIME FOR GROUP A [2] TIME FOR GROUP B [3] TIME FOR GROUP C [4] QUIT PROGRAM enter choice [1,2,3,4]: a.m

© The McGraw-Hill Companies, 2006 Sample run of program 3.5 continued [1] TIME FOR GROUP A [2] TIME FOR GROUP B [3] TIME FOR GROUP C [4] QUIT PROGRAM enter choice [1,2,3,4]: a.m [1] TIME FOR GROUP A [2] TIME FOR GROUP B [3] TIME FOR GROUP C [4] QUIT PROGRAM enter choice [1,2,3,4]: 4 Goodbye

© The McGraw-Hill Companies, 2006 Picking the right loop if the number of repetitions required can be determined prior to entering the loop - use a for loop; if the number of repetitions required cannot be determined prior to entering the loop, and you wish to allow for the possibility of zero repetitions - use a while loop; if the number of repetitions required cannot be determined before the loop, and you require at least one repetition of the loop - use a do...while loop.