1. Trang chủ
  2. » Giáo án - Bài giảng

nguyên lý ngôn ngữ lập trình nguyễn hứa phùng sequence control sinhvienzone com

36 78 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 36
Dung lượng 186,11 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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 1

Sequence 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 3

An 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 4

Expressions (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 5

Infix 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 6

3 + 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 7

If 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 8

Dr Nguyen Hua Phung Sequence Control 9 / 43

SinhVienZone.com https://fb.com/sinhvienzonevn

SinhVienZone.Com

Trang 9

average = (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 10

Prefix 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 11

Postfix 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 12

Operand 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 13

Evaluation 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 14

Ada: "and", "or" versus "and then", "or else"

Dr Nguyen Hua Phung Sequence Control 18 / 43

SinhVienZone.com https://fb.com/sinhvienzonevn

SinhVienZone.Com

Trang 17

Assignment 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 18

Control 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 19

Two-way Selection

if control_expression then clause

Trang 20

Python: indentation matters

Dr Nguyen Hua Phung Sequence Control 25 / 43

SinhVienZone.com https://fb.com/sinhvienzonevn

SinhVienZone.Com

Trang 21

Type 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 22

Case 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 23

Case 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 24

Mammoth 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 25

Counter-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 26

Case 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 27

if 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 28

Logically 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 29

Case Study: C

loop:

[loop body]goto loopout:

Dr Nguyen Hua Phung Sequence Control 34 / 43

SinhVienZone.com https://fb.com/sinhvienzonevn

SinhVienZone.Com

Trang 30

User-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 31

What if we replace break by continue?

Dr Nguyen Hua Phung Sequence Control 36 / 43

SinhVienZone.com https://fb.com/sinhvienzonevn

SinhVienZone.Com

Trang 32

Iteration 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 34

Unconditional 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 36

ExpressionsOperator precedence and associativitySide effects

StatementsAssignmentSelection StatementLoop structures

Dr Nguyen Hua Phung Sequence Control 43 / 43

SinhVienZone.com https://fb.com/sinhvienzonevn

SinhVienZone.Com

Ngày đăng: 30/01/2020, 22:16

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

🧩 Sản phẩm bạn có thể quan tâm