Computer Science Faculty

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.
Program Design Tool. 6 Basic Computer Operations Receive information Put out information Perform arithmetic Assign a value to variable (memory location)
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 5: Control Structures II (Repetition)
Program Design and Development
Chapter 4 Loops and Character Manipulation Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
© 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.
Repetition Control Structures
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand basic loop concepts: ■ pretest loops and post-test loops ■ loop.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
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.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
Programming Logic and Design Fifth Edition, Comprehensive
Programming Logic and Design Sixth Edition Chapter 5 Looping.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 5: Control Structures II (Repetition)
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
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.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Repetition Control Structures Simple Program Design Third Edition A Step-by-Step Approach 5.
CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
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.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
6. Iteration Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 Iteration pass (or iteration)-one.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
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.
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.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Programming Logic and Design Fourth Edition, Introductory Chapter 2 Understanding Structure.
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.
© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.
Topic 4: Looping Statements
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II
Problem Solving and Control Statements: Part 2
Chapter 5: Control Structures II
Programming Logic and Design Fourth Edition, Comprehensive
Chapter 5: Control Structures II
Control Structures II (Repetition)
CiS 260: App Dev I Chapter 4: Control Structures II.
Chapter 5: Control Structures II
Control Structures - Repetition
Chapter 6 Repetition Objectives ❏ To understand basic loop concepts:
Program Design Introduction to Computer Programming By:
Pseudocode algorithms using sequence, selection and repetition
Iteration: Beyond the Basic PERFORM
CHAPTER 4 Iterative Structure.
Chapter 5: Control Structures II (Repetition)
More Loops Topics Counter-Controlled (Definite) Repetition
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
More Loops Topics Counter-Controlled (Definite) Repetition
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

Computer Science Faculty Chapter 4 Repetition control structures M. Bahrul Ulum, M.Kom Informatics Engineering Computer Science Faculty

Objectives To develop algorithm that 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

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

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

Repetition using the DOWHILE structure Leading decision loop DOWHILE construct is a leading decision loop – that is the condition is tested before any statements are executed. Format is: DOWHILE condition p is true statement block ENDDO

Repetition using the DOWHILE structure The following processing takes place: Logical condition p is tested If condition p is found to be true, the statements within the statement block are executed once. The delimiter ENDDO then triggers a return of control to the retesting of condition p If condition p is still true, the statements are executed again, and so the repetition process continues until the condition is found to be false If condition p is found to be false, no further processing takes place within this loop

Repetition using the DOWHILE structure There are two important consideration to be aware of before designing a DOWHILE loop Testing of the condition is at the beginning of the loop The only way to terminate the loop is to render the DOWHILE condition false

Repetition using the DOWHILE structure Using DOWHILE to repeat a set of instructions a known number of times When a set of instruction is repeated a specific number of times, a counter can be used in pseudocode, which is initialised before the DOWHILE statement and incremented just before the ENDDO statement

Repetition using the DOWHILE structure Using DOWHILE to repeat a set of instruction an unknown number of times When a trailer record or sentinel exists Special record or value placed at the end of valid data to signify the end of that data When a trailer record or sentinel does not exist End-of-file (EOF) marker is added when the file is created as the last character in the file

Repetition using the REPEAT…UNTIL structure Trailing decision loop REPEAT…UNTIL structure is similar to the DOWHILE structure (group of statements are repeated in accordance with specific condition) REPEAT…UNTIL structure tests condition at the end of the loop This means that the statement within the loop will be executed once before the condition is tested

Repetition using the REPEAT…UNTIL structure If the condition is false, the statement will then be repeated UNTIL the condition becomes true REPEAT…UNTIL is a trailing decision loop (the statement are executed once before the condition is tested) Two consideration to be aware of when using REPEAT…UNTIL Loops are executed when the condition is false This structure will always be executed at least once

Counted repetition Counted loop Counted repetition occurs only once when the exact number of loop iterations is known in advance Simple keyword DO is use as follows: DO loop_index = initial_value to final_value statement block ENDDO

Counted repetition The DO loop does more than repeat the statement block. Initialise the loop_index to the required intial_value Increment the loop_index by 1 for each pass through the loop Test the value of loop_index at the beginning of each loop to ensure that it is within the stated range of values Terminate the loop when the loop_index has exceeded the specified final_value

Counted repetition In other words, a counted repetition construct will perform the initialising, incrementing and testing of the loop counter automatically It will also terminate the loop once the require number of repetition has been executed

Summary This chapter covered the repetition control structure in detail Description and pseudocode were given for: Leading decision loops (DOWHILLE) Trailing decision loops (REPEAT…UNTIL) Counted loops (DO)

Summary Most of the solution algorithms that used the DOWHILE structure had the same general pattern. This pattern consisted of: some initial processing before the loop some processing for each record within the loop some final processing once the loop has been exited.

Summary Expressed as a solution algorithm, this basic pattern was developed as a general solution: Process_sequential_file Initial processing Read first record DOWHILE more records exist Process this record Read next record ENDDO Final processing END

Reference Simple Design Program: A Step by Step Approach, Lesley Anne Robertson