Chapter 8. Sequence Control Levels of sequence control Sequencing with expressions Statement-level...
-
date post
21-Dec-2015 -
Category
Documents
-
view
285 -
download
3
Transcript of Chapter 8. Sequence Control Levels of sequence control Sequencing with expressions Statement-level...
![Page 1: Chapter 8. Sequence Control Levels of sequence control Sequencing with expressions Statement-level sequence control Prime programs.](https://reader036.fdocuments.net/reader036/viewer/2022081512/56649d585503460f94a36e46/html5/thumbnails/1.jpg)
Chapter 8 . Sequence Control
• Levels of sequence control• Sequencing with expressions• Statement-level sequence
control • Prime programs
![Page 2: Chapter 8. Sequence Control Levels of sequence control Sequencing with expressions Statement-level sequence control Prime programs.](https://reader036.fdocuments.net/reader036/viewer/2022081512/56649d585503460f94a36e46/html5/thumbnails/2.jpg)
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)
![Page 3: Chapter 8. Sequence Control Levels of sequence control Sequencing with expressions Statement-level sequence control Prime programs.](https://reader036.fdocuments.net/reader036/viewer/2022081512/56649d585503460f94a36e46/html5/thumbnails/3.jpg)
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.
![Page 4: Chapter 8. Sequence Control Levels of sequence control Sequencing with expressions Statement-level sequence control Prime programs.](https://reader036.fdocuments.net/reader036/viewer/2022081512/56649d585503460f94a36e46/html5/thumbnails/4.jpg)
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
![Page 5: Chapter 8. Sequence Control Levels of sequence control Sequencing with expressions Statement-level sequence control Prime programs.](https://reader036.fdocuments.net/reader036/viewer/2022081512/56649d585503460f94a36e46/html5/thumbnails/5.jpg)
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)
![Page 6: Chapter 8. Sequence Control Levels of sequence control Sequencing with expressions Statement-level sequence control Prime programs.](https://reader036.fdocuments.net/reader036/viewer/2022081512/56649d585503460f94a36e46/html5/thumbnails/6.jpg)
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) ?
![Page 7: Chapter 8. Sequence Control Levels of sequence control Sequencing with expressions Statement-level sequence control Prime programs.](https://reader036.fdocuments.net/reader036/viewer/2022081512/56649d585503460f94a36e46/html5/thumbnails/7.jpg)
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.
![Page 8: Chapter 8. Sequence Control Levels of sequence control Sequencing with expressions Statement-level sequence control Prime programs.](https://reader036.fdocuments.net/reader036/viewer/2022081512/56649d585503460f94a36e46/html5/thumbnails/8.jpg)
Arithmetic operations / expressions
Linear representation of the expression tree:
Prefix notation
· Postfix notation
· Infix notation
Prefix and postfix notations are parentheses-free.
![Page 9: Chapter 8. Sequence Control Levels of sequence control Sequencing with expressions Statement-level sequence control Prime programs.](https://reader036.fdocuments.net/reader036/viewer/2022081512/56649d585503460f94a36e46/html5/thumbnails/9.jpg)
Execution-time representation of expressions
Machine code sequence
Tree structures - software simulation
Prefix or postfix form - requires stack, executed by an interpreter.
![Page 10: Chapter 8. Sequence Control Levels of sequence control Sequencing with expressions Statement-level sequence control Prime programs.](https://reader036.fdocuments.net/reader036/viewer/2022081512/56649d585503460f94a36e46/html5/thumbnails/10.jpg)
Evaluation of tree representation
Eager evaluation - evaluate all operands before applying operators.
Lazy evaluation
![Page 11: Chapter 8. Sequence Control Levels of sequence control Sequencing with expressions Statement-level sequence control Prime programs.](https://reader036.fdocuments.net/reader036/viewer/2022081512/56649d585503460f94a36e46/html5/thumbnails/11.jpg)
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.
![Page 12: Chapter 8. Sequence Control Levels of sequence control Sequencing with expressions Statement-level sequence control Prime programs.](https://reader036.fdocuments.net/reader036/viewer/2022081512/56649d585503460f94a36e46/html5/thumbnails/12.jpg)
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
![Page 13: Chapter 8. Sequence Control Levels of sequence control Sequencing with expressions Statement-level sequence control Prime programs.](https://reader036.fdocuments.net/reader036/viewer/2022081512/56649d585503460f94a36e46/html5/thumbnails/13.jpg)
Explicit Sequence Control
goto X
if Y goto X
– transfer control to the statement
labeled X if Y is true.
break
![Page 14: Chapter 8. Sequence Control Levels of sequence control Sequencing with expressions Statement-level sequence control Prime programs.](https://reader036.fdocuments.net/reader036/viewer/2022081512/56649d585503460f94a36e46/html5/thumbnails/14.jpg)
Structured programming
![Page 15: Chapter 8. Sequence Control Levels of sequence control Sequencing with expressions Statement-level sequence control Prime programs.](https://reader036.fdocuments.net/reader036/viewer/2022081512/56649d585503460f94a36e46/html5/thumbnails/15.jpg)
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
![Page 16: Chapter 8. Sequence Control Levels of sequence control Sequencing with expressions Statement-level sequence control Prime programs.](https://reader036.fdocuments.net/reader036/viewer/2022081512/56649d585503460f94a36e46/html5/thumbnails/16.jpg)
Structured control statements
• Compound statements
• Conditionals
• Iterations
![Page 17: Chapter 8. Sequence Control Levels of sequence control Sequencing with expressions Statement-level sequence control Prime programs.](https://reader036.fdocuments.net/reader036/viewer/2022081512/56649d585503460f94a36e46/html5/thumbnails/17.jpg)
Compound statementsTypical syntax:
begin
statement1;
statement2;
...
end;
Execute each statement in sequence.
Sometimes (e.g., C) { ... } used instead of begin ... end
![Page 18: Chapter 8. Sequence Control Levels of sequence control Sequencing with expressions Statement-level sequence control Prime programs.](https://reader036.fdocuments.net/reader036/viewer/2022081512/56649d585503460f94a36e46/html5/thumbnails/18.jpg)
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)
![Page 19: Chapter 8. Sequence Control Levels of sequence control Sequencing with expressions Statement-level sequence control Prime programs.](https://reader036.fdocuments.net/reader036/viewer/2022081512/56649d585503460f94a36e46/html5/thumbnails/19.jpg)
Iteration statements
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;
![Page 20: Chapter 8. Sequence Control Levels of sequence control Sequencing with expressions Statement-level sequence control Prime programs.](https://reader036.fdocuments.net/reader036/viewer/2022081512/56649d585503460f94a36e46/html5/thumbnails/20.jpg)
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
![Page 21: Chapter 8. Sequence Control Levels of sequence control Sequencing with expressions Statement-level sequence control Prime programs.](https://reader036.fdocuments.net/reader036/viewer/2022081512/56649d585503460f94a36e46/html5/thumbnails/21.jpg)
![Page 22: Chapter 8. Sequence Control Levels of sequence control Sequencing with expressions Statement-level sequence control Prime programs.](https://reader036.fdocuments.net/reader036/viewer/2022081512/56649d585503460f94a36e46/html5/thumbnails/22.jpg)
![Page 23: Chapter 8. Sequence Control Levels of sequence control Sequencing with expressions Statement-level sequence control Prime programs.](https://reader036.fdocuments.net/reader036/viewer/2022081512/56649d585503460f94a36e46/html5/thumbnails/23.jpg)
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.
![Page 24: Chapter 8. Sequence Control Levels of sequence control Sequencing with expressions Statement-level sequence control Prime programs.](https://reader036.fdocuments.net/reader036/viewer/2022081512/56649d585503460f94a36e46/html5/thumbnails/24.jpg)
Prime programs
Three types of nodes to be used in a flowchart
![Page 25: Chapter 8. Sequence Control Levels of sequence control Sequencing with expressions Statement-level sequence control Prime programs.](https://reader036.fdocuments.net/reader036/viewer/2022081512/56649d585503460f94a36e46/html5/thumbnails/25.jpg)
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
![Page 26: Chapter 8. Sequence Control Levels of sequence control Sequencing with expressions Statement-level sequence control Prime programs.](https://reader036.fdocuments.net/reader036/viewer/2022081512/56649d585503460f94a36e46/html5/thumbnails/26.jpg)
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.
![Page 27: Chapter 8. Sequence Control Levels of sequence control Sequencing with expressions Statement-level sequence control Prime programs.](https://reader036.fdocuments.net/reader036/viewer/2022081512/56649d585503460f94a36e46/html5/thumbnails/27.jpg)
![Page 28: Chapter 8. Sequence Control Levels of sequence control Sequencing with expressions Statement-level sequence control Prime programs.](https://reader036.fdocuments.net/reader036/viewer/2022081512/56649d585503460f94a36e46/html5/thumbnails/28.jpg)
Primes of up to 4 nodes
![Page 29: Chapter 8. Sequence Control Levels of sequence control Sequencing with expressions Statement-level sequence control Prime programs.](https://reader036.fdocuments.net/reader036/viewer/2022081512/56649d585503460f94a36e46/html5/thumbnails/29.jpg)
Structure theorem
Any flowchart can be represented using only if statements, while statements and sequence control statements