Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4.

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
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.
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
James Tam Repetition In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
Computer Science 1620 Loops.
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
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
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Do … while ( continue_cond ) Syntax: do { stuff you want to happen in the loop } while (continue_condition);
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
Programming with C# Iteration LECTURE 3. Summary of last lecture SequenceSelectionif and switch statementsCastingRandom.
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.
CIS 234: LOOPS Adapted from materials by Dr. Donald Bell, 2000 (updated April 2007)
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
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.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Chapter 4: Control Structures II
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Pascal Programming Pascal Loops and Debugging. Pascal Programming Pascal Loops In our first brush with the while do loops, simple comparisons were used.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
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.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
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.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Loops Brent M. Dingle Texas A&M University Chapter 7 – part C (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
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 3 of Programming Languages by Ravi Sethi
CS161 Introduction to Computer Science
CiS 260: App Dev I Chapter 4: Control Structures II.
Java Programming: Guided Learning with Early Objects
Ch 7: JavaScript Control Statements I.
JavaScript: Control Statements.
Arrays, For loop While loop Do while loop
Chapter 6: Repetition Statements
More Loops Topics Counter-Controlled (Definite) Repetition
PROGRAM FLOWCHART Iteration Statements.
Conditional Loops Counted Loops
More Loops Topics Counter-Controlled (Definite) Repetition
Chap 7. Advanced Control Statements in Java
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4

Definite Loops Iteration is commonly referred to as looping and describes the situation where an action, or number of actions, are repeated a number of times. Sometimes it is known in advance exactly how many repetitions are needed (a DEFINITE loop) e.g. turn the wing nut anti clockwise 5 times to unlock for each person in a family of 4 place plate on table place knife on right side of plate place fork on left side of plate

The FOR statement in Pascal The FOR statement is used for definite looping such as:- FOR control variable := start value TO end value DO BEGIN statements to be repeated (known as the loop body) END;

FOR Statement 1… The control variable can be of any ordinal type i.e. Integer Char Boolean (True or False) (Note when using FOR loops this control variable needs adding to the list of variables.)

FOR Statement 2… When the FOR loop executes, this variable is set to the value specified in start value. After each execution of the loop body the control variable is incremented by 1 and the loop body is executed again until the control variable reaches a value that is greater than the end value specified. Execution will then continue at the statement following the END clause. Hence the control variable acts as a counter.

FOR Statement 3… Note: The start value and end value can be specified as a literal value, a variable name or an expression e.g. FOR counter := 3 TO (x*y) NOTE: The start value does not have to be the value 1 NOTE: If the end value is not greater than the start value the loop body will not be executed.

Sample code FOR counter := 1 TO max DO BEGIN writeln (counter); writeln (counter * counter) END; Note: max will need defining in your list or variables

Comment In the previous example the control variable is used in the body of the loop. This is a very useful programming practice, but do not attempt to change the value of this variable as this will affect the number of times the loop will execute. Notice also that there is NO semicolon on the final statement in the loop body before the END statement.

The DownTo clause In FOR loops, unlike many other languages the control variable can only be incremented by 1. However the value can be decremented by using the DOWNTO clause in place of TO e.g. FOR letter 'z' DOWNTO 'a' DO write(letter);

Indefinite Loops For many applications it is not known beforehand how many times a loop may execute. The loop will terminate when some condition has been met. This type of loop is an indefinite loop. There are two implementations of indefinite loops in Pascal: The WHILE statement The REPEAT statement. Consider the WHILE statement.

The WHILE statement WHILE test condition DO BEGIN statements for the body of the loop END; The WHILE loop repeats the loop body zero or more times as long as the test condition remains true. This is also known as a “preconditioned loop” as the condition is tested before the loop body is executed. This means that if the condition is false the first time, the loop body is not executed at all. The loop body can be a single statement or a statement block. The BEGIN and END statements are not needed for a single statement. NOTE if the test condition remains true then the loop will execute indefinitely and the program will ‘hang’

While Example ‘While’ is often used in File Handling while not Eof(InFile) do begin ReadLn(InFile, Line); WriteLn(OutFile, Line); Inc(LineCount); end;

REPEAT statement The statements between repeat and until are executed in sequence until, at the end of a sequence, the Boolean expression is True. Syntax: repeat statement;... statement until expression

Repeat Examples NOTE: The sequence is executed at least once. Example1: { Repeat Statements } repeat Ch := GetChar until Ch <> ' '; Example 2: repeat Write('Enter value: '); ReadLn(I); until (I >= 0) and (I <= '9');

FOR, REPEAT or WHILE? FOR should be used if the loop is to be executed a predetermined number of times As a general rule While should be used when the test is done before the loop is entered. E.g. when reading data from a file that may be empty (test for EOF) Repeat loops always execute at least once and should be used when the control check is tested on ‘exit’ i.e. repeat UNTIL something happens

Loops with Arrays Loops allow programs to address individual elements in sequence, using the index (i) E.g. For i:= 1 to 7 DO

Summary We looked at:- Definition of ‘Iteration’ For Loops While Loops Repeat Loops Choosing the correct loop for the problem Using Loops with Arrays