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)

Slides:



Advertisements
Similar presentations
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Advertisements

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 Repetition In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
True or false A variable of type char can hold the value 301. ( F )
Computer Science 1620 Loops.
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.
Starting Out with C++, 3 rd Edition 1 Chapter 5. Looping.
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.
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
Announcements The first graded lab will be posted Sunday 2/15 and due Friday 2/27 at midnight It is brand new, so please don’t hand in last semester’s.
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.
Pseudocode and Algorithms
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
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.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Looping Exercises Deciding Which Loop to Use At this.
Chapter 4: Looping. Resource: Starting Out with C++, Third Edition, Tony Gaddis 5.1 The Increment and Decrement Operators ++ and -- are operators that.
Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Fundamentals of Software Development 1Slide 1 Loops A loop is:A loop is: –a block of code that executes repeatedly while some condition holds true. Java.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Loops Brent M. Dingle Texas A&M University Chapter 7 – part D (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Compunet Corporation1 Programming with Visual Basic.NET While, Do and For – Next Loops Week 5 Tariq Ibn Aziz.
1 Loops. 2 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite)
Lecture 26 Final review. Problem 1 (10 points) How many times would the FOR-DO loops beginning with the following statements be executed? If the loop.
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.
Repetition Control Structures Simple Program Design Third Edition A Step-by-Step Approach 5.
6.2 For…Next Loops General Form of a For…Next Loop
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Introduction to Loops For Loops. Motivation for Using Loops So far, everything we’ve done in MATLAB, you could probably do by hand: Mathematical operations.
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
Loops Brent M. Dingle Texas A&M University Chapter 7 – part B (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4.
Top Down Design Brent M. Dingle Texas A&M University Chapter 4 – Section 1 (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Fundamentals of Software Development 1Slide 1 Loops A loop is:A loop is: –a block of code that executes repeatedly while some condition holds true. Java.
2 nd Semester Looping Technique FOR…DO Module5-1 Looping Technique FOR…DO อภิรักษ์ จันทร์สร้าง Aphirak Jansang
Fall 2001(c)opyright Brent M. Dingle 2001 Simple Sorting Brent M. Dingle Texas A&M University Chapter 10 – Section 1 (and some from Mastering Turbo Pascal.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Pendalaman Materi Conditional dan Repetition (Pengulangan)
Chapter Looping 5. The Increment and Decrement Operators 5.1.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
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#
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
CS 1023 Intro to Engineering Computing 3: Computer Programming Looping Statements Mark Kerstetter Department of Computer Science Western Michigan University.
Loops Brent M. Dingle Texas A&M University Chapter 6 – Section 6.3 Multiway Branches (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Fall 2001(c)opyright Brent M. Dingle 2001 Multidimensional Arrays Brent M. Dingle Texas A&M University Chapter 10 – Section 2, part B (and some from Mastering.
Fall 2001(c)opyright Brent M. Dingle 2001 Abstract Data Types (ADTs) Brent M. Dingle Texas A&M University Chapter 8 – Sections 2 and 3 (and some from Mastering.
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:
(c)opyright Brent M. Dingle 2001
Welcome back to Software Development!
Brent M. Dingle Texas A&M University Chapter 12 – section 1
Module 4 Loops.
Turbo Pascal Units (TPU)
Procedures Brent M. Dingle Texas A&M University
Simple Branches and Loops
(c)opyright Brent M. Dingle 2001
Brent M. Dingle Texas A&M University Chapter 5 – Section 2-3
Complex Array Structures
Brent M. Dingle Texas A&M University Chapter 5 – Section 1
Presentation transcript:

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)

For–To-Do Format FOR [ctrl var] := [init val] TO [final val] DO [statement] Notice, there is no explicit [expr] in a for-loop. Basically for-to-loops just count BY 1 from [init val] to [final val]

For-To-Do Example PROGRAM ForCount; VAR Counter : integer; BEGIN FOR counter := 1 TO 10 DO BEGIN writeln(‘count = ‘, counter); END; END.

For-To-Do Example The above program simply counts from 1 to 10. Notice that the control variable is “automatically” initialized and incremented by one for you. Can you create a while loop that will do exactly the same thing? Try it. And TEST run the two programs to check!

While-loop, for the lazy This while loop will count to 10 also: PROGRAM WhileCount; VAR counter : integer; BEGIN counter := 1; while (counter <= 10) do BEGIN writeln(‘count = ‘, counter); counter := counter + 1; END; END.

Observance Check What do you think would happen if we altered the for line to be: for counter := 10 to 1 do What would have happened in the while loop if you initialized counter := 10 and said: while (counter <= 1) do Try it and see if you answered correctly.

For the lazy, Here are the two altered progs PROGRAM ForCount; VAR counter : integer; BEGIN FOR counter := 10 TO 1 DO BEGIN writeln(‘count = ‘, counter); END; END. PROGRAM WhileCount; VAR counter : integer; BEGIN counter := 10; while (counter <= 1) do BEGIN writeln(‘count = ‘, counter); counter := counter + 1; END; END.

Observance Check - Answer For those of you too lazy to try entering the programs yourself, you would have discovered that ‘nothing’ happens. More specifically, if you start at 10 can you count to 1 by ADDING 1 (repeatedly) to 10? Exactly. Since 10 is already >= 1 neither the for- to-do loop nor the while loop would ever execute. So can you use a for-loop to count backwards? Yes, see the next example =)

Another Example For-Downto-Do PROGRAM ForCount; VAR Counter : integer; BEGIN FOR counter := 10 DOWNTO 1 DO BEGIN writeln(‘count = ‘, counter); END; END.

For-Downto-Example Notice the above program counts from 10 down to 1. Notice the control variable is automatically initialized to 10. Notice also the control variable is automatically decremented by 1 (i.e. one is subtracted from it).

Challenge Can you write a program using a for loop to display the capital characters A to Z on the screen? Could you write it using a while-do loop? Recall that succ() allowed you see that Pascal “knows” the character after A is B, and after B is C, and so on. Notice in a for-loop you will NOT need to use the function succ().

Challenge Answer PROGRAM ForAlpha; VAR letter : char; BEGIN FOR letter := ‘A’ TO ‘Z’ DO BEGIN write(letter); END; writeln; END. PROGRAM WhileAlpha; VAR letter : char; BEGIN letter := ‘A’; while (letter <= ‘Z’) do BEGIN write(letter); letter := succ(letter); END; writeln; END.

Challenge two Can you write a repeat-until loop that does the same as the above two loops? Or rather, write a repeat-until loop which displays the characters A to Z using the function succ().

Challenge Two Answer You think you get all the answers for free? Go try that one on your own. =)

NEVER in a for-loop Never increment the control variable of a for-loop inside the loop. Never write a for-loop like this: FOR count := 1 to 10 DO BEGIN writeln(count); count := count + 1; { BAD LINE !!! } END;

When to use WHILE If you know there is a condition where the loop should never execute, not even once, a while loop would be a good choice. While loops also can be used to count by increments other than +1 or –1. Point  some while loops NEVER execute.

When to use REPEAT If you know that a loop while ALWAYS need to execute at least once then a repeat loop might be a good choice. Point  repeat loops ALWAYS execute AT LEAST ONCE.

When to use FOR If it is known in advance exactly how many times a loop should be executed (or it is known that there will be a specific number of times entered or calculated) then a for loop is a good choice. i.e. if you know you will do something 10 times, or N times, a for loop could be a good choice. Note  some for loops NEVER execute.

Suggested Problems (not graded) page 244, 245  1, 2, 3, 4, 6, 7, 8, 9, 11

End part C