Repetition Control Structures Simple Program Design Third Edition A Step-by-Step Approach 5.

Slides:



Advertisements
Similar presentations
Programming Logic and Design Eighth Edition
Advertisements

Repetition There are three different ways that a set of instructions can be repeated, and each way is determined by where the decision to repeat is.
Repetition Control Structures
Repetition control structures
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Program Design Tool. 6 Basic Computer Operations Receive information Put out information Perform arithmetic Assign a value to variable (memory location)
Objectives In this chapter, you will learn about:
Repetition Control Structure
Do/Loops A loop repeats a series of instructions. An iteration is a single execution of the statement(s) in the loop. Used when the exact number of iterations.
Steps in Program Development
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
Chapter 2: Algorithm Discovery and Design
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Program Design and Development
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.
Objectives You should be able to describe:
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Repetition Control Structures
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand basic loop concepts: ■ pretest loops and post-test loops ■ loop.
Pseudocode.
Chapter 2: Algorithm Discovery and Design
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Pseudocode.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Pseudocode algorithms using sequence, selection and repetition
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Developing an Algorithm
Selection Control Structures Simple Program Design Third Edition A Step-by-Step Approach 4.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Programming Logic and Design Fifth Edition, Comprehensive
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
Developing an Algorithm
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Programming Logic and Design Sixth Edition Chapter 5 Looping.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 2 Pseudocode. Objectives To introduce common words, keywords and meaningful names when writing pseudocode To define the three basic control structures.
Pseudocode. Simple Program Design, Fourth Edition Chapter 2 2 Objectives In this chapter you will be able to: Introduce common words, keywords, and meaningful.
Pseudocode Simple Program Design Third Edition A Step-by-Step Approach 2.
9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
Pseudocode Algorithms Using Sequence, Selection, and Repetition
Pseudocode Algorithms Using Sequence, Selection, and Repetition Simple Program Design Third Edition A Step-by-Step Approach 6.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Developing an Algorithm. Simple Program Design, Fourth Edition Chapter 3 2 Objectives In this chapter you will be able to: Introduce methods of analyzing.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
A First Book of C++ Chapter 5 Repetition.
Chapter 7 Problem Solving with Loops
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.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
Pendalaman Materi Conditional dan Repetition (Pengulangan)
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Computer Science Faculty
Chapter 6 Repetition Objectives ❏ To understand basic loop concepts:
Unit# 9: Computer Program Development
Program Design Introduction to Computer Programming By:
Pseudocode algorithms using sequence, selection and repetition
MSIS 655 Advanced Business Applications Programming
Iteration: Beyond the Basic PERFORM
CHAPTER 4 Iterative Structure.
Presentation transcript:

Repetition Control Structures Simple Program Design Third Edition A Step-by-Step Approach 5

Objectives To develop algorithms which use the DOWHILE and REPEAT…UNTIL control structures To introduce a pseudocode structure for counted repetition loops To develop algorithms using variations of the repetition construct 5

Repetition Using the DOWHILE Structure Most programs require the same logic to be repeated for several sets of data The most efficient way to deal with this situation is to establish a looping structure in the algorithm that will cause the processing logic to be repeated a number of times The DOWHILE structure will continue to repeat a group of statements while a condition remains true 5

Repetition Using the DOWHILE Structure There are two important considerations about which you must be aware before designing a DOWHILE loop First, the testing of the condition is at the beginning of the loop Second, the only way to terminate the loop is to render the DOWHILE condition false 5

Example 5.1 Fahrenheit-Celsius Conversion A program is to be written which will accept each Fahrenheit temperature, convert it to Celsius, and display the converted temperature to the screen. After 15 temperatures have been processed, the words ‘All temperatures processed’ are to be displayed on the screen A Defining diagram (see figure at top of page 52 of the textbook) The defining diagram still only lists what needs to be done; the equation to convert the temperature will not need to be known until the algorithm is developed 5

Example 5.1 Fahrenheit-Celsius Conversion Having defined the input, output, and processing, you are ready to outline a solution to the problem In this example you need: –A DOWHILE structure to repeat the necessary processing –A counter, initialized at zero, which will control the fifteen repetitions B Solution algorithm This solution algorithm could also have been expressed using the keywords WHILE…DO and ENDWHILE 5

Example 5.1 Fahrenheit-Celsius Conversion Fahrenheit_Celsius_conversion Set temperature_count to zero DOWHILE temperature_count <15 Prompt operator for f_temp Get f_temp Compute c_tempt = (f_temp - 32) * 5/9 Display c_tempt Add 1 to temperature_count ENDDO Display ‘All temperatures processed’ to the screen END 5

Example 5.1 Fahrenheit-Celsius Conversion C Desk checking Although the program will require 15 records to process properly, it is still only necessary to check the algorithm at this stage with two valid sets of data (i) Input data: (see first figure on page 53 of the textbook) (ii) Expected results: (see second figure on page 53 of the textbook) 5

Example 5.1 Fahrenheit-Celsius Conversion (iii) Desk check table Desk checking this algorithm shows the exact processing of a DOWHILE loop There is some initial processing (first statement), which will be executed only once Then the DOWHILE condition is tested and found to be true 5

Repetition Using the Repeat…Until Structure The REPEAT…UNTIL structure is similar to the DOWHILE structure, in that a group of statements are repeated in accordance with a specified condition However, where the DOWHILE structure tests the condition at the beginning of the loop, a REPEAT…UNTIL structure tests the condition at the end of the loop 5

Repetition Using the Repeat…Until Structure The format of the REPEAT…UNTIL structure is: REPEAT statement “ UNTIL condition is true The REPEAT…UNTIL is a trailing decision loop; the statements are executed once before the condition is tested The statements within a REPEAT…UNTIL structure will always be executed at least once 5

Counted Repetition Constructs Counted repetition occurs when the exact number of loop iterations is known in advance The execution of the loop is controlled by a loop index, and instead of using DOWHILE, or REPEAT…UNTIL, the simple keyword DO is used as follows: DO loop_index = initial_value to final_value statement block ENDDO 5

Counted Repetition Constructs The DO loop does more than just repeat the statement block. It will: 1.Initialize the loop_index to the required initial_value 2.Increment the loop_index by 1 for each pass through the loop 3.Test the value of the loop_index at the beginning of each loop to ensure that it is within the stated range of values 4.Terminate the loop when the loop_index has exceeded the specified final_value 5

Summary This chapter covered the repetition control structure in detail Descriptions and pseudocode examples were given for DOWHILE, REPEAT…UNTIL, and counted repetition loops Most of the solution algorithms had the same general pattern 5

Summary This pattern consisted of: 1.Some initial processing before the loop 2.Some processing for each record within the loop 3.Some final processing once the loop has been exited 5