CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Programming Logic and Design Eighth Edition
Repetition control structures
Programming Logic and Design, Third Edition Comprehensive
Objectives In this chapter, you will learn about:
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Chapter 5: Control Structures II (Repetition)
 2007 Pearson Education, Inc. All rights reserved C Program Control.
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.
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Programming Logic and Design Fifth Edition, Comprehensive
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Programming Logic and Design Sixth Edition Chapter 5 Looping.
6 Chapter 61 Looping Programming Logic and Design, Second Edition, Comprehensive 6.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Repetition Control Structures Simple Program Design Third Edition A Step-by-Step Approach 5.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
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 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
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.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Chapter 4 – C Program Control
Chapter 4 Repetition Statements (loops)
Lecture 7: Repeating a Known Number of Times
Programming Logic and Design Fourth Edition, Comprehensive
JavaScript: Control Statements I
CHAPTER 5A Loop Structure
Chapter 5: Repetition Structures
Ch 7: JavaScript Control Statements I.
( Iteration / Repetition / Looping )
Repetition Structures (Loops)
Computer Science Faculty
JavaScript: Control Statements.
Learning About the Loop Structure (continued)
Control Structure Senior Lecturer
Programming Fundamentals Lecture #6 Program Control
MSIS 655 Advanced Business Applications Programming
Outline Altering flow of control Boolean expressions
Structured Program Development in C
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Chapter 8: More on the Repetition Structure
Chapter 6: Repetition Statements
ICT Programming Lesson 3:
CHAPTER 4 Iterative Structure.
Repetition Statements (Loops) - 2
PROGRAM FLOWCHART Iteration Statements.
Presentation transcript:

CHAPTER 5: Repetition Control Structure

Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for counted repetition  Develop algorithms using variations of repeat structure  Learn to avoid common loop mistakes

Repeat Loop DOWHILE condition p is true statement block ENDDO WHILE condition p is true statement block ENDWHILE

Advantages  Looping makes computer programming efficient and worthwhile  Write one set of instructions to operate on multiple, separate sets of data  Loop: structure that repeats actions while some condition continues

Controlling Loops  As long as a Boolean expression remains true, while loop’s body executes  Must control number of repetitions to avoid an infinite loop  Repetitions controlled by  Counter  Sentinel value

Controlling Loops with Counters and Sentinel Values  As long as a Boolean expression remains true, DOWHILE loop’s body executes  Must control number of repetitions to avoid an infinite loop  Repetitions controlled by  Counter  Sentinel value

Using a Definite while Loop with a Counter  Three actions make a DOWHILE loop end correctly:  Loop control variable is initialized Prior to entering the loop  Loop control variable is tested If result is true, loop body entered  Loop control variable must be altered in loop body DOWHILE expression eventually evaluates to false  Loop control variables altered by:  Incrementing  Decrementing

Using a Definite DOWHILE Loop with a Counter (continued)

Programming Logic and Design, Fifth Edition, Comprehensive9 Using a Definite while Loop with a Counter (continued)  Definite loop: number of iterations predetermined  Also called counted loop  Counter: numeric variable used to count number of times an event occurs  Loop control variable may be altered by user input  Indefinite loop: loop iterates until some condition is true  Number of iterations may vary

Using an Indefinite DOWHILE Loop with a Sentinel Value  Indefinite loop: loop performed a different number of times each time the program executes  Three crucial steps:  Starting value to control the loop must be provided  Comparison must be made using the value that controls the loop  Within the loop, value that controls the loop must be altered  Loop control variable: any variable that determines whether the loop will continue

Using an Indefinite DOWHILE Loop with a Sentinel Value (continued)

Avoiding Common Loop Mistakes  Neglecting to initialize the loop control variable  Neglecting to alter the loop control variable  Using the wrong comparison with the loop control variable  Including statements inside the loop that belong outside the loop

Using a for Loop for statement or for loop is a definite loop  Provides three actions in one structure  Initializes  Evaluates  Increments  Takes the form: for initialValue to finalValue do something endfor

Using a for Loop (continued)  Example: for count = 0 to 99 print LABEL_TEXT, name endfor  Initializes count to 0  Checks count against the limit value 99  If evaluation is true, for statement body prints the label  Increases count by 1

Summary  When using a loop, write one set of instructions that operates on multiple, separate data  Three steps must occur in every loop:  Initialize loop control variable  Compare variable to some value  Alter the variable that controls the loop

Summary (continued)  Common mistakes made by programmers:  Neglecting to initialize loop control variable  Neglecting to alter loop control variable  Using wrong comparison with loop control variable  Including statements inside the loop that belong outside the loop  Most computer languages support a for statement  for loop used with definite loops  When number of iterations is known