DOC PREVIEW
UT Arlington CSE 3302 - Programming Languages

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

2008-2-121CSE 3302 Programming LanguagesControl IidSChengkai Li, Weimin HeSpring 2008Expressions and StatementsLecture 9 – Control I, Spring 2008 1CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008Control• Control:what gets executed, when, and in what order.• Abstraction of control:– Expression– Statement– Exception Handling– Procedures and functionsLecture 9 – Control I, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20082Expression vs. Statement• In pure (mathematical) form:– Expression:• no side effect•return a valuereturn a value– Statement:• side effect• no return value• Functional languages aim at achieving this pure form• No clear-cut in most languagesLecture 9 – Control I, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20083Expression• Constructed recursively:– Basic expression (literal, identifiers)– Operators, functions, special symbols• Number of operands:–unary, binary, ternary operators• Operator, function: equivalent concepts– (3+4)*5 (infix notation)– mul( add(3,4), 5)• “*”(“+”(3,4),5) (Ada, prefix notation)• (* (+ 3 4) 5) (LISP, prefix notation)Lecture 9 – Control I, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20084Postfix notation• PostScript:• %!PS /Courier findfont 20 scalefont setfontsetfont 72 500 moveto (Hello world!) show showpagehttp://en.wikipedia.org/wiki/PostScriptLecture 9 – Control I, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20085Expression and Side Effects• Side Effects:– changes to memory, input/output– Side effects can be undesirableBut a program without side effects does nothing!–But a program without side effects does nothing!• Expression:– No side effect: Order of evaluating subexpressions doesn’t matter (mathematical forms)– Side effect: Order mattersLecture 9 – Control I, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200862008-2-122Applicative Order Evaluation(Strict Evaluation)• Evaluate the operands first, then apply operators (bottom-up evaluation)(subexpressions evaluated, no matter whether they are needed)they are needed)• But is 3+4 or 5-6 evaluated first?Lecture 9 – Control I, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20087*-4+365Order MattersC:int x=1;int f(void) {x=x+1;return x;}Java:class example{ static int x = 1;public static int f(){ x = x+1;return x; main(){printf(“%d\n”, x + f());return 0;}Lecture 9 – Control I, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20088return x; }public static void main(String[] args){ System.out.println(x+f()); }}43Many languages don’t specify the order, including C, java.– C: usually right-to-left– Java: always left-to-right, but not suggested to rely on that.Expected Side Effect• Assignment (expression, not statement)x = (y = z) (right-associative operator)•x++ ++xWhy?x++, ++xint x=1;int f(void) {return x++;}main(){printf(“%d\n”, x + f());return 0;}Lecture 9 – Control I, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 20089ySequence Operator• (expr1, expr2, …, exprn)– Left to right (this is indeed specified in C)– The return value is exprnx=1;y=2;x = (x=x+1, y++, x+y);printf(“%d\n”,x);Lecture 9 – Control I, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200810Non-strict evaluation• Evaluating an expression without necessarily evaluating all the subexpressions.hii l i• short-circuit Boolean expression• if-expression, case-expressionLecture 9 – Control I, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200811Short-Circuit Evaluation• if (false and x) … if (true or x)…– No need to evaluate x, no matter x is true or false• What is it good for?–if (i <=lastindex and a[i]>=x)if (i <=lastindex and a[i]>=x)…– if (p != NULL and p->next==q)…• Ada: allow both short-circuit and non short-circuit.– if (x /= 0) and then (y/x > 2) then ...– if (x /= 0) and (y/x > 2) then ... ?– if (ptr = null) or else (ptr.x = 0) then ...– if (ptr = null) or (ptr.x = 0) then ... ?Lecture 9 – Control I, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008122008-2-123if-expression• if (test-exp, then-exp, else-exp)ternary operator– test-exp is always evaluated first–Either then-exp or else-exp are evaluated, not bothpp,– if e1 then e2 else e3 (ML)– e1 ? e2 : e3 (C)• Different from if-statemnt?Lecture 9 – Control I, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200813case-expression• ML:case color of red => “R’’ |blue => “B’’ |green => “G” |_ => “AnyColor”;Lecture 9 – Control I, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200814Normal order evaluation(lazy evaluation)• When there is no side-effect:Normal order evaluation (Expressions evaluated in mathematical form)– Operation evaluated before the operands are evaluated;Odldlh–Operands evaluated only when necessary.• int double (int x) { return x+x; }int square (int x) { return x*x; }Applicative order evaluation : square(double(2)) = …Normal order evaluation : square(double(2)) = …Lecture 9 – Control I, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200815What is it good for?(p!=NULL) ? p->next : NULLint if_exp(bool x, int y, int z){ if (x) return y; else return z;}if_exp(p!=NULL, p->next, NULL);• With side effect, it may hurt you:int get_int(void) {int x;scanf(“%d” &x);return x;}Lecture 9 – Control I, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200816Examples• Call by Name (Algol60)• Macro#define swap(a, b) {int t; t = a; a = b; b = t;}{}– What are the problems here?Lecture 9 – Control I, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200817Unhygienic Macros• Call by Name (Algol60)• Macro#define swap(a, b) {int t; t = a; a = b; b = t;}main (){int t 2;main (){int t=2;Lecture 9 – Control I, Spring 2008CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 200818int t=2;int


View Full Document

UT Arlington CSE 3302 - Programming Languages

Documents in this Course
Smalltalk

Smalltalk

11 pages

Syntax

Syntax

5 pages

Syntax

Syntax

5 pages

JAVA

JAVA

57 pages

Semantics

Semantics

41 pages

Control

Control

74 pages

Load more
Download Programming Languages
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Programming Languages and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Programming Languages 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?