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.

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.
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Repeating Actions While and For Loops
James Tam Repetition In Pascal In this section of notes you will learn how to have a section of code repeated without duplicating the code.
James Tam Loops In Python In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
J. Michael Moore Structured Programming CPSC 110 Drawn from James Tam's material.
James Tam Loops In Python In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
J. Michael Moore Other Control Structures CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
James Tam Loops In Python In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
J. Michael Moore Structured Programming CSCE 110 Drawn from James Tam's material.
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate your code.
J. Michael Moore Other Control Structures CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
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.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
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.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 5: Control Structures II
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Fundamental Programming Fundamental Programming for Loops.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
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.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
James Tam Loops In Python In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Topic 4: Looping Statements
REPETITION CONTROL STRUCTURE
Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code. Repetition in Pascal:
The switch Statement, and Introduction to Looping
Chapter 5: Repetition Structures
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Iteration with While You can say that again.
Outline Altering flow of control Boolean expressions
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Chapter 8: More on the Repetition Structure
Three Special Structures – Case, Do While, and Do Until
Chapter 6: Repetition Statements
Loops In Python In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

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.

James Tam The Need For Repetition Writing out a simple counting program (1 – 3). The full text-only program can be found in Unix under /home/231/examples/repetition/counting.p: program counting (output); begin writeln('1'); writeln('2'); writeln('3'); end.

James Tam The Need For Repetition (2) Simple program but what if changes need to be made? Need to re-edit source code and re-compile program? What if you need to count many times? This example indicates the need for a mechanism to allow for parts of the program to repeat an arbitrary number of times (loops).

James Tam Basic Structure Of Loops 1)Initialize the control a)Control – typically a variable that determines whether or not the loop executes or not. 2)Testing the control against a condition 3)Executing the body of the loop 4)Update the value of the control

James Tam Types Of Loops Pre-test loops 1.Initialize control 2.Check if a condition is met (using the control in some Boolean expression) a)If the condition has been met then continue on with the loop (go to step 3) b)If the condition hasn't been met then break out of the loop (loop ends) 3.Execute the body of the loop 4.Update the value of the control 5.Repeat step 2 General characteristics The body of the loop executes zero or more times Execute body only if condition is true Examples: while-do, for

James Tam Types Of Loops (2) Post-test loops 1.Initialize control 2.Execute the body of the loop 3.Update the value of the control 4.Check if a condition is met (using the control in some Boolean expression) a)If the condition has been met then break out of loop (loop ends) b)If the condition hasn't been met then continue on with loop (go to step 5) 5.Repeat step 2 General characteristics The body of the loop executes one or more times Execute body only if condition is false Examples: repeat-until

James Tam Pre-Test Loop: Flowchart (While-do, For) Condition met? Initialize control Body of loop Y Statements after the loop N Update value of control

James Tam Post-Test Loop: Flowchart (Repeat-Until) Initialize control Body of loop N Statements after the loop Y Update value of control Condition met?

James Tam Pre-Test Loop: While-Do Can be used if the number of times that the loop executes is not known in advance. Syntax: while (Boolean expression) do body Example (Full text-only version can be found in Unix under /home/231/examples/repetition/whileDo.p) i: = 1; while (i <= 5) do begin writeln('i = ', i); i := i + 1; end; (* while *) Initialize controlCheck condition Execute body Update control

James Tam Tracing The While Loop Variables Execution i./a.out

James Tam Pre-Test Loop: For Typically used when it is known in advance how many times that the loop will execute (counting loops). Syntax (counting up): for initialize control to final value do body Syntax (counting down): for initialize control downto final value do body

James Tam First For Loop Example Example one ( A compilable text-only version can be found in Unix under /home/231/examples/repetition/forLoopUp.p) var i, total : integer; begin total := 0; for i := 1 to 5 do begin total := total + i; writeln('i=', i, ‘ total=', total); end; (* for *) end. Test condition Update control Execute body Initialize control

James Tam Tracing The First For Loop Example Variables Execution i./a.out total

James Tam Second For Loop Example Example one ( A compilable text-only version can be found in Unix under /home/231/examples/repetition/forLoopDown.p) var i, total : integer; begin total := 0; for i := 5 downto 1 do begin total := total + i; writeln('i=', i, ' total=',total); end; (* for *) end.

James Tam Tracing The Second For Loop Example Variables Execution i./a.out total

James Tam Post Test Loops: Repeat-Until Used instead of a while-do loop if you need the loop to execute at least once. Syntax: repeat body until (Boolean expression);

James Tam Repeat-Until: An Example A compilable version of this example can be found in Unix under: /home/231/examples/repetition/guzzlingGame.p

James Tam Repeat-Until: An Example (2) repeat begin answer := Random(10); write('Enter your guess: '); readln(guess); if (guess = answer) then writeln('You guessed correctly!') else writeln('You guessed incorrectly'); writeln('Number was ', answer, ', your guess was ', guess); write('Play again? Enter "N" or "n" to quit or anything else to '); writeln('continue'); write('Choice: '); readln(choice); writeln; end; until (choice = 'N') OR (choice = 'n'); Initialize control Execute body Update control Test condition

James Tam Infinite Loops Loops that never end (the stopping condition is never met). Infinite loops can be caused by logical errors: The loop control is never updated (Example 1). The updating of the loop control never brings it closer to the stopping condition (Example 2). Example 1 ( a compilable version can be found in Unix under /home/231/examples/repetition/infinite1.p ) i := 1; while (i <=10) do writeln('i=', i); i := i + 1; To stop a program with an infinite loop in Unix simultaneously press the and the keys

James Tam Infinite Loops (2) Example 2 ( a compilable version can be found in Unix under /home/231/examples/repetition/infinite2.p ) i := 10; while (i > 0) do begin writeln('i = ', i); i := i + 1 end; To stop a program with an infinite loop in Unix simultaneously press the and the keys

James Tam Nested Loops One loop executes inside of another loop(s). Example structure: Outer loop (runs n times) Inner loop (runs m times) Body of inner loop (runs n x m times) Example program (complete compilable program can be found in Unix under: /home/231/examples/repetition/nested.p) for i := 1 to 2 do for j := 1 to 3 do writeln('i=', i, ' j=', j); writeln('All done!');

James Tam Summary What is the purpose of loops in a programming language? What are the different types of loops that are employed in Pascal Pre-test: while-do, for Post-test: repeat-until What are nested loops and how do they work