Expression and control structure - PowerPoint PPT Presentation

About This Presentation
Title:

Expression and control structure

Description:

Expressions are the fundamental means of specifying computations in ... Associativity in most PL is left-to-right, except for exponentiation op (right-to-left) ... – PowerPoint PPT presentation

Number of Views:14
Avg rating:3.0/5.0
Slides: 21
Provided by: pllabCs
Category:

less

Transcript and Presenter's Notes

Title: Expression and control structure


1
Expression and control structure
  • Kun-Yuan Hsieh
  • kyshieh_at_pllab.cs.nthu.edu.tw
  • Programming Language Lab., NTHU

2
Expressions
  • Expressions are the fundamental means of
    specifying computations in a programming language
  • Expression evaluation depends on the orders of
    operator and operand evaluation
  • Issues
  • Precedence
  • Associativity
  • Order of operand evaluation
  • Side effects

3
Operators
  • A unary operator has one operande.g.
  • A binary operator has two operandse.g. , -, ,
    /
  • A ternary operator has three operandse.g. ?

4
Operator precedence
  • Precedence rules define the order in which
    adjacent operators of different precedence
    levels are evaluated.
  • Ex
  • a 10 b 3 c 4
  • d a b c
  • d (in C) 22

5
Operator associativity
  • Associativity rules define the order in which
    adjacent operators with the same precedence level
    are evaluated
  • Associativity in most PL is left-to-right, except
    for exponentiation op (right-to-left)
  • Ex
  • a 10 b 3 c 4
  • d a b c
  • d (in C) 7

6
Parentheses
  • Precedence and associativity rules can be
    overriden with parentheses
  • a 10 b 3 c 4
  • d (a b) c
  • d (in C) 52
  • Use parentheses when not clear in your code to
    increase readability
  • Ex
  • a 10 b 3 c 4
  • d (in C) abc a(bc) 1081

7
Functional side effects
  • Side effect of a function occurs when a function
    changes one of its parameter or global variable
  • Ex

void main() fun2() a (in C) 8
int a 5 int fun1() a 17 return 3
int fun2() a a fun1()
8
Operator overloading
  • Use of an operator for more than one purpose
  • Increase readability but also decrease
    readability
  • EX
  • Matrix operation
  • A B C, instead of MatrixAdd( MatrixMult( A, B
    ), C )
  • A B is actually implemented as (A AND B)

9
Type Conversions
  • Narrowing conversion converts an object to a type
    that cannot include all of the values of the
    original type e.g., float to int
  • Widening conversion convets an object to a type
    that can include at least approximations to all
    of the values of the original type
    e.g., int to float
  • Coercion is an implicit type conversion

10
Relational and Boolean Expressions
  • Compares the values of its operands
  • Evaluate to some Boolean representation
  • C has no Boolean type--it uses int type with 0
    for false and nonzero for true
  • Ex
  • a 3, b 4, c a 1 gt 2 b
  • c 0

11
Short Circuit Evaluation
  • The result of an expression is determined without
    evaluating all the operands and/or operators
  • EX
  • while ((index lt length) (listindex ! key))
  • I ndex index 1

Table lookup
12
Assignment Statements
  • The operator symbol
  • FORTRAN, BASIC, PL/I, C, C, Java
  • ALGOLs, Pascal, Ada
  • Unary assignment ops
  • count (count count 1)
  • Undetected error in C/C
  • if (x y) is different from if (x y)
  • Java only allows Boolean expressions in if
    statements

13
Control structure
  • Control structure is a control statement and the
    statements whose execution it controls
  • Selection statement provides the means of
    choosing between two or more paths of execution
  • Two-Way Selection Statements
  • Multi-Way Selection Statements

14
Two-Way Selection Statements
  • if-then-else
  • matching else problem
  • rules else matches the most recent then
  • used in Java and most other imperative PL (C,
    Pascal)
  • ALGOL 60 use compound statement for nesting if
  • Ada use end if reserve word

15
Multiple selection constructs
  • FORTRAN IF (exp) N1, N2, N3
  • FORTRAN GO TO (lb1, lb2, , lbN), exp
  • Pascal case (exp) of end
  • C, C switch (index) case
    break default

16
Iterative Statements
  • repeated execution of a statement or compound
    statement is accomplished either by iteration or
    recursion
  • Counter-Controlled Loops
  • Design Issues
  • 1. What are the type and scope of the loop
    variable?
  • 2. What is the value of the loop variable at loop
    termination?
  • 3. Should it be legal for the loop variable or
    loop parameters to be changed in the loop body,
    and if so, does the change affect loop control?
  • 4. Should the loop parameters be evaluated only
    once, or once for every iteration?

17
examples
  • Iterative statements
  • FORTRAN 90 DO lavel var init, final ,step
  • ALGOL 60 for count 1 step 1 until 10 do
  • for index 1, 4, 13, 41 step 2 until 47,
  • 3 index while index lt 1000,
  • 34, 2, -24, do
  • ? 1, 4,13, 41,43, 45, 47, 147, 441, 34, 2, -24
  • Pascal for var init (to downto) final do
  • Ada for k in 1..10 loop
  • C, C, Java for (k 0 k lt 10 k)
  • Iteration on data list
  • Perl _at_names Bob, John, Tom
  • foreach name (_at_names) print name

18
Unconditional Branching
  • Problem readability
  • Some languages do not have them e.g., Java
  • Loop exit statements are restricted and somewhat
    camouflaged gotos

19
Guarded statements
  • Guarded commands
  • suggested by Dijkstra (1975)
  • all boolean exps are evaluated
  • one of the true-stmts is nondeterministically
    chose for execution
  • if all exps are false, run-time error occurs
  • if i 0 -gt sum sum i
  • i gt j -gt sum sum j
  • i lt j -gt sum sum i
  • fi

20
Guarded statements
  • do ltboolean expgt -gt ltstmtgt
  • ltboolean expgt -gt ltstmtgt
  • ltboolean expgt -gt ltstmtgt
  • od
  • all boolean exps are evaluated on each iteration
  • one of the true stmts are nondeterministically
    executed, and then the loop is evaluated again
  • when all exps are false, the loop terminates
  • sort q1 to q4 such that q1 lt q2 lt q3 lt q4
  • do q1 gt q2 -gt temp q1 q1 q2 q2 temp
  • q2 gt q3 -gt temp q2 q2 q3 q3 temp
  • q3 gt q4 -gt temp q3 q3 q4 q4 temp
  • od
Write a Comment
User Comments (0)
About PowerShow.com