Flow of Control Execution Sequence

Similar documents
Control Structures. Outline. In Text: Chapter 8. Control structures Selection. Iteration. Gotos Guarded statements. One-way Two-way Multi-way

Chapter 7. - FORTRAN I control statements were based directly on IBM 704 hardware

Statement level control structures

Ch. 7: Control Structures

Chapter 8. Statement-Level Control Structures

Iterative Statements. Iterative Statements: Examples. Counter-Controlled Loops. ICOM 4036 Programming Languages Statement-Level Control Structure

Chapter 8. Statement-Level Control Structures

Module 25 Control Flow statements and Boolean Expressions

Chapter 8 Statement-Level Control Structures

Chapter 8. Statement-Level Control Structures ISBN

Chapter 8 Statement-Level Control Structure

Chapter 8. Statement-Level Control Structures

CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV. Department of Computer Science University of Arizona

Expressions vs statements

Control Structures. Boolean Expressions. CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV

Chapter 7 Control I Expressions and Statements

Module 27 Switch-case statements and Run-time storage management

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

LECTURE 18. Control Flow

CPSC 3740 Programming Languages University of Lethbridge. Control Structures

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Test 1 Summer 2014 Multiple Choice. Write your answer to the LEFT of each problem. 5 points each 1. Preprocessor macros are associated with: A. C B.

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

LECTURE 5 Control Structures Part 2

CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction

Statement-Level Control Structures

Statement-Level Control Structures

UNIT II Structuring the Data, Computations and Program. Kainjan Sanghavi

Relational Expressions. Boolean Expressions. Boolean Expressions. ICOM 4036 Programming Languages. Boolean Expressions

Principles of Programming Languages Lecture 22

Ordering Within Expressions. Control Flow. Side-effects. Side-effects. Order of Evaluation. Misbehaving Floating-Point Numbers.

Control Flow COMS W4115. Prof. Stephen A. Edwards Fall 2006 Columbia University Department of Computer Science

COP4020 Programming Languages. Control Flow Prof. Robert van Engelen

Syntax. A. Bellaachia Page: 1

Lecture 4. CS118 Term planner. The simple if statement. Control flow: selection. The if... else statement cont... The if... else statement.

Expressions and Statements. Department of CSE, MIT, Manipal

Page # Expression Evaluation: Outline. CSCI: 4500/6500 Programming Languages. Expression Evaluation: Precedence

Programming Languages Semantics

G Programming Languages - Fall 2012

Quick Reference Guide

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Control Flow. Stephen A. Edwards. Fall Columbia University

int foo() { x += 5; return x; } int a = foo() + x + foo(); Side-effects GCC sets a=25. int x = 0; int foo() { x += 5; return x; }

Lexical Considerations

Imperative Programming Languages (IPL)

Flow Control. CSC215 Lecture

CSE 307: Principles of Programming Languages

Lecture 9: Control Flow

Lexical Considerations

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

CS 199 Computer Programming. Spring 2018 Lecture 5 Control Statements

Accelerating Information Technology Innovation

NOTE: Answer ANY FOUR of the following 6 sections:

1 Lexical Considerations

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

CS1100 Introduction to Programming

More Examples. Lecture 24: More Scala. Higher-Order Functions. Control Structures

Compiler Construction

SYNTAX DIRECTED TRANSLATION FOR CONTROL STRUCTURES

Control Structures. Important Semantic Difference

Compiler Construction

UNIT 3

! Non-determinacy (unspecified order) Maria Hybinette, UGA 2. 1 Landin adding sugar to a language to make it easier to read (for humans)

Compiler Construction

Continuations provide a novel way to suspend and reexecute

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

Compiler Construction

C++ Programming: From Problem Analysis to Program Design, Third Edition

Compiler Construction

Programmin Languages/Variables and Storage

CMPSC 160 Translation of Programming Languages. Three-Address Code

Compiler Construction

Introduction. C provides two styles of flow control:

C Course. IIT Kanpur. Lecture 3 Aug 31, Rishi Kumar <rishik>, Final year BT-MT, CSE

Compiler Construction

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Formal Languages and Compilers Lecture X Intermediate Code Generation

Theory of control structures

Principle of Compilers Lecture VIII: Intermediate Code Generation. Alessandro Artale

Review of the C Programming Language

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

Annex A (Informative) Collected syntax The nonterminal symbols pointer-type, program, signed-number, simple-type, special-symbol, and structured-type

Languages and Compiler Design II IR Code Generation I

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

MIDTERM EXAMINATION - CS130 - Spring 2003

R13 SET Discuss how producer-consumer problem and Dining philosopher s problem are solved using concurrency in ADA.

Introduction to Programming Using Java (98-388)

Computer Programming I - Unit 5 Lecture page 1 of 14

Introduction to C/C++ Lecture 3 - Program Flow Control

Instructor: SIR MUHAMMAD NAVEED Created by: ARSLAN AHMED SHAAD ( ) MUHAMMAD BILAL ( ) ISIT:

Princeton University Computer Science 217: Introduction to Programming Systems The Design of C

Object-Oriented Programming. Topic 2: Fundamental Programming Structures in Java

Concepts Introduced in Chapter 7

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

Using Boolean Expressions. Multiway Branches. More about C++ Loop Statements. Designing Loops. In this chapter, you will learn about:

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

MODULE 2: Branching and Looping

Transcription:

Flow of Control Execution Sequence Sequence sequence Decision tree Repetition graph Language Constructs sequence statements (incl Foc) simple / compound decision (1 /2 / n way) if then [else] case / switch repetition for loop (counter) while (condition) * repeat (do) / until (cond) + Recursion 3AC: if X goto L / goto L 1DFR - PL - Flow of Control

Control Structures Control structure control statement + collection of statements single entry & exit Compound statement e.g. begin statement_1 statement_2 statement_n end Block compound statement + data declarations (e.g. C) Decision selection statements 1 way Fortran IV IF (Boolean expr) statement 2 way if then else n way case / switch 2DFR - PL - Flow of Control

2 way Decision: if then else dangling else problem if A then if B then C else D syntactic/semantic solution? if A then { if B then C else D } if A then { if B then C } else D or if A then if B then C else D use a special word end if Algol 68, Fortran 77/90, Ada Modula 2 uses end only less readable Operational Semantics if A then B else C if not A goto Lelse B goto Lout Lelse: C Lout: NB Three address code (3ac) conditional goto if A then goto L unconditional goto goto L 3DFR - PL - Flow of Control

Multiple Selection Examples Fortran IF (arith expr) N1 N2 N3 others case <expr> of <0 0 >0 const_list_1: statement_1; const_list_2: statement_2; const_list_n: statement_n; else default_statement end {case} Operational Semantics if I = 1 goto L1 if I = 3 goto L3 if I = n goto Ln default statement; goto Lout L1: statement_1; goto Lout... Ln: statement_n; goto Lout default: statement Lout: NB C switch & break 4DFR - PL - Flow of Control

Multiple Selection : Design Issues Design Issues (multiple selection) type of case expression ordinal case selectors disjoint sets? multiple entries e.g. 1, 3, 6: statement_x are ranges allowed? e.g.1..10: statement_x default case mandatory? ex/implicit branch (C/Pascal) Cascaded if then else Boolean rather than ordinal if A then S1 else if B then S2 else if X then Sn if A then S1 (Ada) elsif B then S2 elsif X then Sn 5DFR - PL - Flow of Control

Repetition (Iteration, Loop) Loop = test + body pre test (before body executes)* post test (after body executes)+ Counter controlled loops n iterations loop parameters loop variable initial value terminal value step size Design Issues loop variable allowable types ordinal (int, char), real? scope? value on termination? can loop parameters be changed in the body? are loop parameters evaluated once or at every iteration? 6DFR - PL - Flow of Control

Loop Examples (historic) Fortran 77 DO label var = init, term, stepsize operational semantics iv := init initial value tv := term terminal value sv := stepsize step size dov := initval do variable ic := max( (tv iv + sv) / sv, 0) loop: if ic <= 0 goto lout [loop body] dov = dov + sv ic = ic 1 goto loop lout: Algol 60 <for_stmt> ::= for var := <list_el> {, <list_el> } do <stmt> <list_el> ::= <expr> <expr> step <expr> until<expr> <expr> while <Boolean_expr> for c := 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 do for c := 1 step 1 until 10 do for c := 1, c+1, while (c <= 10) do NB all <expr>s in for are evaluated for every iteration; loop variable may be changed in body 7DFR - PL - Flow of Control

Loop Examples Pascal for var := init_value (to downto) final_value do statement semantics loop variable ordinal type value undefined on normal termination last value on abnormal termination may not be changed in the body init_/ final_value evaluated once C, C++, Java for (expr1; expr2; expr3) loop body operational semantics expr1 loop: if expr2 = 0 goto lout [loop body] expr3 goto loop lout: e.g. for ( i=0; i<limit; i++)... 8DFR - PL - Flow of Control

Logically Controlled Loops Design issues pre test or post test? is this a special form of a counting loop or separate? Examples C++ while (<expr>) [loop body] do [loop body] while (<expr>) Operational semantics while loop: if!<expr> goto lout [loop body] goto loop lout: do while loop: [loop body] if <expr> goto loop 9DFR - PL - Flow of Control

Other Control Constructs Ada loop exit (also other PLs) loop end loop exit [loop label] [when <cond>] (un)conditional loop exit design issues is <cond> part of exit? where is exit allowed? in controlled loops? anywhere? is exit from one loop or several nested loops? C, C++ continue statement skips part of loop body exit, break allow multiple exits Data structure based iteration $ns = ( Bob, Ted, Joy ) foreach $n ($ns) { print $n; } function iterators user data structure + function used to navigate the structure the goto statement!!! Dijkstra s guarded if & do DFR - PL - Flow of Control 10

Control Flow Templates if E then S 1 if E then S 1 else S 2 while E do S 1 E.true E.false E.code* S 1.code E.true E.false S.next E.code* S 1.code goto S.next S 2.code S.begin E.true E.false E.code* S 1.code goto S.begin Note the distinction between the labels associated with the expression (E.true, E.false) and those associated with the statement (S.begin, S.next) *NB at end of E.code: if (E.code) goto E.true; go to E.false DFR - PL - Flow of Control 11

Control Flow summary Language Constructs sequence statements (incl Foc) simple / compound decision (1 / 2 / n way) if then [else] case / switch repetition for loop (counter) while (condition) * repeat (do) / until (cond) + Recursion 3AC: if X goto L / goto L Code generation if then (else); case; for; while; repeat; etc. are translated to if X goto L (conditional goto) goto L (unconditional goto) See templates above Operational semantics are used to describe foc constructs goto (break; exit; continue) may be language provided DFR - PL - Flow of Control 12