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)

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

1 Data Type Anan Phonphoem Data Type Ordinal TypeNon-ordinal Type StandardUser Define Enumerated Subrange Integer Char Boolean Real.
Chapter 2.5 Modula 2 Control Instructions. Modula 2 Control Statements Selection statements –BOOLEAN Selector : IF statement –Ordinal Selector : CASE.
Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:
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.
1 Loops. 2 Often we want to execute a block of code multiple times. Something is always different each time through the block. Typically a variable is.
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.
J. Michael Moore Structured Programming CPSC 110 Drawn from James Tam's material.
J. Michael Moore Other Control Structures CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 3, Lecture 2.
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.
Program Design and Development
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.
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
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.
16/27/ :53 PM6/27/ :53 PM6/27/ :53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +,
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
FOR DOWNTO Suppose you want to write a FOR-DO loop where the control variable is decreased with each repetition of the loop. Pascal provides the reserved.
Fundamentals of Python: From First Programs Through Data Structures
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
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.
True or False: Boolean Expression Boolean expression are expressions which evaluate to "true" or "false“ Primary operators to combine expressions: && (and),
STRINGS CMSC 201 – Lab 3. Overview Objectives for today's lab:  Obtain experience using strings in Python, including looping over characters in strings.
Recursion A method is recursive if it makes a call to itself. A method is recursive if it makes a call to itself. For example: For example: public void.
Lecture 13 Midterm overview When you know a thing, to hold that you know it, and when you do not know a thing, to allow that you do not know it: this is.
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)
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Lecture 9 There are ain’t no way to find out why a snorer can’t hear himself snore Mark Twain, Western koan.
CONTENTS Processing structures and commands Control structures – Sequence Sequence – Selection Selection – Iteration Iteration Naming conventions – File.
Repetition Control Structures Simple Program Design Third Edition A Step-by-Step Approach 5.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 1 (Monday)
Repetition and Iteration ANSI-C. Repetition We need a control instruction to allows us to execute an statement or a set of statements as many times as.
1 Ordinal types An ordinal data type is an ordered set in which every element, except the first element, has an immediate predecessor, and every element,
Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4.
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
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 © 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.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
1 CSC103: Introduction to Computer and Programming Lecture No 9.
Learning Javascript From Mr Saem
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)
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:
Week 4 – Repetition Structures / Loops
Chapter 4: Making Decisions.
(c)opyright Brent M. Dingle 2001
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Loops October 10, 2017.
Iteration with While You can say that again.
Brent M. Dingle Texas A&M University Chapter 12 – section 1
Module 4 Loops.
Procedures Brent M. Dingle Texas A&M University
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Computer Science 1 For..do loop Dry Run Take notes on the For loop
Simple Branches and Loops
(c)opyright Brent M. Dingle 2001
Python While Loops.
Jump & Loop instructions
Complex Array Structures
Presentation transcript:

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)

Repeat Until Format REPEAT [statement] UNTIL [expr] Notice, [expr] works differently here than in a WHILE-DO loop. While the boolean expression [expr] is FALSE the computer will repeatedly perform [statement]. So be sure that [expr] will eventually become TRUE – else you will be in an infinite loop. Note: the exit condition of the repeat-until loop is the condition that makes [expr] TRUE.

Repeat – Until (cont) Every REPEAT-UNTIL loop executes at least once. The value of [expr] is not checked until the statement has been executed. Notice also a REPEAT-UNTIL does NOT use a BEGIN-END pair for a compound statement.

REPEAT-UNTIL Example PROGRAM UntilCount; VAR count : integer; BEGIN count := 10; REPEAT writeln(count); count := count – 1; UNTIL (count = 0); END.

REPEAT-UNTIL Example The previous example will output the numbers 10 down to 1 on the screen. Something to think about is what value does count have after the loop ends?

REPEAT-UNTIL Second Example PROGRAM UntilEx2; VAR count, c2 : integer; BEGIN count := 435; REPEAT writeln(‘Howdy’); count = count + 1; UNTIL (count > 10); END.

REPEAT-UNTIL Second Example How many times will the above loop execute? How many times is Howdy written to the screen? If you initialized count := 1, what would the answers to the above questions be? If you initialized count := 5, what would the answers be?

New Functions succ = successor (that which comes after) pred = predecessor (that which comes before) These functions are discussed in 8.3, but are useful for doing cool things with loops.

succ The function succ(x) returns the value that comes AFTER whatever value x currently has. For example: succ( 1 ) returns 2 succ( 28 ) returns 29 succ( ‘A’ ) returns ‘B’ succ( ‘d’ ) returns ‘e’ You may only send ordinal data types to succ(). Ordinal data types are things that have a definite finite order – like integers, or letters in the alphabet. Real numbers are not ordinal, after 1.0 comes what? 1.1? 1.01? 1.001? ? … -> no answer.

pred The function pred(x) returns the value that comes BEFORE whatever value x currently has. For example: pred( 1 ) returns 0 pred( 28 ) returns 27 pred( ‘A’ ) returns hmmm… you go check that out. pred( ‘d’ ) returns ‘c’ You may only send ordinal data types to pred().

REPEAT-UNTIL Third Example PROGRAM RepeatAlpha; VAR ch : char; BEGIN ch := ‘Z’; REPEAT write(ch); ch := pred(ch); UNTIL ( ch < ‘A’ ); writeln; END.

REPEAT-UNTIL Third Example The above example will output the alphabet (uppercase) backwards on the screen. Try altering the program so it will output it in lowercase. Try altering it (use an if statement?) to make it output uppercase z to a and then lowercase Z to A.

ASCII character codes Find out what ASCII character codes are. What does the function ord() do? How about chr()? Go find out ! Impress your friends ! =)

End part B