Content:

Reading: Textbook Chapter 3

 

Lecture 4:

Expressions

Precedence and Associativity

 

Lecture 5:

Mixed type expressions

Statements and Expressions

Program Style

 


Lecture 4

 

Expressions

fig. 3-1

3-1.gif (28998 bytes)

E.g. a = 5

E.g. b = x + 1

E.g. i = i + 1

 

Expression

x

y

Expr. Value

Expr. result

x = y + 2

10

5

7

x = 7

x = x / y

10

5

2

x = 2

x = y % 4

10

5

1

x = 1

 

 

fig.3-2

3-2.gif (13135 bytes)

Expression

value of a before

value of a after

expression value

a++

10

11

10

a = a + 1

10

11

11

a--

10

9

10

a = a - 1

10

9

9

 


Precedence and Associativity

 

 

Precedence

 

Associativity

fig. 3-5

3-5.gif (23768 bytes)

        (((3 * 8) / 4) % 4) * 5

since the operators have the same precedence and have left associativity.

fig. 3-6

3-6.gif (21095 bytes)

        a += (b *= (c -= 5))

since the operators have the same precedence and have right associativity. The above is then expanded to

        a = a + (b = b * (c = c – 5)).

 


Lecture 5

 

Mixed type expressions

 


Statements and Expressions

 

{

    /* Local Declarations */

    int x; 

    int y;

    /* Statements */

    x=1;

    y=2;

    …

}

 


Predefined Constants

Such as:

#define TAX_RATE  0.15

To place a semicolon ":" at the end of the command,

#define TAX_RATE 0.15;

then, for the following statement:

        salaryTax = TAX_RATE * income;

the statement after replacement becomes:

        salaryTax = 0.15; * income;


Program Style

 

- End of Lectures 4 and 5-