Sequence Control Chapter 6. 2 l Control structures: the basic framework within which operations and data are combined into programs. Sequence control.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Statement-Level Control Structures
Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
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.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
ISBN Chapter 7 Expressions and Assignment Statements.
COEN Expressions and Assignment
CHAPTER 8 SEQUENCE CONTROL Hardware- Von Neumann architecture (sequence from incrementing program counter); loop and conditional from test and jump (branch)
ISBN Chapter 7 Expressions and Assignment Statements.
1 Chapter 7: Expressions Expressions are the fundamental means of specifying computations in a programming language To understand expression evaluation,
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.
Chapter 8 . Sequence Control
ISBN Chapter 8 Statement-Level Control Structures.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
ISBN Chapter 7 Expressions and Assignment Statements.
ISBN Chapter 7 Expressions and Assignment Statements.
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 07 Expressions and Assignment Statements.
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
Copyright © 1998 by Addison -Wesley Longman, Inc. 1 Chapter 6 Arithmetic Expressions - Their evaluation was one of the motivations for the development.
1-1 University of Hail College of Computer Science and Engineering Department of computer Science and Software Engineering Course: ICS313: Fundamentals.
7-1 Chapter 7: Expressions and Assignment Statements Arithmetic Expressions –the fundamental means of specifying computations in a programming language.
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
C H A P T E R S E V E N Expressions and Assignment Statements.
Flow of Control. 2 Control Structures Control structure: An instruction that determines the order in which other instructions in a program are executed.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
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.
Arithmetic Expressions
Control Structures sequence of execution of high-level statements.
Expressions and Assignment Statements
Chapter 7 © 1998 by Addison -Wesley Longman, Inc Arithmetic Expressions - Their evaluation was one of the motivations for the development of the.
ISBN Chapter 7 Expressions and Assignment Statements.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Semantics (1).
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.
Chapter 7 Expressions and Assignment Statements. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 7 Topics Introduction Arithmetic Expressions.
7-1/27 Chapter 7 Expressions and Assignment Statements Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean.
Expressions and Assignment Statements
Expressions and Assignment Statements
7.2 Arithmetic Expressions
Expressions and Assignment Statements
Dr. Vamsi Paruchuri University of Central Arkansas
Statement-Level Control Structures
Expressions and Assignment Statements
Chapter 8: Control Structures
Expressions and Assignment Statements
Arithmetic Expressions
Expressions and Assignment Statements
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.
College of Computer Science and Engineering
The University of Texas – Pan American
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Chapter 6: Repetition Statements
Statement-Level Control Structures
Chapter 7 Expressions and Assignment Statements.
Chapter8: Statement-Level Control Structures April 9, 2019
Chap 7. Advanced Control Statements in Java
CSC215 Lecture Control Flow.
Expressions and Assignment Statements
Presentation transcript:

Sequence Control Chapter 6

2 l Control structures: the basic framework within which operations and data are combined into programs. Sequence control data control

3 l Sequence control: the control of the order of execution of the operations ( Primitive, user defined). l Data control: the control of the transmission of data among the subprograms of a program.

4 sequence control l Structures used in expressions (and thus within statements) such as precedence rules, parentheses. l Structures used between statements or groups of statements, such as conditional, iteration. l Structures used between subprograms, such as subprograms calls, coroutines.

5 l Implicit sequence control: (default) defined by the language. l Explicit sequence control: defined by the programmer.

Sequence with arithmetic expressions l R=(-B+SQRT(B**2-4*A*C))/(2*A) 15 separate operations can the two references to the value of B and A be combined? what order to evaluate the expression to minimize the temporary storage ?

7 l Tree-structure representation (p.240, fig. 6.1.) l syntax for expression prefix notation no parentheses postfix notation no parentheses infix notation l semantics for expressions evaluation of expressions (p.243)

8 Semantics for expressions l Hierarchy of operations (precedence rules) p.245 l Associativity (left to right,right to left) p.245,246

9 Execution-Time Representation l Machine code sequences. l tree structures. l prefix or postfix form.

10 Evaluation of tree representation of expressions l Problems: uniform evaluation rules Side effects error conditions Short-circuit boolean expressions

11 uniform evaluation rules l Eager evaluation rule: first evaluate the operands (order is not important). P.250 l lazy evaluation rule: never evaluate the operands first. Fig Z+(X==o?Y:Y/X)

12 l Z+(X=0?Y:Y/X) pass the operands (or at least the last two operands) to the conditional operation unevaluated an let the operation determine the order of evaluation. Passing parameters by value or by name

13 l No simple evaluation rule In LISP: functions are split into two categories, receives evaluated operands receives unevaluated operands In SNOBOL4: programmer-defined operations: receives evaluated operands, language-defined operations: receives unevaluated operands.

14 Side effects l a*fun(x)+a evaluate each term in sequence evaluate a only once call fun(x) before evaluating a

15 Side effects solutions: side effects should be outlawed in expressions, language definition clears the order of evaluation, cause optimization impossible, ignore the question, decided by implementer.

16 error conditions l Overflow, divide by zero. l Solutions varies from language to language and implementation to implementation.

17 Short-circuit Boolean expressions If ((A==0)||(B/A>c)) while ((I c)) l in many languages both operands are evaluated before the Boolean operation is evaluated. l Solution in Ada, explicitly: and then, or else

Sequence with non- arithmetic expressions l Pattern matching term rewriting l unification l backtracking

19 Pattern matching l Pattern matching by string replacement (in SNOBOL4 ): A-> 0A0 | 1A1 | 0 | A 1 matches the center 1 A 2 matches 0A 1 0 A 3 matches 0A 2 0

20 l In Prolog, a relation as a set of n-tuples, l specify known instances of these relations (called facts), l other instances can be derived.

21 ParentOf(John,Mary). ParentOf(Susan,Mary). ParentOf(Bill,John). ParentOf(Ann,John). ParentOf(X,Mary) ParentOf(X,Mary), ParentOf(Y,Mary), not(X=Y)

22 l Building relations out of other relations, GrandparentOf(X,Y):- ParentOf(X,Z), ParentOf(Z,Y).

23 Term Rewriting l A restricted form of pattern matching string: a 1 a 2 …a n rewrite rule x=>y if x=a i, a 1 …a i-1 y…a n is a term rewrite of a 1 a 2 …a n.

24 Unification l Prolog uses unification, or the substitution of variables in relations, to pattern match. l Determine if the query has a valid substitution consistent with the rules and facts in the database.

25 l A rule: GrandparentOf(X,Y) :- ParentOf(X,Z), ParentOf(Z,Y) ParentOf(X,Mary) = ParentOf(John,Y) ParentOf(John,Mary) unifies it.

26 l In Prolog : Queries are unified with rules or with facts in the database until true results. If false results, it means that a wrong rule or fact is used, then an alternative (if any) must be tried.

Sequence control between statements l Basic statements assignments (p. 265) subprogram calls I/O statements

28 Forms of statement- level sequence control l Composition l Alternation l Iteration

29 l Explicit sequence control goto conditional unconditional break, continue (in C)

30 Structured programming design l Gotos advantages: hardware support easy to use in small programs familiar to older programmers(!!) general purpose

31 l Gotos disadvantages: lack of hierarchical program structure there is no one-in, one-out control structure spaghetti code (program text, execution order) groups of statements serve multiple purposes

32 Structured Programming l hierarchical program design using only THE three structures. l Hierarchical Implementation like design. l No spaghetti code, textual sequence of statements like execution sequence. l groups of statements serve single purpose.

33 Structured sequence control l Compound statements l conditional statements if (single-branch,multi-branch), case (p.274). l Iteration statements

34 Iteration statements Head and a body (p.276,277) l Simple repetition l repetition while condition holds l repetition while incrementing counter l infinite repetition When is the termination test made? When are the variables used in the statement ahead evaluated?

35 l In C simple counter from 1 to 1o for(I=1;I<=10;I++){body} infinite loop for(;;){body} counter with exit condition for(I=1;I<=100&&NotEntfile;I++){body}

36 Problems in structured sequence control l Multiple exit loops. l do-while-do. l exceptional conditions. p. 278,279