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)

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

Objectives In this chapter, you will learn about:
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Week 7 - Programming II Today – more features: – Loop control – Extending if/else – Nesting of loops Debugging tools Textbook chapter 7, pages
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
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.
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.
Programming Logic and Design Fifth Edition, Comprehensive
Chapter 5 Loops.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Programming Logic and Design Sixth Edition Chapter 5 Looping.
1 Loops. 2 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite)
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
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.
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.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
CS101 Computer Programming I Chapter 4 Extra Examples.
Chapter 5: Structured Programming
Control Structures Repetition or Iteration or Looping Part II.
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)
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Pascal Programming Pascal Loops and Debugging. Pascal Programming Pascal Loops In our first brush with the while do loops, simple comparisons were used.
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Count Controlled Loops (Nested) Ain’t no sunshine when she’s gone …
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.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
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)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
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 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.
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.
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.
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Brent M. Dingle Texas A&M University Chapter 6, Sections 1 and 2
C++ Programming: CS150 For.
Loop Structures.
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Chapter 5: Repetition Structures
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
While Loops Chapter 3.
Control Structures - Repetition
Week 8 - Programming II Today – more features: Loop control
Control Structure Senior Lecturer
Introduction to pseudocode
Brent M. Dingle Texas A&M University Chapter 12 – section 1
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Chapter 2.1 Repetition.
Chapter 6: Repetition Statements
Turbo Pascal Units (TPU)
Procedures Brent M. Dingle Texas A&M University
3.1 Iteration Loops For … To … Next 18/01/2019.
Let’s all Repeat Together
Flowcharts and Pseudo Code
Programming Right from the Start with Visual Basic .NET 1/e
Simple Branches and Loops
Complex Array Structures
Presentation transcript:

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)

Using loops for input Loops are often used to do input – for example a while NOT (eof (infile) ). Or while NOT (all stuff input from user). With this idea comes the idea that input is just a list of data (a list of names, a list of numbers, a list of some combination of both, etc). If the input is a list, how might a loop be used? How would it know when it was at the end of the list?

Methods to terminate an INPUT loop. List headed by size The list begins with a number indicating how long the list is. Ask before iterating (repeating) After every input the user is asked if they want to continue inputting stuff. List ended with a sentinel value. Some impossible value is put at the end of the list. For example if the list is of colors use BOB as a sentinel value, as BOB is not a valid color, the program would know there was no more input. Running out of input. There simply is no more input – eof is an easy example of this method.

Non-Input Loops Not all loops are input loops. Since we have terminating methods for input-loops, we must have methods for terminating non-input loops. These lists of methods tend to make good multiple choice questions.

Methods to terminate NON-input loops Loop is count controlled. There is an initial value and a final value. Ask before iterating The user is asked if they want the program to loop during the course of each loop. Exit on sentinel value Somehow an impossible value was encountered (usually calculated at runtime, but defined somewhere else). Running out of data (“input”) There is no more data to perform the operations of the loop on. Exit on a flag condition. Some variable (a flag) is automatically set within the loop that indicates the status of whether the loop should continue. When the variable’s value says ‘stop’ then the loop ends.

Nested Loops Nested loops are loops containing loops. Often these are used to create tables (as tables have rows and columns).

Nested Loops – Example PROGRAM MultTab; VAR row, col, prod : integer; BEGIN writeln(‘Multiplication Table’); writeln (‘ * ’); FOR row := 0 to 10 DO Begin write(row:4); FOR col := 0 to 5 DO begin prod := row * col; write(prod:4); end; { for col } writeln; End; { for row } END.

Nested Loops (cont) Notice the output of the above program has 11 rows and only 6 columns (recall we started with zero). Alter the program so it has 10 rows AND 10 columns. I would STRONGLY encourage you to figure out how nested loops work.

Definitions Assertions Comments such as preconditions (what was true before a loop) and postconditions (what is true after a loop) are called assertions. We are asserting that such things are true. Invariant A loop invariant or invariant is an assertion that is true before a loop and true after a loop. Variant expression An expression for a quantity (or a variable) that changes each time a loop is executed is called a variant expression.

More Problems (not graded) page 265  13, 14 page 271 – 273  16, 17, 18, 24, 26 and maybe 21

End part D And so ends Chapter 7