Control Structures. Hierarchical Statement Structure Standard in imperative languages since Algol60. Exceptions: Early FORTRAN, COBOL, early BASIC, APL.

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

Intermediate Code Generation
Programming Languages and Paradigms
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:
Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements.
(8.1) COEN Control Structures  Control structure general issues  Compound statements  Selectors (conditional structures) – single – two-way –
1 Languages and Compilers (SProg og Oversættere) Sequence control and Subprogram Control.
Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
Computer Science 1620 Loops.
Control Flow C and Data Structures Baojian Hua
PZ07B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ07B - Basic statements Programming Language Design.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Control Structures Any mechanism that departs from straight-line execution: –transfer of control (closest to machine): gotos –Selection: if-statements.
Chapter 8 . Sequence Control
TYPE EQUIVALENCE 1) Coercion 2) Casting 3) Conversion.
Statement-Level Control Structures Sections 1-4
ISBN Chapter 8 Statement-Level Control Structures.
Sequence Control Chapter 6. 2 l Control structures: the basic framework within which operations and data are combined into programs. Sequence control.
1 Statement-Level Control Structures Levels of flow control Control Statements 1. Sequence 2. Selection 3. Iteration Unconditional branching Guarded commands.
ISBN Chapter 8 Statement-Level Control Structures Sections 1-4.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
CPS120 Introduction to Computer Science Iteration (Looping)
PLLab, NTHU,Cs2403 Programming Languages Expression and control structure Kun-Yuan Hsieh Programming Language Lab., NTHU.
1 Homework / Exam Turn in HW3 today Exam 1 next class –Open Book / Open Notes –Recommended Use of Book / Notes in Exam: Avoids reliance on “rote memorization”
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.
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.
Controlling Execution Dong Shao, Nanjing Unviersity.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Control Structures sequence of execution of high-level statements.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Statement Level Flow of Control Iteration Structures Copyright © by Curt Hill.
CPS120 Introduction to Computer Science Iteration (Looping)
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
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.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
Copyright © 2009 Elsevier Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
Engineering Computing I Chapter 3 Control Flow. Chapter 3 - Control Flow The control-flow of a language specify the order in which computations are performed.
Chapter 8 Statement-Level Control Structures. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements.
Imperative Programming Statements and invariants.
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Lecture 18 Control Flow.
Def: A control structure is a control statement and
Structure of Programming Language
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Lecture 14: Iteration and Recursion (Section 6.5 – 6.6)
Chapter 8: Control Structures
Imperative languages Most familiar: computation modifies store
Chap. 6 :: Control Flow Michael L. Scott.
Chap. 6 :: Control Flow Michael L. Scott.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Statement-Level Control Structures
Homework Any Questions?.
Languages and Compilers (SProg og Oversættere)
Chapter8: Statement-Level Control Structures April 9, 2019
CSC215 Lecture Control Flow.
Controlling Program Flow
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:

Control Structures

Hierarchical Statement Structure Standard in imperative languages since Algol60. Exceptions: Early FORTRAN, COBOL, early BASIC, APL. (Fortran kludged definite loops.)

Control structures within function Sequence: { S 1, S 2 … S k } Conditional: if statement, case statement. Loop: while loop, for loop. Jump: goto, exception raising

Expression evaluation Ambiguity of grouping: is 1+2*3 interpreted as (1+2)*3=9 or 1+(2*3)=7? Solutions: Operator precedence. * binds more tightly than +, so 1+2*3 = 7. Order precedence. Always compute left to right so 1+2*3=(1+2)*3 = 9 Best to use a lot of parentheses.

Short circuit evaluation If Y <> 0 and X/Y > 5, compute Z. if (Y <> 0 and X/Y > 5) Z = … --- but operators evaluate their arguments Solutions: Lazy evaluation of Booleans: if (Y <> 0 && X/Y > 5) Z = … Explicit conditionals if (Y <> 0) then if (X/Y > 5) Z= …

Prefix and postfix notation Prefix: Operator precedes its arguments Postfix: Operator follows its arguments e.g. (1+3*N)/[(1+N)*(1+2*N)] Prefix: / + 1 * 3 N *+ 1 N + 1 * 2 N Postfix: 1 3 N * + 1 N N * + * / If each operator has a fixed number of arguments, then prefix and postfix are unambiguous with no need for brackets, precedence, or associativity conventions.

Control structure between functions Function call and return Coroutine yield Parallel synchronization Exception raising

Assembly language In assembly language, (essentially) the only control structures are: Progression: Move to the next statement (increment the program counter). Unconditional jump: JMP A Jump to address A Conditional jump: JMZ R,A If (R==0) then jump to A Possible forms of conditions and addresses vary.

Sequence Pascal: begin … end C, C++, Java: { … } Ada: Brackets for sequence are unnecessary. Keywords for control structures suffice. for J in 1.. N loop … end loop ABC, Python: Indicate structure by indentation.

Semicolons Pascal: Semicolons are separators C etc.: Semicolons are terminators begin X := 1; { X = 1; Y := 2 Y = 2; end }

Conditionals if Condition then Statement -- Pascal, Ada if (Condition) Statement -- C, C++, Java To avoid ambiguities, use end marker: end if, “}” To deal with alternatives, use keyword or bracketing: if Conditions if (Conditions) then Statements { Statements } elseif Conditions else if (Conditions) then Statements then { Statements} else Statements else { Statements } end if

if-else ambiguity if Conditions if (Conditions) then then { if Conditions if (Conditions) then Statements { Statements } else Statements else { Statements } end if } end if

Compiling conditionals if (Cond) then Statement1 else Statement2 Assembly: …. Compute Cond in register R JMZ R,L … Code for Statement1 JMP END L: … Code for Statement2 END: … Next statement

Multi-way selection “The case statement is the most useful control structure because most programs are interpreters.” (Ed Schonberg) Can achieve same with conditionals, but case statements are clearer. case (NextChar) { I: N=1; V: N=5; X: N=10 L: N=50 }

The well-structured case statement Discrete type. No flow-through (hideous C misdesign). Every value is in exactly one option. Default option for values not covered. Labels are computable at compile time.

Implementation Finite set of possibilities: can build a table of addresses, and convert expression into table index: –compute value –transform into index –retrieve address of corresponding code fragment –branch to code fragment and execute –branch to end of case statement

Loops while loop: test at beginning while (Condition) Statement repeat loop: test at end repeat Statement until Condition do Statement while (Condition) breaking out: test in middle loop Statement if (condition) then exitloop; Statement end loop

Multiple exits If you want to exit from imbedded loops, you need to specify which one you’re exiting. Ada solution: Label loops Outer: while C1 loop … Inner: while C2 loop … Innermost: while C3 loop … exit Outer when MajorFailure; exit Inner when SmallAnnoyance; end loop Innermost; end loop Inner; end loop Outer;

Definite loops for J in loop … end loop for (int I=0; I < N; I++) … Design issues: Evaluation of bounds (only at start, since Algol60) Scope of loop variables Empty bodies Increments other than 1 Backward iteration Non-numeric domains

Definite loops Since the C for loop for (start; test; increment) body; allows arbitrary test conditions and arbitrary increment actions, this is not really a definite loop at all, just an distinctively formatted while loop. However, the compiler is sensitive to the particular construction for (int I=0; I < N; I++) and optimizes the code for this.

C: break and continue break: exit innermost loop (exitloop) continue: go to next iteration of innermost loop.

Loop counter Local to loop? If not, what is the value on exit? Settable in body of loop? Multiple loop counters?

Non-numeric domains Ada: for M in months loop … end loop Other data types: iterator = Collection.iterator(); element thing = iterator.first; while (iterator.moreElements()) { Body; thing = interator.next(); }

Implementation of loops loop Statement1; if (condition) exit loop; Statement2; end loop L1: … Code for Statement1 … Compute not(condition) and put in R; JMZ R,L2 … Code for Statement2 JMP L1 L2: Next statement

Gotos If you don’t have hierarchical statement structure, you need goto’s. “Go To Statements Considered Harmful” --- Dijkstra, 1968 Many more recent languages (e.g. Java) don’t have them. Certainly must prohibit jumping into the middle of a embedded structure; e.g. { … go to L for (int I=0; I<N; I++) { … L: …} }