Control Structures sequence of execution of high-level statements.

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:
Chapter 8 Statement-Level Control Structures. 1-2 Chapter 8 Topics Introduction Selection Statements Iterative Statements Unconditional Branching Guarded.
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 –
ISBN Chapter 8 Statement-Level Control Structures.
ICE1341 Programming Languages Spring 2005 Lecture #14 Lecture #14 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Chapter 8 Statement-Level Control Structure. Introduction Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program statements.
ISBN Chapter 8 Statement-Level Control Structures.
ISBN Chapter 8 Statement-Level Control Structures.
ISBN Chapter 8 Statement-Level Control Structures.
Controlling Program Flows
Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.
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.
ISBN Chapter 8 Statement-Level Control Structures.
COMP4730/2002/lec8/H.Melikian Statement-Level Control Structures Introduction Compound Statements Selection Statements Iterative Statements Unconditional.
Statement-Level Control Structures
1 Statement-Level Control Structures Levels of flow control Control Statements 1. Sequence 2. Selection 3. Iteration Unconditional branching Guarded commands.
Structure of Programming Language
ISBN Chapter 8 Statement-Level Control Structures Sections 1-4.
PLLab, NTHU,Cs2403 Programming Languages Expression and control structure Kun-Yuan Hsieh Programming Language Lab., NTHU.
Control Structures Programs have 4 basic control structures:
ISBN Chapter 8 Statement-Level 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
Expressions and Statements. Expressions Literals and identifiers are expressions More complex expressions are built from simple expressions by the application.
第八章 敘述層級的控制結構 (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
8-1 Statement-Level Control Structures Introduction Selection Statements Iterative Statements Unconditional Branching Guarded Commands Conclusions.
C HAPTER 8 Statement-Level Control Structures. CCSB314 Programming Language C HAPTER 8 T OPICS Introduction Selection Statements Iterative Statements.
April 16, ICE 1341 – Programming Languages (Lecture #14) In-Young Ko Programming Languages (ICE 1341) Lecture #14 Programming Languages (ICE 1341)
Statement Level Flow of Control Iteration Structures Copyright © by Curt Hill.
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.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
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.
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.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Chapter 8 Statement-Level Control Structures. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements.
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Chapter 8 Statement-Level Control Structures. 2 Chapter 8 Topics  Introduction  Selection Statements  Iterative Statements  Unconditional Branching.
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
Statement-Level Control Structures
Statement-Level Control Structures
Control statements Simple statements Basic structured statements
Control Structures In Text: Chapter 8.
CSC215 Lecture Flow Control.
Control Structures Programs utilize 4 basic control structures
Statement-Level Control Structures
Chapter8: Statement-Level Control Structures April 9, 2019
Statement-Level Control Structures
Statement-Level Control Structures
CSC215 Lecture Control Flow.
Chapter 8: Statement Level Control Structures
REPETITION Why Repetition?
Presentation transcript:

Control Structures sequence of execution of high-level statements

logic/ functional addressing encoding datainstructions data manipulation control location calculation identifiers/ names data types ADTs expressions assignment objects subprograms concurrency exception handling control structures COSC 3217 Programming Languages Dave Goforth

Control is control of sequence of execution  machine level – manipulate address register: (un)conditional branch  high level branch to label  goto GOTO GO TO constrained branch: break high level: selection, iteration

Control by branching - FORTRAN IF(X.EQ. Y) X=X+1 IF(X.EQ. Y) GO TO 100 > GO TO CONTINUE (dummy statement) > 200 CONTINUE >

Control by branching - FORTRAN IF(X-Y) 10,10,20 10 CONTINUE << statements for X<=Y GO TO CONTINUE Y >> 30 CONTINUE >

Statement labels  integers: FORTRAN, Pascal  identifiers as constants: ALGOL, C  variables! PL/1

The ‘goto’ debate: is branching needed?  powerful: only control needed  easy to abuse: readability, reliability suffer  ‘goto’ unnecessary (Böhm and Jacopini, 1966) but most languages retain limited (unlabelled) branching – e.g., break

Top-down structured programming 1975 flow chart model: no goto 3 flow structures -sequence -if-then-else (selection) -do-while (iteration) boolean expr statement

Selection: either / or  how many alternatives: 1do something or nothing 2if then else 3IF (X-Y) 10,20,30 manyswitch, nested IF, COND (LISP) variations on the selection structure

Selection design decisions  control of multiple statements in the control statement compound statement / block

Selection design decisions - if  nesting selection – syntax and logic convention/rule java, etc syntax – use block if (sum==0) //p.314 ? if (count==0) result = 0; else result = 1;

Multiple selection (1)  FORTRAN computed goto is multiple branch GO TO (10,20,30,40,50,60), X^^2+Y  case / switch model design decisions  default case allowed/ignored?  branch after section?

Multiple selection (2)  guarded statements – Dijkstra evaluate all conditions:  none true – error  one true – do statement  many true – select statement at random if -> [] -> fi

Multiple selection (2)  Ada: if (condition) elsif (condition) elsif (condition) else  LISP (COND (( condition )(value)) (( condition )(value)) (( condition )(value)) ( T(value)))

Iteration: repetition of statements  control by counter - for loop  control by data (by logic) - while loop  number of executions: 0 or more - pretest at beginning of loop body 1 or more - posttest at end of loop body  branching models of repetition

Counter controlled loops  developed with array indexing  counter (loop variable) parameters start, stop, stepsize

Counter controlled loops  design decision parameters computed when once or every execution ? x = 10; do I = 0 to x x = x executions or 6 executions

Counter controlled loops  design decisions: loop variable type, scope and value x = 0; I = 100; do I = 1 to 10 by 3 begin x = x + I; I = I - 1; print I end; print I, x;

Counter controlled loops  FORTRAN DO loop posttest, parameters evaluated once, number of executions predetermined, loop variable retains last value DO 100 K = 1, 10, 2 > 100 CONTINUE

Generalizing the counter  start, stop, step - FORTRAN  sequence, index expression & condition, “all of the above” - ALGOL for i = 4,5,6 i*10 while i,10000, 12, 14 step 10 until 25 do  simple counting – Ada, Pascal  expressions – C,C++,java for (,, )

Logical control  design issue: pretest or posttest?  most languages have ‘while’ pretest loop  posttest:  PascalC sum := 0;sum = 0; repeatdo sum := sum + 15 sum += 15; until (sum > 100); while (sum <= 100);

Constrained branching  switch statement break  programmer control of loop when to exit (between start and end) branch to end or branch out orthogonality – separate repetition (infinite loop) from branching out