CSI 3120, Control, page 1 Control statements Simple statements Basic structured statements Sequence Selection Iteration The jump statement.

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:
(8.1) COEN Control Structures  Control structure general issues  Compound statements  Selectors (conditional structures) – single – two-way –
ISBN Chapter 8 Statement-Level Control Structures.
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.
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.
1 Control Flow Aaron Bloomfield CS 415 Fall 2005.
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.
Principle of Programming Lanugages 2: Imperative languages Isao Sasano Department of Information Science and Engineering ( structured programming, control.
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.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
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)
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.
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.
1 Iterative Statements Repeated execution of a (compound) statement by iteration or recursion –Iteration is statement level –Recursion is unit-level control.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Chapter 8 © 2002 by Addison Wesley Longman, Inc Introduction - Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 8 Java Fundamentals Control Structures Fri.
W E E K F I V E Statement-Level Control Structures.
LECTURE 18 Control Flow. CONTROL FLOW Sequencing: the execution of statements and evaluation of expressions is usually in the order in which they appear.
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.
Chapter 8 Statement-Level Control Structures. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements.
Programming Techniques
Chapter 8 Statement-Level Control Structures. 2 Chapter 8 Topics  Introduction  Selection Statements  Iterative Statements  Unconditional Branching.
Lecture 18 Control Flow.
Def: A control structure is a control statement and
Principle of Programming Lanugages 2: Imperative languages
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
Flow of Control.
Flow of Control.
Statement-Level Control Structures
Control statements Simple statements Basic structured statements
Control Structures In Text: Chapter 8.
Flow of Control.
CSC215 Lecture Flow Control.
Control Structures Programs utilize 4 basic control structures
CSC215 Lecture Control Flow.
Statement-Level Control Structures
Chapter8: Statement-Level Control Structures April 9, 2019
CSC215 Lecture Control Flow.
Chapter 8: Statement Level Control Structures
Presentation transcript:

CSI 3120, Control, page 1 Control statements Simple statements Basic structured statements Sequence Selection Iteration The jump statement

CSI 3120, Control, page 2 Simple statements in imperative languages These are atomic (all or nothing) operations: assignment, the empty statement, a procedure call, exit, next, break, continue go to (jump). A block is also an all-or-nothing operation.

CSI 3120, Control, page 3 Structured statements sequence, or the compound statement: { S1 S2 } selection, or the conditional statement: if (C) S1 else S2 iteration, or the loop statement: while (C) S Three fundamental mechanisms allow us to group simple statements into structured statements.

CSI 3120, Control, page 4 All other control structures can be easily derived from the three basic mechanisms. if (C) S  if (C) S else {} do S while (C)  S while (C) S switch (i) {if (i == C1) S1 case C1: S1 …  else … and so on. Structured statements (2)

CSI 3120, Control, page 5 Sequence (1) LanguagesMechanisms Algol, Pascal, Ada,...begin... end C, Java, Perl{... } Fortran IVnothing Prologimplicit a :- b, c, d. This means evaluating b, then c, then d. Scheme(begin...)

CSI 3120, Control, page 6 Sequence (2) single entry, single exit A compound statement is treated as a simple statement. This is based on an important abstraction principle. The inner structure can be “abstracted away”.

CSI 3120, Control, page 7 Selection The if-else construct is present in almost all programming languages (Prolog is the only major exception). Modula and Ada were the first to have properly bracketed if-then-else, with four keywords around three elements of the selection: if C then S1 else S2 end if Nested selection if-elsif-...-else was also introduced in Ada: if C1 then S1 elsif C2 then S elsif Cn then Sn else Sn+1 end if

CSI 3120, Control, page 8 Special forms of selection in Fortran IV Computed GO TO. GO TO (label 1,..., label n ), expression Assigned GO TO. ASSIGN label i TO variable GO TO variable(label 1,..., label n )

CSI 3120, Control, page 9 The switch statement in C and Java has been probably inspired by computed GO TO. switch(expression){ case const 1 : S 1 ;... case const n : S n ; default: S n+1 ;} After S i has been executed, control "falls through" to the subsequent case: S i+1 is executed next. Fall-through can be avoided by adding break statements. Special forms of selection (2)

CSI 3120, Control, page 10 Case statement in Pascal, Ada and other similar languages: each case is separate, there is no "fall-through". In Ada: case expression is when constantList 1 => S 1 ;... when constantList n => S n ; when others => S n+1 ; end case; Special forms of selection (3)

CSI 3120, Control, page 11 Selection in Prolog is driven by success and failure, not by the true-false opposition. Selection is implicit in backtracking: if you succeed, stop; if not, try another choice. union( [Elem | S1], S2, S1_S2 ) :- member( Elem, S2 ), union( S1, S2, S1_S2 ). union( [Elem | S1], S2, [Elem | S1_S2] ) :- \+ member( Elem, S2 ), union( S1, S2, S1_S2 ). union( [], S2, S2 ). Special forms of selection (4)

CSI 3120, Control, page 12 Graphical representation flowgraphs — flow diagrams — flowcharts S2S2 C S1S1 Y N C S Y N if–then–else if–then

CSI 3120, Control, page 13 The abstraction principle: if ( C ) S1 else S2 is a simple statement. S2S2 C S1S1 Y N Single entry, single exit Graphical representation (2)

CSI 3120, Control, page 14 if–then–elsif-…elsif-then-else S2S2 S1S1 Y N C2C2 C1C1 S3S3 N C3C3 SnSn N CnCn … YY Y S n+1 Graphical representation (3)

CSI 3120, Control, page 15 case e of v1: S1;... else Sn+1 end S2S2 S1S1 Y N e=v 2 e=v 1 S3S3 N e=v 3 SnSn N e=v n … YY Y S n+1 Graphical representation (4)

CSI 3120, Control, page 16 Iteration Variations: pretest iteration or posttest iteration. while C do S Pascal repeat S until C while (C) S Java do S while (C) while C loop S end loop; Ada (no posttest iteration)

CSI 3120, Control, page 17 In Ada, the prefix while C is an extension of the basic iterative statement: loop S end loop; Another prefix: for i in range The bare loop statement must be stopped from inside the loop. Forced exit closes the nearest iteration: exit; unconditional exit when C; Iteration (2)

CSI 3120, Control, page 18 The while prefix is an abbreviation. The construction while C loop S end loop; is equivalent to loop exit when not C; S end loop; Iteration (3)

CSI 3120, Control, page 19 Example: use of exit SUM := 0; loop get(X); exit when X = 0; SUM := SUM + X; end loop; Simpler, more intuitive SUM := 0; get(X); while X /= 0 loop SUM := SUM + X; get(X); end loop; Condition reversed, get(X) appears twice

CSI 3120, Control, page 20 Graphical representation while-do C S Y N repeat-until S Y N S ¬C C YN S repeat-until

CSI 3120, Control, page 21 loop - exit - end loop S2S2 Y N S1S1 C Graphical representation (2)

CSI 3120, Control, page 22 for loops For-loops ("counter-controlled") are historically earlier and less general than condition-controlled iterative structures. DO 1000 var = lo, hi Fortran IV CONTINUE DO label var = lo, hi, incr for var := expr do S Algol 60 for var := low step incr until high do S for var := expr while C do S Iterators can be combined: for i := 0, i+1 while i ≤ n do S(i)

CSI 3120, Control, page 23 for var in range Ada loop S end loop; for var in reverse range loop S end loop; for (e1; e2; e3) S C, Java, Perl (enough said ) What is this? for (;;) S for loops (2)

CSI 3120, Control, page 24 Iteration in Prolog and in Scheme is expressed by recursion. The same, of course, is possible in most other typical languages. for loops 32)

CSI 3120, Control, page 25 Jump (the goto statement) Unconstrained transfer of control is the only mechanism available in low-level languages — but they are very general. One-branch selection and goto allow us to express all other control structures. The jump mechanism is dangerous, may hurt readability, and should be avoided — advanced control structures work well for all typical and for most less typical uses.

CSI 3120, Control, page 26 Some languages restrict goto (do not allow jumping inside an iteration or selection) and make it hard to use. Ada makes labels visible from far away (so that your boss can see it!).: SUM := 0; loop get(X); if X = 0 then goto DONE; end if; SUM := SUM + X; end loop; > put(SUM); goto may leave “unfinished business” — active control structures that must be "folded" at once. Jump (the goto statemen)t (2)