Nguyen Hua Phung Sequence Control 1 / 43 SinhVienZone.com https://fb.com/sinhvienzonevn SinhVienZone.Com... Nguyen Hua Phung Sequence Control 4 / 43 SinhVienZone.com https://fb.com/sinhv
Trang 1Sequence Control
Dr Nguyen Hua Phung
HCMC University of Technology, Viet Nam
07, 2014
Dr Nguyen Hua Phung Sequence Control 1 / 43
SinhVienZone.com https://fb.com/sinhvienzonevn
SinhVienZone.Com
Trang 3An expression is a syntactic entity whose evaluationeither:
produces a valuefails to terminate → undefinedExamples
4 + 3 * 2(a + b) * (c - a)(b != 0) ? (a/b) : 0
Dr Nguyen Hua Phung Sequence Control 4 / 43
SinhVienZone.com https://fb.com/sinhvienzonevn
SinhVienZone.Com
Trang 4Expressions (cont’d)
Expression Evaluation Mechanism
Expressions have functional composition nature
*+
Dr Nguyen Hua Phung Sequence Control 5 / 43
SinhVienZone.com https://fb.com/sinhvienzonevn
SinhVienZone.Com
Trang 5Infix Notation
(a + b) * (c - d)
Good for binary operatorsUsed in most imperative programming languageMore than two operands?
(b != 0) ? (a/b) : 0Smalltalk:
myBox displayOn: myScreen at: 100@50
Dr Nguyen Hua Phung Sequence Control 6 / 43
SinhVienZone.com https://fb.com/sinhvienzonevn
SinhVienZone.Com
Trang 63 + 4 * 5 = 23, not 35
Evaluation priorities in mathematicsProgramming languages define their ownprecedence levels based on mathematics
A bit different precedence rules among languagescan be confusing
Dr Nguyen Hua Phung Sequence Control 7 / 43
SinhVienZone.com https://fb.com/sinhvienzonevn
SinhVienZone.Com
Trang 7If operators have the same level of precedence, thenapply associativity rules
Mostly left-to-right, except exponentiation operator
An expression contains only one operatorMathematics: associative
Computer: optimization but potential problems
1020∗ 1020∗ 10−20
Dr Nguyen Hua Phung Sequence Control 8 / 43
SinhVienZone.com https://fb.com/sinhvienzonevn
SinhVienZone.Com
Trang 8Dr Nguyen Hua Phung Sequence Control 9 / 43
SinhVienZone.com https://fb.com/sinhvienzonevn
SinhVienZone.Com
Trang 9average = (count == 0) ? 0 : sum / count;
C-based languages, Perl, JavaScript, Ruby
Dr Nguyen Hua Phung Sequence Control 10 / 43
SinhVienZone.com https://fb.com/sinhvienzonevn
SinhVienZone.Com
Trang 10Prefix Notation
Polish Prefix: * + a b - c dCambridge Polish Prefix: (* (+ a b) (- c d))Normal Prefix: *(+(a,b),-(c,d))
Derived from mathematical function f(x,y)Parentheses and precedence is no required,provided the -arity of operator is knownMostly see in unary operators
LISP: (append a b c my_list)
Dr Nguyen Hua Phung Sequence Control 11 / 43
SinhVienZone.com https://fb.com/sinhvienzonevn
SinhVienZone.Com
Trang 11Postfix Notation
Polish Postfix: a b + c d - *Cambridge Polish Postfix: ((a b +) (c d -) *)Normal Postfix: ((a,b)+,(c,d)-)*
Common usage: factorial operator (5!)Used in intermediate code by some compilers
PostScript: (Hello World!) show
Dr Nguyen Hua Phung Sequence Control 12 / 43
SinhVienZone.com https://fb.com/sinhvienzonevn
SinhVienZone.Com
Trang 12Operand Evaluation Order
C program int a = 5;
What is the value of a? 8 20Reason: Side-effect on the operand of the expression
Dr Nguyen Hua Phung Sequence Control 14 / 43
SinhVienZone.com https://fb.com/sinhvienzonevn
SinhVienZone.Com
Trang 13Evaluation Mechanisms
Eager evaluationFirst evaluate all operandsThen operators
How about a == 0 ? b : b / aLazy evaluation
Pass the un-evaluated operands to the operatorOperator decide which operands are requiredMuch more expensive than eager
Lazy for conditional, eager for the rest
Dr Nguyen Hua Phung Sequence Control 16 / 43
SinhVienZone.com https://fb.com/sinhvienzonevn
SinhVienZone.Com
Trang 14Ada: "and", "or" versus "and then", "or else"
Dr Nguyen Hua Phung Sequence Control 18 / 43
SinhVienZone.com https://fb.com/sinhvienzonevn
SinhVienZone.Com
Trang 17Assignment Statements
C-based languages consider assignment as anexpression
while ((ch = getchar()) != EOF) { }
Introduce compound and unary assignmentoperators (+=, -=, ++, –)
Increasing code legibilityAvoiding unforeseen side effects
Dr Nguyen Hua Phung Sequence Control 22 / 43
SinhVienZone.com https://fb.com/sinhvienzonevn
SinhVienZone.Com
Trang 18Control Structures
Control statementsSelecting among alternative control flow pathsCausing the repeated execution of sequences ofstatements
Control structure is a control statement and thecollection of its controlled statements
Dr Nguyen Hua Phung Sequence Control 23 / 43
SinhVienZone.com https://fb.com/sinhvienzonevn
SinhVienZone.Com
Trang 19Two-way Selection
if control_expression then clause
Trang 20Python: indentation matters
Dr Nguyen Hua Phung Sequence Control 25 / 43
SinhVienZone.com https://fb.com/sinhvienzonevn
SinhVienZone.Com
Trang 21Type of selector expression?
How are selectable segments specified?
Execute only one segment or multiple segments?How are case values specified?
What if values fall out of selectable segments?
Dr Nguyen Hua Phung Sequence Control 26 / 43
SinhVienZone.com https://fb.com/sinhvienzonevn
SinhVienZone.Com
Trang 22Case Study: C
switch (index) { case 1:
Multiple segments exited by break
for unrepresented values
Dr Nguyen Hua Phung Sequence Control 27 / 43
SinhVienZone.com https://fb.com/sinhvienzonevn
SinhVienZone.Com
Trang 23Case Study: Pascal
Integer or character
- Single statement
- Blockmultiple values,subrange
for unrepresented values
Dr Nguyen Hua Phung Sequence Control 28 / 43
SinhVienZone.com https://fb.com/sinhvienzonevn
SinhVienZone.Com
Trang 24Mammoth amounts of memory to storeDesign questions:
How is iteration controlled?
Logic, counting
Where should the control appear in the loop?
Pretest and posttest
Dr Nguyen Hua Phung Sequence Control 29 / 43
SinhVienZone.com https://fb.com/sinhvienzonevn
SinhVienZone.Com
Trang 25Counter-Controlled Loops
Counter-controlled loops must have:
Loop variableInitial and terminal valuesStepsize
Dr Nguyen Hua Phung Sequence Control 30 / 43
SinhVienZone.com https://fb.com/sinhvienzonevn
SinhVienZone.Com
Trang 26Case Study: Algol-based
General form
f o r i : = f i r s t to l a s t by s t e p do
l o o p body
end
Semantic
[define end_save]end_save := last
i = firstloop:
if i > end_save goto out
[loop body]
i := i + stepgoto loopout:[undefine end_save]
Trang 27if expr_2 = 0 goto out
[loop body]expr_3goto loopout:
Can be infinite loop
Dr Nguyen Hua Phung Sequence Control 32 / 43
SinhVienZone.com https://fb.com/sinhvienzonevn
SinhVienZone.Com
Trang 28Logically Controlled Loops
Repeat based on Boolean expression rather than acounter
Are more general than counter-controlledDesign issues:
Should the control be pretest or posttest?
Should the logically controlled loop be a specialform of a counting loop or a separate statement?
Dr Nguyen Hua Phung Sequence Control 33 / 43
SinhVienZone.com https://fb.com/sinhvienzonevn
SinhVienZone.Com
Trang 29Case Study: C
loop:
[loop body]goto loopout:
Dr Nguyen Hua Phung Sequence Control 34 / 43
SinhVienZone.com https://fb.com/sinhvienzonevn
SinhVienZone.Com
Trang 30User-Located Loop Control
Programmer can choose a location for loop controlrather than top or bottom
Simple design: infinite loops but include user-locatedloop exits
Languages have exit statements: break and continue
A need for restricted goto statement
Dr Nguyen Hua Phung Sequence Control 35 / 43
SinhVienZone.com https://fb.com/sinhvienzonevn
SinhVienZone.Com
Trang 31What if we replace break by continue?
Dr Nguyen Hua Phung Sequence Control 36 / 43
SinhVienZone.com https://fb.com/sinhvienzonevn
SinhVienZone.Com
Trang 32Iteration Based on Data Structures
Controlled by the number of elements in a datastructure
Iterator:
Called at the beginning of each iterationReturns an element each time it is called in somespecific order
Pre-defined or user-defined iterator
Dr Nguyen Hua Phung Sequence Control 38 / 43
SinhVienZone.com https://fb.com/sinhvienzonevn
SinhVienZone.Com
Trang 34Unconditional Branching
Unconditional branch, or goto, is the most powerfulstatement for controlling the flow of execution of aprogram?s statements
Dangerous: difficult to read, as the result, highlyunreliable and costly to maintain
Structured programming: say no to gotoJava, Python, Ruby: no goto
It still exists in form of loop exit, but they are severelyrestricted gotos
Dr Nguyen Hua Phung Sequence Control 40 / 43
SinhVienZone.com https://fb.com/sinhvienzonevn
SinhVienZone.Com
Trang 36ExpressionsOperator precedence and associativitySide effects
StatementsAssignmentSelection StatementLoop structures
Dr Nguyen Hua Phung Sequence Control 43 / 43
SinhVienZone.com https://fb.com/sinhvienzonevn
SinhVienZone.Com