Programming Language Chap.: 4 Sequence Control
Programming Language Chap.: 4 Sequence Control
Company
Logo
@
Internet Management Technology Lab.
Contents
Introduction
Sequence Control Structures
Sequencing with Arithmetic Expressions
Sequencing with Non-Arithmetic Expressions
Sequence Control between Statements
Sawan kumar
Introduction
Program control involves
Sequence control
control of the order of execution of the operations
Data control
control of transmission of data among subprograms of a
program
SungKyunKwan Univ.
Sequence Control Structures
Structures used in expressions
precedence rules and parenthesis
Structures used between group of statements
conditional and iteration statements
Structures used between subprograms
subprogram calls
SungKyunKwan Univ.
Sequencing with arithmetic expressions(1/5)
Prefix Notation
Operators come first, then the operands
e.g. (a + b) * (c - a) => * + a b - c a
No ambiguity and no parenthesis needed
Number of arguments for an operator must be known a
priori
Relatively easy to decode using stack mechanism
Postfix notation
An operator follows its operands
e.g. (a+b) *(c-a) => a b + c a - *
No ambiguity and no parenthesis needed
Relatively easy to decode using stack mechanism
SungKyunKwan Univ.
Sequencing with arithmetic expressions(2/5)
Infix notation
Only suitable for binary operations (Thus used with prefix)
SungKyunKwan Univ.
Sequencing with arithmetic expressions(3/5)
Implicit control rules
Hierarchy of operations (precedence) e.g. Ada
SungKyunKwan Univ.
Sequencing with arithmetic expressions(4/5)
Issues in Evaluating Expressions
Uniform Evaluation rules (eager and lazy)
Eager
evaluate the operands as soon as they appear (parallel
processing SNOBOL)
Lazy Evaluation
Delay evaluation of operands as late as possible (LISP)
foo( x )
{ ...
if ( a > 0 )
a = x;
else a = a + 1;
}
foo( b/a );
SungKyunKwan Univ.
Sequencing with arithmetic expressions(5/5)
Issues in Evaluating Expressions (continued)
Side effects
a * foo(x) + a; say a= 1; foo(x)
generates 3
if each term is evaluated 1 * 3 + 2 =
5
a || b - a is true b is not evaluated
if a is evaluated only once 1 * 3 + 1 a && b - a is false b is not evaluated
=4 e.g.
b = 9;
if evaluate foo(x) first 2 * 3 + 2 = 8 if ( a = 4 || b = 3 )
printf (" a = %d b = %d\n");
Error Condition
No solution other than exceptional
statements
Short-circuit expression
Use the characteristics of “and” or
“or” operation
SungKyunKwan Univ.
Sequencing with Non-arithmetic expressions(1/3)
Pattern Matching
Pattern matching operation
An operation matches and assigns a set of variables to a
predefined template
e.g. (palindromes)
A -> 0A0 | 1A1 | 0 | 1
matches 00100 -> 00A00 -> 0A0 -> A
applied term rewriting 00A00 is a term rewrite of
00100
term rewriting
A restricted form of pattern match
e.g. Given string a1a2a3a4 and α -> β. e.g.) in ML
if α = a3 then a1a2βa4 fun fact(1) = 1
we say a1a2βa4 is term rewrite of a1a2a3a4 | fact(N:int) = N * fact(N - 1);
SungKyunKwan Univ.
Sequencing with Non-arithmetic expressions(2/3)
Pattern Matching (continued)
Unification and substitution
In Prolog, Database consists of facts and rules
Substitution
replacement of a string to another one
Unification
A pattern match to determine if the query has a valid
substitution consistent with the rules and facts in the database
Fact : ParentOf( Ann, John )
Fact : ParentOf( Sam, Ann )
Fact : ParentOf( Tom, John )
Query : ParentOf( x, John )
X = Ann, x = Tom
Rule : GrandOarentOf( X,Y ) :- ParentOf(X,Z), ParentOf(Z,Y)
then query : GrandParentOf (x,John) => x = sam
SungKyunKwan Univ.
Sequencing with Non-arithmetic expressions(3/3)
Backtracking
Backup to the previous subgoal that matched and try
another possible goal for it to match when current
subgoal is failed
SungKyunKwan Univ.
Sequence Control Between Statements (1/9)
Basic statements
Statements that apply operations to data objects
Considered as a unit of step
e.g. : assignment operations, subprogram calls,
input/output statements
Statement Level Sequence Control
Composition
Statements may be placed in a textual sequence and be
executed in order
Alternation
Two Sequences may form alternatives and either one is
executed
Iteration
A sequence of statements are executed repeatedly, zero or
more times
SungKyunKwan Univ.
Sequence Control Between Statements (2/9)
Explicit Sequence Control (1/3)
Goto statement (1/2)
Unconditional goto
Transfers control to the labeled statement
Ex) goto NEXT
Conditional goto
Transfers control to the labeled statement only if the specified
condition holds
Ex) if A=0 then goto NEXT
SungKyunKwan Univ.
Sequence Control Between Statements (3/9)
Explicit Sequence Control (2/3)
Goto statement (2/2)
Advantages
Direct hardware support for efficient execution
Simple and easy use and easy understanding for low level
programmers
May simulate any control structure
Problems
Leading unstructured programming (no goto in ML)
Superfluous any program with goto can be translated into
another one w/o goto
Undefined control with nested structure
SungKyunKwan Univ.
Sequence Control Between Statements (4/9)
Explicit Sequence Control (3/3)
Break Statement
Control to move forward to an explicit point at the end of
given control structure
That is, exit the immediately enclosing while, for, or switch
statement
While (a > b)
{
A;
…
if ( k > 0 ) break;
B;
…
}
<Example of a break statement>
SungKyunKwan Univ.
Sequence Control Between Statements (5/9)
Structured Programming Design
Structured program is
A program that contains no GOTOs
SungKyunKwan Univ.
Sequence Control Between Statements (6/9)
Structured Sequence Control (1/4)
Control statements that only contains one-in one-out
control sequences (no goto in ML)
Compound statement
A sequence of statements that may be treated as a single
unit in construction of large one
Conditional statement
Expresses alternation of two or more statements, or optional
execution of a single statement
Ex) IF statement, CASE statement
If statements are implemented using the usual hardware
supported branch and jump instruction
Case statements are implemented using a jump table to
avoid the testing of the same variable
Jump table : A vector whose components are unconditional
jump instructions
SungKyunKwan Univ.
Sequence Control Between Statements (7/9)
Structure Sequence Control (2/4)
Iteration Statement (1/2)
Simple repetition
Repeat a fixed number of times
Ex) perform BODY K times (COBOL)
Repetition while condition holds
Reevaluate the condition each time after BODY
Ex) While test do BODY
Repetition while incrementing a counter
Initial value, final value, and the increment
Ex) for l := 1 step 2 until 30 do BODY
Indefinite repetition
Used when the exit condition is complex and hard to express
In C, all 4 forms can be written by for statement
SungKyunKwan Univ.
Sequence Control Between Statements (8/9)
Structure Sequence Control (3/4)
Iteration Statement (2/2)
Advantages
Easy to debug, understand, and verify
Disadvantages
Multiple exit loops
Can be replaced with “exit” in ADA or “break” in C., but PASCAL
goto must be used in the for loop
Do-while-do
The condition is checked in the middle of the loop
Exceptional conditions
Unexpected end-of-file, subscript range error, bad data…
Ex) raise statement in ADA raise BAD DATA
SungKyunKwan Univ.
Sequence Control Between Statements (9/9)
Structure Sequence Control (4/4)
Proper program
Formal model of control structure, as a flow chart which;
Has a single entry arc
Has a single exit arc,
Has a path from the entry arc to each node and from each
node to the exit arc
Prime program
A proper program that cannot be subdivided into smaller
proper programs
Composite program
A program that is not a prime
The Structure theorem
Bohm and Jacobini (1966)
Any prime program could be converted into tone using only while
and if statements called a well-structured program
SungKyunKwan Univ.