0% found this document useful (0 votes)
10 views37 pages

Sequence

Uploaded by

tc9466
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views37 pages

Sequence

Uploaded by

tc9466
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 37

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,goto,parenthesis)
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
An expression consists of constants, variables, parenthesis,
function calls and operators.

It is of 2 types:

1. Sequencing with arithmetic expression.

2. Sequencing with non-arithmetic expression.

In it firstly, we fetch the operand from memory and then apply


the operation on the operands.

Given an operation with its operands, the operands may be:


· Constants
· Data objects
· Other operations
Sequencing with expressions
Functional composition :

It is a Basic sequence-control mechanism.

It impose tree structure of expression.

Result of operations at lower levels in tree serve as


operands at higher levels. So it must be evaluated first.

For the expression: 3*(a+5)

(a+5 act as operand for next higher level operation so it is


evaluated first.)
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: when expression contain 2 adjacent


occurrence of operators with same level of
precedence then which operator is evaluated first

Precedence and associativity are defined when the


language is defined - within the semantic rules for
expressions.
Precedence and associativity
Associativity:
Left associativity means leftmost occurrence is
evaluated first.
Precedence:
In C language:(Highest Precedence) Postfix ++,--
» Prefix ++,--
» Unary +,-
» * / %
» (Lowest Precedence) Binary +,-
Arithmetic operations /
expressions

Linear representation of the expression tree:


1.Prefix notation(Polish notation)
2.Postfix notation(Reverse polish notation)
3.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
Uniform Evaluation rule:


For each operation node, in expression tree first evaluate
each of its operands, then apply operation to evaluated
operands.

It can not be used in expressions containing conditions.


Lazy Evaluation rule:

Never evaluate operands before applying the


operation, instead always pass the operands
unevaluated and let the operation decide if the
evaluation is needed or not.
Problems
(1)Error Condition

Errors can occur because of coercions of operands
in expression.

Floating point overflow and underflow.



Divide by zero error.

(2)Short circuit boolean expression:



In it, result is determined without evaluating all of
the operands and operators.

(16*a)*(B/16-1) (A>=0)&&(B<10),
(A>=0)||(B/A<10
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.
Non-arithmetic expression

(1)Pattern matching:
It is a fundamental char string operation in many PL.


It is provided by library function rather than operation in
language.

Consider a grammar that recognize odd-length
palindrome over ∑{a, b}
S--->aSa|bSb|0|1

Non-arithmetic expression
(2)Unification:

(a)Fact:
It is a relationship that are stated during a consult
operation which adds new facts and rules to database.
father(A,B)
(b)Rules:
If antecedent of a statement is true, then consequent of
statement must also be true.
ancestor(Mary,Bill)--->mother(Mary,Bill)
sequence control within the
statement

The basic statement in a language


includes:
Assignment statement
Input and output statement
Subprogram call
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
Statement-level sequence control
Explicit sequence control between the statements:

1. Go To statement:It interrupt the normal flow of


control from one statement and transfer it to a
specified place in the program.

2. Break statement:It causes the control to move


forward forward in program to an explicit point at the
end of a given control structure.

3. Continue statement: It forces the next iteration of


the loop to takes place, skipping any code between.
Statement-level sequence control
Advantages of Goto statement
Simple and easy to use

Familiar to programmer

Immediate transfer of control


Direct hardware support for efficient execution


Disadvantages:

Unreadable program, Unreliable and difficult to maintain


Lack of hierarchical program structure


Multiple I/O structure



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
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;
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

You might also like