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

Nội dung

C o e on nZ Dr Nguyen Hua Phung 07, 2014 hV ie HCMC University of Technology, Viet Nam in m Sequence Control https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung Sequence Control / 43 C o Statements Program Units e on Expressions hV ie nZ in m Outline https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung Sequence Control / 43 C o e hV ie nZ on An expression is a syntactic entity whose evaluation either: produces a value fails to terminate → undefined Examples 4+3*2 (a + b) * (c - a) (b != 0) ? (a/b) : in m Expressions https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung Sequence Control / 43 C o b (a + b) * (c - d) c d ie a - nZ + on e Expression Evaluation Mechanism Expressions have functional composition nature * hV Expression Syntax Infix Prefix Postfix in m Expressions (cont’d) https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung Sequence Control / 43 C o e on (a + b) * (c - d) hV ie nZ Good for binary operators Used in most imperative programming language More than two operands? (b != 0) ? (a/b) : Smalltalk: myBox displayOn: myScreen at: 100@50 in m Infix Notation https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung Sequence Control / 43 C o e on + * = 23, not 35 hV ie nZ Evaluation priorities in mathematics Programming languages define their own precedence levels based on mathematics A bit different precedence rules among languages can be confusing in m Precedence https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung Sequence Control / 43 C o e hV ie nZ on If operators have the same level of precedence, then apply associativity rules Mostly left-to-right, except exponentiation operator An expression contains only one operator Mathematics: associative Computer: optimization but potential problems 1020 ∗ 1020 ∗ 10−20 in m Associativity https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung Sequence Control / 43 C o e APL nZ on Alter the precedence and associativity (A + B) * C Using parentheses, a language can even omit precedence and associativity rules hV ie Advantage: simple Disadvantage: writability and readability in m Parentheses https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung Sequence Control / 43 C o e If statement ie nZ on if (count == 0) average = 0; else average = sum / count; hV Conditional Expression average = (count == 0) ? : sum / count; C-based languages, Perl, JavaScript, Ruby in m Conditional Expressions https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung Sequence Control 10 / 43 C o e hV ie nZ on Polish Prefix: * + a b - c d Cambridge 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 known Mostly see in unary operators LISP: (append a b c my_list) in m Prefix Notation https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung Sequence Control 11 / 43 C o Type must be int Exact value - Stmt sequences hV ie nZ on e switch (index) { - Block case 1: case 3: odd += 1; sumodd += index; break; Multiple segments exited by break case 2: case 4: even += 1; sumeven += index; break; default: printf("Error in switch") } for unrepresented values in m Case Study: C https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung Sequence Control 27 / 43 C o on - Single statement - Block multiple values, subrange ie nZ case exp of : clause_A , : clause_B : clause_C : clause_D else clause_E end e Integer or character hV for unrepresented values in m Case Study: Pascal https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung Sequence Control 28 / 43 C o ie nZ on e Cause a statement or collection of statements to be executed zero, one or more times Essential for the power of the computer Programs would be huge and inflexible Large amounts of time to write Mammoth amounts of memory to store Design questions: How is iteration controlled? Logic, counting hV Where should the control appear in the loop? Pretest and posttest in m Iterative Statements https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung Sequence Control 29 / 43 C o e on hV ie nZ Counter-controlled loops must have: Loop variable Initial and terminal values Stepsize in m Counter-Controlled Loops https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung Sequence Control 30 / 43 C o e Semantic on General form nZ constant hV ie f o r i : = f i r s t to l a s t by s t e p l o o p body end Known number of iterations before executing in m Case Study: Algol-based [define end_save] end_save := last i = first loop: if i > end_save goto out [loop body] i := i + step goto loop out: [undefine end_save] https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung Sequence Control 31 / 43 C o e on nZ General form ie f o r ( expr_1 ; expr_2 ; expr_3 ) l o o p body hV Can be infinite loop in m Case study: C Semantic expr_1 loop: if expr_2 = goto out [loop body] expr_3 goto loop out: https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung Sequence Control 32 / 43 C o e hV ie nZ on Repeat based on Boolean expression rather than a counter Are more general than counter-controlled Design issues: Should the control be pretest or posttest? Should the logically controlled loop be a special form of a counting loop or a separate statement? in m Logically Controlled Loops https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung Sequence Control 33 / 43 C o on loop: if ctrl_expr is false goto out [loop body] goto loop out: loop: [loop body] if ctrl_expr goto loop hV ie nZ while (ctrl_expr) loop body Semantics e Forms loop body while (ctrl_expr); in m Case Study: C https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung Sequence Control 34 / 43 C o e hV ie nZ on Programmer can choose a location for loop control rather than top or bottom Simple design: infinite loops but include user-located loop exits Languages have exit statements: break and continue A need for restricted goto statement in m User-Located Loop Control https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung Sequence Control 35 / 43 C o e nZ on while (sum < 1000) { getnext(value); if (value < 0) break; sum += value; } hV ie What if we replace break by continue? in m Case Study: C https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung Sequence Control 36 / 43 C o e hV ie nZ on Controlled by the number of elements in a data structure Iterator: Called at the beginning of each iteration Returns an element each time it is called in some specific order Pre-defined or user-defined iterator in m Iteration Based on Data Structures https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung Sequence Control 38 / 43 C o e on hV ie nZ String[] strList = {"Bob","Carol","Ted"}; foreach (String name in strList) Console.WriteLine("Name:{0}", name); in m Case Study: C https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung Sequence Control 39 / 43 C o e hV ie nZ on Unconditional branch, or goto, is the most powerful statement for controlling the flow of execution of a program?s statements Dangerous: difficult to read, as the result, highly unreliable and costly to maintain Structured programming: say no to goto Java, Python, Ruby: no goto It still exists in form of loop exit, but they are severely restricted gotos in m Unconditional Branching https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung Sequence Control 40 / 43 C o e on hV ie nZ to be continued in m Program Units https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung Sequence Control 42 / 43 C o e hV ie nZ on Expressions Operator precedence and associativity Side effects Statements Assignment Selection Statement Loop structures in m Summary https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung Sequence Control 43 / 43 ... statements Control structure is a control statement and the collection of its controlled statements in m Control Structures https://fb .com/ sinhvienzonevn Dr Nguyen Hua Phung Sequence Control 23... https://fb .com/ sinhvienzonevn Dr Nguyen Hua Phung Sequence Control 22 / 43 C o e hV ie nZ on Control statements Selecting among alternative control flow paths Causing the repeated execution of sequences... should the control appear in the loop? Pretest and posttest in m Iterative Statements https://fb .com/ sinhvienzonevn Dr Nguyen Hua Phung Sequence Control 29 / 43 C o e on hV ie nZ Counter-controlled

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