programming language ch4
TRANSCRIPT
-
8/6/2019 Programming Language Ch4
1/21
CompanyLogo
@
Programming Languagechap.4 SEQUENCE CONTROL
Internet Management Technology Lab.
-
8/6/2019 Programming Language Ch4
2/21
Sawan kumar
Contents
Introduction
Sequence Control Structures
Sequencing with Arithmetic Expressions
Sequencing with Non-Arithmetic Expressions
Sequence Control between Statements
-
8/6/2019 Programming Language Ch4
3/21
SungKyunKwan Univ.
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 aprogram
-
8/6/2019 Programming Language Ch4
4/21
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
Implicit control vs. explicit control Implicit control
defined by the language and not modified until user
redefine it Explicit control
programmer uses to redefine implicit control sequence(parenthesis)
-
8/6/2019 Programming Language Ch4
5/21
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
-
8/6/2019 Programming Language Ch4
6/21
SungKyunKwan Univ.
Sequencing with arithmetic expressions(2/5)
Infix notation
Only suitable for binary operations (Thus used with prefix)
For more than one infix operator, it is inherentlyambiguous
Parenthesis is used to explicitly annotate the order
-
8/6/2019 Programming Language Ch4
7/21
SungKyunKwan Univ.
Sequencing with arithmetic expressions(3/5)
Implicit control rules
Hierarchy of operations (precedence) e.g. Ada **, abs, not : Exponential absolute negation
* / mod : Multiplication, division,
+ - : Unary addition and subtraction
+ - & : Binary addition and subtraction = < > : Relational
and or xor : Boolean operations
Associative left-right associativity (+, -, others)
right-left associativity (exponential)
-
8/6/2019 Programming Language Ch4
8/21
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 (parallelprocessing 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 );
-
8/6/2019 Programming Language Ch4
9/21
SungKyunKwan Univ.
Sequencing with arithmetic expressions(5/5)
Issues in Evaluating Expressions Side effects
a * foo(x) + a; say a= 1; foo(x)generates 3
if each term is evaluated 1 * 3 + 2 =5
if a is evaluated only once 1 * 3 + 1= 4
if evaluate foo(x) first 2 * 3 + 2 = 8
Error Condition No solution other than exceptional
statements
Short-circuit expression Use the characteristics of and oror operation
a || b - a is true b is not evaluateda && b - a is false b is not evaluatede.g.b = 9;if ( a = 4 || b = 3 )printf (" a = %d b = %d\n");
(continued)
-
8/6/2019 Programming Language Ch4
10/21
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 | 1matches 00100 -> 00A00 -> 0A0 -> Aapplied term rewriting 00A00 is a term rewrite of
00100
term rewriting
A restricted form of pattern match
e.g. Given string a1a2a3a4 and -> .if = a
3then a
1a
2a
4we say a1a2a4 is term rewrite of a1a2a3a4
e.g.) in MLfun fact(1) = 1| fact(N:int) = N * fact(N - 1);
fun length( nil ) = 0| length( a :: y) = 1 +
length( y )
-
8/6/2019 Programming Language Ch4
11/21
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 validsubstitution 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 = TomRule : GrandOarentOf( X,Y ) :- ParentOf(X,Z), ParentOf(Z,Y)then query : GrandParentOf (x,John) => x = sam
-
8/6/2019 Programming Language Ch4
12/21
SungKyunKwan Univ.
Sequencing with Non-arithmetic expressions(3/3)
Backtracking
Backup to the previous subgoal that matched and tryanother possible goal for it to match when currentsubgoal is failed
-
8/6/2019 Programming Language Ch4
13/21
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 beexecuted in order
Alternation Two Sequences may form alternatives and either one is
executed Iteration
A sequence of statements are executed repeatedly, zero ormore times
-
8/6/2019 Programming Language Ch4
14/21
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 specifiedcondition holds
Ex) if A=0 then goto NEXT
-
8/6/2019 Programming Language Ch4
15/21
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 levelprogrammers
May simulate any control structure
Problems
Leading unstructured programming (no goto in ML)
Superfluous any program with goto can be translated intoanother one w/o goto
Undefined control with nested structure
-
8/6/2019 Programming Language Ch4
16/21
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 switchstatement
While (a > b){
A;
if ( k > 0 ) break;B;
}
-
8/6/2019 Programming Language Ch4
17/21
SungKyunKwan Univ.
Sequence Control Between Statements (5/9)
Structured Programming Design
Structured program is A program that contains no GOTOs
Structured programming emphasizes
Hierarchical design of program structure with thecomposition, alternation, and iteration
Use structured sequence control that represents hierarchicaldesign
Textual sequences corresponds to the execution sequence
Unique purpose group of statements then it is copied
-
8/6/2019 Programming Language Ch4
18/21
SungKyunKwan Univ.
Sequence Control Between Statements (6/9)
Structured Sequence Control (1/4)
Control statements that only contains one-in one-outcontrol 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 hardwaresupported branch and jump instruction
Case statements are implemented using ajump table toavoid the testing of the same variable
Jump table : A vector whose components are unconditionaljump instructions
-
8/6/2019 Programming Language Ch4
19/21
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 Ktimes (COBOL)
Repetition while condition holds
Reevaluate the condition each time after BODY
Ex) While testdo 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
-
8/6/2019 Programming Language Ch4
20/21
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., butPASCALgoto 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
-
8/6/2019 Programming Language Ch4
21/21
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 smallerproper 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 whileand ifstatements called a well-structured program