Statement Level Flow of Control Iteration Structures Copyright © 2003-2014 by Curt Hill.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Statement-Level Control Structures
Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Control Flow-II: Execution Order.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
CS 355 – PROGRAMMING LANGUAGES Dr. X. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements Iterative.
(8.1) COEN Control Structures  Control structure general issues  Compound statements  Selectors (conditional structures) – single – two-way –
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Copyright © by Curt Hill Expressions and Assignment Statements Part 2.
Gary MarsdenSlide 1University of Cape Town Statements & Expressions Gary Marsden Semester 2 – 2000.
ISBN Chapter 8 Statement-Level Control Structures.
PZ07B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ07B - Basic statements Programming Language Design.
ISBN Chapter 8 Statement-Level Control Structures.
ISBN Chapter 8 Statement-Level Control Structures.
Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.
Chapter 5: Control Structures II (Repetition)
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Statement-Level Control Structures Sections 1-4
ISBN Chapter 8 Statement-Level Control Structures.
1 Chapter 8 Statement-Level Control Structures In Chapter 7, the flow of control within expressions, which is governed by operator associativity and precedence.
ISBN Lecture 08 Statement-Level Control Structures.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
COMP4730/2002/lec8/H.Melikian Statement-Level Control Structures Introduction Compound Statements Selection Statements Iterative Statements Unconditional.
Statement Level Flow of Control Selection Structures Copyright © by Curt Hill.
ISBN Chapter 8 Statement-Level Control Structures Sections 1-4.
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
PLLab, NTHU,Cs2403 Programming Languages Expression and control structure Kun-Yuan Hsieh Programming Language Lab., NTHU.
Control Structures Programs have 4 basic control structures:
Chapter 8 Chapter 8 Control Structures. Control Structures  A control structure is a control statement and the statements whose execution it controls.
ISBN Chapter 8 Statement-Level Control Structures.
1 CS Programming Languages Class 11 September 26, 2000.
sequence of execution of high-level statements
第八章 敘述層級的控制結構 (Statement-Level Control Structures)
CSI 3120, Control, page 1 Control statements Simple statements Basic structured statements Sequence Selection Iteration The jump statement.
Chapter 8: Statement-Level Control Structures
Control Structures sequence of execution of high-level statements.
8-1 Statement-Level Control Structures Introduction Selection Statements Iterative Statements Unconditional Branching Guarded Commands Conclusions.
April 16, ICE 1341 – Programming Languages (Lecture #14) In-Young Ko Programming Languages (ICE 1341) Lecture #14 Programming Languages (ICE 1341)
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 7 Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program statements.
1 Iterative Statements Repeated execution of a (compound) statement by iteration or recursion –Iteration is statement level –Recursion is unit-level control.
Statement Level Flow of Control GOTOs and Other Weirdness Copyright © by Curt Hill.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Chapter 8 © 2002 by Addison Wesley Longman, Inc Introduction - Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program.
W E E K F I V E Statement-Level Control Structures.
W E E K F I V E Control Flow. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements Iterative Statements.
JavaScript and Ajax (Control Structures) Week 4 Web site:
Copyright © Curt Hill Flow of Control A Quick Overview.
Chapter 8 Statement-Level Control Structures. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
The for Statement A most versatile loop
REPETITION CONTROL STRUCTURE
Def: A control structure is a control statement and
8.1 Introduction - Levels of Control Flow: 1. Within expressions
Dr. Vamsi Paruchuri University of Central Arkansas
Statement-Level Control Structures
Chapter 8: Control Structures
Doing things more than once
Control statements Simple statements Basic structured statements
Control Structures In Text: Chapter 8.
Control Structures Programs utilize 4 basic control structures
Statement-Level Control Structures
PowerShell Flow of Control Copyright © 2016 – Curt Hill.
Chapter8: Statement-Level Control Structures April 9, 2019
Chapter 8: Statement Level Control Structures
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
PZ07B - Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Presentation transcript:

Statement Level Flow of Control Iteration Structures Copyright © by Curt Hill

Looping statements Iteration is fundamental Repeating a statement or statements is present in all languages –Even Ada Lovlace wrote a loop in the nineteenth century –Plankakul also had a loop The use of loops is a large amount of the power of the computers Copyright © by Curt Hill

Looping The early loops were often connected to processing array data, since early programming was mostly numerical Just because the while is the most general looping construct does not mean that it is desirable to have only it –Writeability is improved by several loops Copyright © by Curt Hill

Design issues How is the iteration controlled? –Counting, Boolean testing or combination Where shall the test and exit mechanism be? Two separate considerations –Physical placement of the mechanism This is generally negligible –Logical placement before the loop body or after loop body Copyright © by Curt Hill

Location Tests may be: Before –Pretest or leading decision After –Posttest or trailing decision Anywhere in the middle Copyright © by Curt Hill

Counter controlled This is the classic array manipulation loop It is less general than a boolean controlled loop but very handy There is a control variable, usually integer –It is given an initial value and tested against a terminal value –There may be a step size value to allow counting by other than 1 –These are the loop parameters Supported by machine instructions Copyright © by Curt Hill

Design issues What is the type of the control variable? What is the scope of the control variable? May the loop parameters be changed in the loop? How many times should the loop parameters be evaluated Copyright © by Curt Hill

The DO of FORTRAN I-IV Posttest DO label variable = initial, terminal [,step] Support for this in the hardware Only posttest count controlled loop Parameters must be unsigned constants or positive integers, not expressions Copyright © by Curt Hill

FORTRAN 77 and 90 Loop retains same form, but becomes leading decision The type of the parameters is enlarged to that of integer or real The parameters may now be expressions as well They are evaluated once at the beginning of the loop to produce the iteration count, the number of times to loop When loop terminates it has its most recent value Copyright © by Curt Hill

FORTRAN 90 F90 also introduces an END DO option instead of statement label Control variable only allows integer Copyright © by Curt Hill

ALGOL 60 This is one of the most complicated loops because it tried to do it all Count controlled List controlled Boolean controlled –Quit when true –Quit when false Stuff before the body Stuff after the body –See the book for some examples It evaluated everything every time The control variable was declared external to the loop Copyright © by Curt Hill

COBOL Perform varying COBOL has a weird and delightful loop for subscripting through arrays that may be at most 3 dimensions The Perform is both a pseudo function call and loop –Several forms of it See example on next screen Copyright © by Curt Hill

COBOL Perform Copyright © by Curt Hill Perform ABC Varying ndx From 1 by q Until ndx = 50 After sub from 10 by 2 Until sub > 30 After N from 12 by -1 Until sub < 2.

The Pascal for A delightfully simple pretest count controlled loop Control variable is any ordinal type Increment can only be 1 or -1 Loop variable is declared externally Loop variable is undefined at the end of the loop Parameters are only evaluated once Assignment to the control variable in the loop is illegal Copyright © by Curt Hill

Ada for Somewhat similar to Pascal in concept but different in form for var in [reverse] range loop … end loop for x in 1..A loop … end loop The control variable is declared in the loop and assumes the same type as the range No assignment is allowed on the control variable Copyright © by Curt Hill

C family for Not actually count controlled loops since the test does not have to match the initialization or step Very powerful and occasionally unruly Book has a nice example using the comma operator Java the test must be boolean C++ and Java restrict any defined variables to the loop body Copyright © by Curt Hill

Logically controlled loops Design issues –Pretest or posttest? –Separate loop or special form of counting loop? Examples –There are several languages that have both a pretest and posttest loop: C, C++, Java, Pascal –FORTRAN 77 & 90 had no pretest or posttest logical loop –Ada has only a pretest Copyright © by Curt Hill

User located test and exits The programmer may choose locations other than beginning and end –Modula-2 and Ada have a loop statement which is infinite –There is a conditional exit statement that can be put anywhere including multiple sites –C, C++, Java have the break which can accomplish the same thing Which is a goto to first statement following loop –C and C++ also have a continue which is exit the current iteration but stay in loop Copyright © by Curt Hill

Iteration based on data structures Several languages allow a loop similar to a counting loop but based on something other than an integer –LISP has a dolist function that executes a piece of code for each item on the list –Perl and Lambda MOO have a similar construct that does something for each item on the list This has become popular enough that Java (5) and C++ (2011) have added Copyright © by Curt Hill

Examples Copyright © by Curt Hill Lambda MOO: for x in (list)... endfor Java (5 and after) : Picture p … ; Pixel [] px = p.getPixels(); for(Pixel ap: px){ // process ap }

Ada Copyright © by Curt Hill subtype MyRange is Integer range 0.99; MyArray: array (MyRange) of Integer; for Index in MyRange loop...MyArray(Index)... end loop; -- Really a Boolean loop

Finally The next presentation will consider is the forms of the GOTO and other control flow oddities Copyright © by Curt Hill