Repetition Control Structure

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
UNIT 3 PROBLEM SOLVING WITH LOOP AND CASE LOGIC STRUCTURE
Program Design Tool. 6 Basic Computer Operations Receive information Put out information Perform arithmetic Assign a value to variable (memory location)
Repeating Actions While and For Loops
TEL 104 / MKK Fundamental Programming: Lecture 3
Nassi-Schneidermann Diagrams Lecture 13. Three basic control Structures.
Steps in Program Development
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
Modularisation II Lecture 9. Communication between modules Also known as intermodule communication. The fewer and the simpler the communications, the.
Array Processing Lecture 7.
Repetition Control Structures
Pseudocode.
Pseudocode Algorithms Using Sequence, Selection, and Repetition.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Pseudocode.
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.
DCT 1123 Problem Solving & Algorithms
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
Combination of Sequence, Selection and Repetition
Pseudocode algorithms using sequence, selection and repetition
DCT 1123 Problem Solving & Algorithms
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.
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
Programming Logic and Design Fifth Edition, Comprehensive
Chapter 7 Array processing. Objectives To introduce arrays and the uses of arrays To develop pseudocode algorithms for common operations on arrays To.
S2008Final_part1.ppt CS11 Introduction to Programming Final Exam Part 1 S A computer is a mechanical or electrical device which stores, retrieves,
Presentation © Copyright 2002, Bryan Meyers Top-Down, Structured Program Design Chapter 5.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
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.
ANALYSIS AND ALGORITHM DESIGN - IV. Repeat statements/looping/counting/iterations It is often necessary to repeat certain parts of a program a number.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Chapter 2 Pseudocode. Objectives To introduce common words, keywords and meaningful names when writing pseudocode To define the three basic control structures.
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
Pseudocode Algorithms Using Sequence, Selection, and Repetition
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Repetition Control Structures Simple Program Design Third Edition A Step-by-Step Approach 5.
Pseudocode Algorithms Using Sequence, Selection, and Repetition Simple Program Design Third Edition A Step-by-Step Approach 6.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
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.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Flowcharts. Symbol Terminal Symbol: indicates the starting or stopping pointin the logic. Input/Output Symbol: Represents an input or output process in.
Flowcharts Lecture 12.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
STEP 3- DEVELOP AN ALGORITHM At this stage we break down the problem into simple manageable steps so that they can be handled easily.
Pendalaman Materi Conditional dan Repetition (Pengulangan)
Solving Problems with Repetition Version 1.0. Objectives At the end of this topic, students should be able to: Correctly use a while statement in a C#
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
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.
PROGRAM CONTROL STRUCTURE
CHAPTER 5A Loop Structure
Ch 7: JavaScript Control Statements I.
Computer Science Faculty
CHAPTER 2 & 3: Pseudocode and Developing and Algorithm
Program Design Introduction to Computer Programming By:
Pseudocode.
Pseudocode algorithms using sequence, selection and repetition
Structured Program
Iteration: Beyond the Basic PERFORM
CHAPTER 4 Iterative Structure.
Introduction to Pseudocode
Presentation transcript:

Repetition Control Structure Lecture 5

Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: Beginning of the loop (leading decision loop) The end of the loop ( trailing decision loop) A counted number of times (counted loop)

Leading Decision Loop DOWHILE condition p is true Statement block ENDDO

Two Important consideration for Decision Loop The testing of the condition is at the beginning of the loop  programmer may need to perform initial processing The only way to terminate the loop is to render the DOWHILE condition false  need to set up some process within the statement block that will change the condition to be false.

Example 5.1 Fahrenheit-Calcius Conversion Every day, a weather station receives 15 temperatures expressed in degrees Fahrenheit. A program is to be written that will accept each Fahrenheit temperature, convert it to Celcius and display the converted temperature to the screen. After 15 temperaturs have been processed, the words „All temperatures processed“ are to be displayed to the screen.

Defining diagram Input Processing Output F_temp (15 temperatures) Get fahrenheit temperatures Convert temperatures Display Celcius temperatures Display screen message C_temp

Solution Algorithm In this example, we need: A DOWHILE structure A counter, called temperature_count initialised to zero

Solution Algorithm (cont) Fahrenheit_Celcius_conversion 1 Set temperature_count to zero 2 DOWHILE temperature_count < 15 3 Promt operator for f_temp 4 Get f_temp 5 Compute c_temp = (f_temp - 32) * 5/9 6 Display c_temp 7 Add 1 to temperature_count ENDDO 8 Display „All temperatures processed“ to the screen END

Desk Checking 1. Input Data Data Set 1 Data Set 2 F_temp 32 50

2. Expected Result Data Set 1 Data Set 2 C_temp 10

3. Desk Check Table Statement number Temperature_count DOWHILE condition F_temp C_temp 1 2 True 3,4 32 5 6 Display 7 50 10

Using DOWHILE to Repeat Unkown Number of Times We cannot use counter Instead, we use : trailer record or sentinel Sentinel : special record or value placed at the end of valid data to signify the end of that data.

Example 5.2 Print examination scores A program is required to read and print a series of names and exams scores for student enrolled in fundamental programming course. The class average is to be computed and printed at the end of the report. Scores can range from 0 to 100. The last record contains a blank name and a score of 999 and is not to be included in the calculations.

Defining diagram Input Processing Output Name Exam_score Read student details Print student details Compute average score Print average score Average_score

We need: A DOWHILE structure An accumulator fo total score An accumulator for total students

Solution Algorithm Print_examination_scores 1 Set total_score to zero 2 Set total_students to zero 3 Read name, exam_score 4 DOWHILE exam_score not = 999 5 Add 1 to total_students 6 Print name, exam_score 7 Add exam_score to total_score 8 Read name, exam_score ENDDO 9 IF total_students not = zero THEN average_score = total_score/total_students Print average_score ENDIF END Priming Read

Desk Checking Record 1 Record 2 Record 3 score 50 100 999 1. Input Data Record 1 Record 2 Record 3 score 50 100 999

Second name and score of 100 Average score 75 2. Expected Result First name and score of 50 Second name and score of 100 Average score 75

3. Desk Check Table Statement number Total_Score Total_ Students Exam_ DOWHILE condition Average_ 1,2 3 50 4 true 5 1 6 print 7 8 100 True 2 Print 150 999 False 9 75

When a trailer record or sentinel does not exist We need to check for EOF (End Of File) Marker EOF marker is added when the file is created as the last character in the file. The check of EOF is in the DOWHILE clause, using one of the following expression: DOWHILE more data DOWHILE more records DOWHILE records exist DOWHILE NOT EOF

Example 5.3 Process Student Enrolments A program is required to read a file of student records, and select and print only those students enrolled in a course unit named Fundamental Programming. Each student record contains student number, name, address, postcode, gender, and course unit number. The course unit number for Fundamental Programming is TEL104. Three totals are to be printed at the end of the report: total females enrolled in the course, total males enrolled in the course, and total students enrolled in the course.

Defining diagram Input Processing Output Student_record Student_no Name Address Postcode Gender Course_unit Read student records Select student records Print selected records Compute total females enrolled Compute total males enrolled Compute total students enrolled Print totals Selected student records Total_females_enrolled Total_males_enrolled Total_students_enrolled

We need: A DOWHILE structure IF statements Accumulator for 3 total fields

Solution Algorithm Process_students_enrolments 1 Set total_females_enrolled to zero 2 Set total_males_enrolled to zero 3 Set total_students_enrolled to zero 4 Read student record 5 DOWHILE record exist 6 IF course_unit = TEL104 THEN print student details increment total_students_enrolled IF student_gender = female THEN increment total_females_enrolled ELSE increment total_males_enrolled ENDIF 7 Read student record ENDDO 8 Print total_females_enrolled 9 Print total_males_enrolled 10 Print total_students_enrolled END

Desk Checking Record 1 Record 2 Record 3 Course unit TIK100 TEL104 1. Input Data Record 1 Record 2 Record 3 Course unit TIK100 TEL104 gender F M

Student number, name, address, postcode, F (2nd student) 2. Expected Result Student number, name, address, postcode, F (2nd student) Student number, name, address, postcode, M (3rd student) Total females enrolled 1 Total males enrolled 1 Total students enrolled 2

3. Desk Check Table Statement number Course_ unit gender DOWHILE condition Total_ Females_ enrolled Males_ Students_ 1,2,3 4 TIK100 F 5 True 6 7 TEL104 print 1 M 2 EOF False 8,9,10

Conclusion Basic pattern for DOWHILE to process sequential file: Initial processing Read first record DOWHILE more record exist Process this record Process next record ENDDO Final processing END

REPEAT...UNTIL STRUCTURE DIFF: DOWHILE test the condition at the beginning of the loop REPEAT..UNTIL  test the condition at the end of the loop REPEAT..UNTIL Structure: REPEAT Statement . UNTIL the condition is true

Consideration of using REPEAT..UNTIL REPEAT..UNTIL is a trailing loop  the statements are excuted before the condition is tested. REPEAT..UNTIL loops are executed when the condition is false. (opposite of DOWHILE) Example: DOWHILE more records equivalent to REPEAT..UNTIL no more records DOWHILE number NOT = 99 equivalent to REPEAT..UNTIL number=99 The statements within a REPEAT..UNTIL structure will always be executed at least once.  no need priming Read, but extra IF statement needed after the READ to prevent the processing the trailer record.

DOWHILE vs REPEAT..UNTIL Process_students_records Set student_count to zero Read student record DOWHILE student_number NOT = 999 Write student record Increment student_count ENDDO Print student_count END

DOWHILE vs REPEAT..UNTIL (cont‘) Process_students_records Set student_count to zero REPEAT Read student record IF student_number NOT = 999 THEN Write student record Increment student_count ENDIF UNTIL student_number = 999 Print student_count END

Example 5.4 Process Inventory Items A program is required to read a series of inventory records that contain item number, item description and stock figure. The last record in the file has an item number of zero. The program is to produce a low stock item report, by printing only those records that have a stock figure of less than 20 items. A heading is to be printed at the top of the report and a total low stock item count printed at the end.

Defining diagram Input Processing Output Inventory_record item_number Item_description Stock_figure Read inventory records Select low stock items Print low stock records Print total low stock items Heading Selected records Total_low_stock_items

We need: A REPEAT..UNTIL structure IF statements to select stock figures < 20 Accumulator for total_low_stock_items Extra IF, within the loop, to ensure the trailer record is not processed

Solution Algorithm Process_inventory_records 1 Set total_low_stock_items to zero 2 Print ‚Low Stock Items‘ heading REPEAT 3 Read inventory record 4 IF item_number > zero THEN IF stock_figure < 20 THEN print item_number, item_description, stock_figure increment total_low_stock_items ENDIF 5 UNTIL item_number = zero 6 Print total_low_stock_items END

Desk Checking Record 1 Record 2 Record 3 Item_number 123 124 1. Input Data Record 1 Record 2 Record 3 Item_number 123 124 Stock_figure 8 25

Low Stock Items 123 8 (first record) Total Low Stock Item = 1 2. Expected Result Low Stock Items 123 8 (first record) Total Low Stock Item = 1

Total_low_stock_items 3. Desk Check Table Statement number Item_ number Stock_ figure REPEAT UNTIL Total_low_stock_items heading 1 2 Print 3 123 8 4 print 5 False 124 25 True 6

Counted Repetition Structure: DO loop_index = initial_value to final_value statement block ENDDO This structure will: Perform the initialising Incrementing and testing the loop counter automatically Terminate th loopwhen the required number of repetition has been executed.

Example 5.5 Fahrenheit-Calcius Conversion Every day, a weather station receives 15 temperatures expressed in degrees Fahrenheit. A program is to be written that will accept each Fahrenheit temperature, convert it to Celcius and display the converted temperature to the screen. After 15 temperaturs have been processed, the words „All temperatures processed“ are to be displayed to the screen.

Defining diagram Input Processing Output F_temp (15 temperatures) Get fahrenheit temperatures Convert temperatures Display Celcius temperatures Display screen message C_temp

We Need: DO loop a loop counter (temperature_count)

Solution Algorithm Fahrenheit_Celcius_conversion 1 DO temperature_count = 1 to 15 2 Promt operator for f_temp 3 Get f_temp 4 Compute c_temp = (f_temp - 32)* 5/9 5 Display c_temp ENDDO 6 Display „All temperatures processed“ to the screen END

Desk Checking 1. Input Data Data Set 1 Data Set 2 F_temp 32 50

2. Expected Result Data Set 1 Data Set 2 C_temp 10

3. Desk Check Table Statement number Temperature_count F_temp C_temp 1 2,3 32 4 5 Display 2 50 10 display