Chapter 8 . Sequence Control

Slides:



Advertisements
Similar presentations
Intermediate Code Generation
Advertisements

Statement-Level Control Structures
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
8 Intermediate code generation
Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements.
ISBN Chapter 8 Statement-Level Control Structures.
PZ08A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ08A - Prime programs Programming Language Design and.
Prime programs Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
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.
Control Structures. Hierarchical Statement Structure Standard in imperative languages since Algol60. Exceptions: Early FORTRAN, COBOL, early BASIC, APL.
Statement-Level Control Structures Sections 1-4
ISBN Chapter 8 Statement-Level Control Structures.
CSE 452: Programming Languages Expressions and Control Flow.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
1 Chapter 8 Statement-Level Control Structures In Chapter 7, the flow of control within expressions, which is governed by operator associativity and precedence.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
Sequence Control Chapter 6. 2 l Control structures: the basic framework within which operations and data are combined into programs. Sequence control.
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
1 Programming Languages Implementation of Control Structures Cao Hoaøng Truï Khoa Coâng Ngheä Thoâng Tin Ñaïi Hoïc Baùch Khoa TP. HCM.
Flow of Control. 2 Control Structures Control structure: An instruction that determines the order in which other instructions in a program are executed.
ISBN Chapter 8 Statement-Level Control Structures Sections 1-4.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
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.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Control Structures sequence of execution of high-level statements.
CSE 425: Control Flow I Categories of Control Flow Constructs Sequencing –order of expressions and statements Selection –if, else, switch Iteration –loops.
8-1 Statement-Level Control Structures Introduction Selection Statements Iterative Statements Unconditional Branching Guarded Commands Conclusions.
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.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
W E E K F I V E Statement-Level Control Structures.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
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.
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
Chapter 8 Statement-Level Control Structures. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Expressions and Assignment Statements
Def: A control structure is a control statement and
A Simple Syntax-Directed Translator
Sequence, Selection, Iteration The IF Statement
Dr. Vamsi Paruchuri University of Central Arkansas
Statement-Level Control Structures
Chapter 8: Control Structures
Statement-Level Control Structures
Control Structures In Text: Chapter 8.
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises UTPA – Fall 2012 This set of slides is revised from lecture.
The University of Texas – Pan American
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Statement-Level Control Structures
Languages and Compilers (SProg og Oversættere)
Flow of Control.
CSC215 Lecture Control Flow.
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:

Chapter 8 . Sequence Control Levels of sequence control Sequencing with expressions Statement-level sequence control Prime programs

Sequence control Control of the order of execution of the operations both primitive and user defined. Implicit: determined by the order of the statements in the source program or by the built-in execution model Explicit: the programmer uses statements to change the order of execution (e.g. uses If statement)

Levels of sequence control Expressions: How data are manipulated using precedence rules and parentheses. Statements: conditional and iteration statements change the sequential execution.   Declarative programming: an execution model that does not depend on the order of the statements in the source program. Subprograms: transfer control from one program to another.

Sequencing with expressions What is the sequence of performing the operations? How is the sequence defined, and how is it represented? Functional composition : Basic sequence-control mechanism: Given an operation with its operands, the operands may be: ·        Constants ·        Data objects ·        Other operations

Example Example: 3 * (var1 + 5) operation - multiplication, operator: *, arity - 2 operand 1: constant (3) operand 2: operation addition operand1: data object (var1) operand 2: constant (5)

More examples and questions Example 2: 3* var1 +5 Question: is the example equivalent to the above one?   Example 3: 3 + var1 +5 Question: is this equivalent to (3 + var1) + 5, or to 3 + (var1 + 5) ?

Precedence and associativity Precedence concerns the order of applying operations Associativity deals with the order of operations of same precedence.   Precedence and associativity are defined when the language is defined - within the semantic rules for expressions.

Arithmetic operations / expressions Linear representation of the expression tree:        Prefix notation ·        Postfix notation ·        Infix notation   Prefix and postfix notations are parentheses-free.

Execution-time representation of expressions Machine code sequence Tree structures - software simulation Prefix or postfix form - requires stack, executed by an interpreter.

Evaluation of tree representation Eager evaluation - evaluate all operands before applying operators. Lazy evaluation

Problems Side effects - some operations may change operands of other operations. Error conditions - may depend on the evaluation strategy (eager or lazy evaluation) Boolean expressions - results may differ depending on the evaluation strategy.

Statement-level sequence control Forms of statement-level control Composition – Statements are executed in the order they appear in the source code. Alternation – Two sequences form alternatives so one sequence or the other sequence is executed but not both. (conditionals) Iteration – A sequence of statements that are executed repeatedly

Explicit Sequence Control goto X if Y goto X – transfer control to the statement labeled X if Y is true. break  

Structured programming

Structured programming design   (1)   Hierarchical design of program structures (2)   Representation of hierarchical design directly in the program text using "structured" control statements. (3)   The textual sequence corresponds to the execution sequence (4)   Use of single-purpose groups of statements

Structured control statements Compound statements Conditionals Iterations

Compound statements Typical syntax: begin statement1; statement2; ... end;   Execute each statement in sequence. Sometimes (e.g., C) { ... } used instead of begin ... end

Conditional statements if expression then statement1 else statement2   if expression then statement1  a choice among many alternatives nested if statements case statements Implementation: jump and branch machine instructions, jump table implementation for case statements (see fig. 8.7)

Iteration statements Specifies a count of the number Simple repetition (for loop) Specifies a count of the number of times to execute a loop: perform statement K times;   for loop - Examples: for I=1 to 10 do statement; for(I=0;I<10; I++) statement;

Repetition while condition holds while expression do statement; Evaluate expression and if true execute statement, then repeat process.   repeat statement until expression; Execute statement and then evaluate expression. Repeat if expression is not true. C++ for loop functionally is equivalent to repetition while condition holds

Problems with structured sequence control Multiple exit loops Exceptional conditions Do-while-do structure   Solutions vary with languages, e.g. in C++ - break statement, assert for exceptions.

Prime programs Three types of nodes to be used in a flowchart

Some definitions Any flowchart is a graph of directed arcs and these 3 types of nodes   A proper program is a flowchart with: •1 entry arc •1 exit arc •There is a path from entry arc to any node to exit arc  

Prime programs, composite programs A prime program is a proper program which has no embedded proper subprogram of greater than 1 node. (i.e., cannot cut 2 arcs to extract a prime subprogram within it).   A composite program is a proper program that is not prime.

Primes of up to 4 nodes

Structure theorem Any flowchart can be represented using only if statements, while statements and sequence control statements