Top Banner
Structure of Programming Language Statements
47

Structure of Programming Language Statements. Expression.

Dec 27, 2015

Download

Documents

Loren Watts
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Structure of Programming Language Statements. Expression.

Structure of Programming Language

Statements

Page 2: Structure of Programming Language Statements. Expression.

Statements

Expression

Page 3: Structure of Programming Language Statements. Expression.

What is an expression ?

• The notion of value is central to programming.

• Program variables get instantiated to values at run-time.

– Integer variables to integer values

– String variables to array of characters etc.

• With this perspective, we could define an expression

simply as:

An expression is a formal description of a value.

Page 4: Structure of Programming Language Statements. Expression.

Expression Examples

• 2

• 2 * 5

• F(4) + 2*5 // Need to define function F

• A < B

• A < B \/ C = D // A,B,C,D are variables

• P(A, B) \/ Q(C, D) // P,Q are predicates

Page 5: Structure of Programming Language Statements. Expression.

Prefix, Infix, Postfix

• Notation Position of Function Examples

• Prefix Left of argument(s) sqrt(16), f(3,4)

• Infix Between two arguments 3 f 4, 3 + 4

• Postfix Right of arguments 16 sqrt, 3 4 f

Page 6: Structure of Programming Language Statements. Expression.

Postfix evaluation - Example

Expression Code Stack Contents

3 5 + 8 6 - * push 3 <3>

^ push 5 <3,5>

add <8>

3 5 + 8 6 - * push 8 <8,8>

^ push 6 <8,8,6>

sub <8, 2>

3 5 + 8 6 - * mul <16>

^

Page 7: Structure of Programming Language Statements. Expression.

C, C++, and Java have over 50 operators and 17 different levels of precedence

Pascal: not, unary - *, /, div, mod, +, -

Ada: ** *, /, mod, rem unary -, not +, -, &and, or, xor

Operator Precedence

Page 8: Structure of Programming Language Statements. Expression.

Arithmetic Expressions: Operator Associativity Rule

• The operator associativity rules for expression evaluation define the

order in which adjacent operators with the same precedence level are

evaluated

• Typical associativity rules

– Left to right, except **, which is right to left

– Sometimes unary operators associate right to left (e.g., in FORTRAN)

• APL is different; all operators have equal precedence and all operators

associate right to left

Page 9: Structure of Programming Language Statements. Expression.

- Use relational operators and operands of various

types

- Evaluate to some boolean representation

- Operator symbols used vary somewhat among

languages (!=, /=, .NE., <>, #)

Relational Expressions

Page 10: Structure of Programming Language Statements. Expression.

- Operands are boolean and the result is boolean

- Operators:

FORTRAN 77 FORTRAN 90 C Ada

.AND. and && and

.OR. or || or

.NOT. not ! not

xor

- C has no boolean type--it uses int type with 0

for false and nonzero for true

-

Boolean Expressions

Page 11: Structure of Programming Language Statements. Expression.

Relational and Boolean Expressions: No Boolean Type in C

• C has no Boolean type--it uses int type with 0 for false and nonzero

for true

• One odd characteristic of C’s expressions:

a < b < c is a legal expression, but the result is not what you might

expect:

– Left operator is evaluated, producing 0 or 1

– The evaluation result is then compared with the third operand (i.e.,

c)

Page 12: Structure of Programming Language Statements. Expression.

Evaluating an expression without evaluating all the

operands.

e.g. (a > b) and (c > 5)

If we know that a > b is false, then there is no need

To determine whether (c > 5) is true.

Short Circuit Evaluation

Page 13: Structure of Programming Language Statements. Expression.

Pascal: does not use short-circuit evaluation index := 1; while (index <= length) and (LIST[index] <> value) do index := index + 1

If value is not in LIST, then ???

Short Circuit Evaluation

Page 14: Structure of Programming Language Statements. Expression.

C, C++, and Java: use short-circuit evaluation for the usual Boolean operators (&& and ||), but also provide bitwise Boolean operators that are not short circuit (& and |)

Ada: programmer can specify either (short-circuit is specified with and then and or else)

FORTRAN 77: short circuit, but any side-affected place must be set to undefined

Short-circuit evaluation exposes the potential problem of side effects in expressions e.g. (a > b) || (b++ / 3)

Short circuit evaluation

Page 15: Structure of Programming Language Statements. Expression.

Conditional Expressions

• Conditional Expressions

– C-based languages (e.g., C, C++)

– An example:

average = (count == 0)? 0 : sum / count

– Evaluates as if written like

if (count == 0) average = 0

else average = sum /count

Page 16: Structure of Programming Language Statements. Expression.

Let expressions

• Example: let square(x) = x*x in square(square(2))

• Of the form: let function_definition in

sub_expression

• The function definition defines a function f in

equational form.

• The sub-expression contains function applications of f

• We assume that definition of f is non-recursive.

Page 17: Structure of Programming Language Statements. Expression.

Let expressions

• Evaluation proceeds by replacing applications of f

in sub-expression with the definition of f

• Example: let square(x) = x*x in

square(square(2))

• square(2) * square(2)

• 2 * 2 * 2 * 2 = 16

• Let expressions allow for function definitions.

• Their evaluation is same as macro-expansion.

Page 18: Structure of Programming Language Statements. Expression.

Statement

Assignment statement

Page 19: Structure of Programming Language Statements. Expression.

Assignment Statements

• The general syntax

<target_var> <assign_operator> <expression>

• The assignment operator

= FORTRAN, BASIC, PL/I, C, C++, Java

:= ALGOLs, Pascal, Ada

Page 20: Structure of Programming Language Statements. Expression.

Assignment Statements: Compound Operators

• A shorthand method of specifying a commonly needed form of assignment

• Introduced in ALGOL; adopted by C• Example

a = a + b

is written as

a += b

Page 21: Structure of Programming Language Statements. Expression.

Mixed-Mode Assignment

• Assignment statements can also be mixed-mode, for exampleint a, b;

float c;

c = a / b;• In Pascal, integer variables can be assigned

to real variables, but real variables cannot be assigned to integers

• In Java, only widening assignment coercions are done

• In Ada, there is no assignment coercion

Page 22: Structure of Programming Language Statements. Expression.

Statement

Selection

Page 23: Structure of Programming Language Statements. Expression.

Selection Statements

• A selection statement provides the means of

choosing between two or more paths of

execution

• Two general categories:

– Two-way selectors

– Multiple-way selectors

Page 24: Structure of Programming Language Statements. Expression.

Two-Way Selection Statements

• General form:if control_expression

then clauseelse clause

• Design Issues:

– In C, Python, and C++, the control

expression can be arithmetic

– In languages such as Ada, Java, Ruby, and

C#, the control expression must be Boolean

Page 25: Structure of Programming Language Statements. Expression.

Two-Way Selection: Examples

• FORTRAN: IF (boolean_expr) statement

• Problem: can select only a single statement; to select

more, a GOTO must be used, as in the following example

IF (.NOT. condition) GOTO 20

...

20 CONTINUE

• This problem was solved in FORTRAN 77

Page 26: Structure of Programming Language Statements. Expression.

Two-Way Selection: Examples

• ALGOL 60:

if (boolean_expr)

then statement (then clause)

else statement (else clause)

• The statements could be single or compound

Page 27: Structure of Programming Language Statements. Expression.

Nesting Selectors

• Java example

if (sum == 0)

if (count == 0)

result = 0;

else result = 1;

• Which if gets the else?

• Java's static semantics rule: else matches with

the nearest if

Page 28: Structure of Programming Language Statements. Expression.

Nesting Selectors (continued)

• To force an alternative semantics, compound

statements may be used:

if (sum == 0) {

if (count == 0)

result = 0;

}

else result = 1;

• The above solution is used in C, C++, and C#

• Perl requires that all then and else clauses to be

compound

Page 29: Structure of Programming Language Statements. Expression.

Multiple-Way Selection

• Early multiple selectors:

– FORTRAN arithmetic IF (a three-way selector)

IF (arithmetic expression) N1, N2, N3

– Segments require GOTOs

Page 30: Structure of Programming Language Statements. Expression.

Multiple-Way Selection

• Modern multiple selectors

– C’s switch statement

switch (expression) {

case const_expr_1: stmt_1;

case const_expr_n: stmt_n;

[default: stmt_n+1]

}

Page 31: Structure of Programming Language Statements. Expression.

31

Switch in C, C++, Jave

switch (x)

default:

if (prime(x))

case 2: case 3: case 5: case 7:

process_prime(x);

else

case 4: case 6: case 8:

case 9: case 10:

process_composite(x);

Page 32: Structure of Programming Language Statements. Expression.

Multiple-Way Selection in C#

• It has a static semantics rule that disallows the implicit execution of

more than one segment

– Each selectable segment must end with an unconditional branch

(goto or break)

• The control expression and the case constants can be strings

switch (value) {

case -1: Negatives++;break;

case 0: Zeros++; goto case 1;

case 1: Positives++;break;

default: Console.WriteLine(“!!!\n”); }

Page 33: Structure of Programming Language Statements. Expression.

Multiple-Way Selection: Examples

• Design choices for C’s switch statement

1. Control expression can be only an integer type

2. Selectable segments can be statement

sequences, blocks, or compound statements

3. default clause is for unrepresented values (if

there is no default, the whole statement does

nothing)

Page 34: Structure of Programming Language Statements. Expression.

The Ada case statement

case Next_Char is

when ‘I’ => Val := 1;

when ‘V’ => Val := 5;

when ‘X’ => Val := 10;

when ‘C’ => Val := 100;

when ‘D’ => Val := 500;

when ‘M’ => Val := 1000;

when others => raise Illegal_Numeral;

end case;

Page 35: Structure of Programming Language Statements. Expression.

Statement

Iterative

Page 36: Structure of Programming Language Statements. Expression.

Iterative Statements

• The repeated execution of a statement or

compound statement is accomplished either by

iteration or recursion

Page 37: Structure of Programming Language Statements. Expression.

Counter-Controlled Loops

• A counting iterative statement has a loop variable, and a means of specifying the initial and terminal, and stepsize values

• 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?

Page 38: Structure of Programming Language Statements. Expression.

Iterative Statements: Examples

• FORTRAN 90 syntax

DO label var = start, finish [, stepsize]

• Stepsize can be any value but zero

• Design choices:

1. Loop variable must be INTEGER

3. The loop variable cannot be changed in the loop;

because they are evaluated only once, it does not

affect loop control

Page 39: Structure of Programming Language Statements. Expression.

Iterative Statements

• Pascal’s for statement

for variable := initial (to|downto) final do

statement

• Design choices:

1. Loop variable must be an ordinal type of usual scope

2. After normal termination, loop variable is undefined

3. The loop variable cannot be changed in the loop but

they are evaluated just once, so it does not affect

loop control

Page 40: Structure of Programming Language Statements. Expression.

Iterative Statements: Examples

• Adafor var in [reverse] discrete_range loop

...

end loop

• A discrete range is a sub-range of an integer or enumeration type

• Scope of the loop variable is the range of the loop

• Loop variable is implicitly undeclared after loop termination

Page 41: Structure of Programming Language Statements. Expression.

Iterative Statements: Examples

• C’s for statementfor ([expr_1] ; [expr_2] ; [expr_3]) statement

• The expressions can be whole statements, or even statement sequences, with the statements separated by commas

– The value of a multiple-statement expression is the value of the last statement in the expression

• Everything can be changed in the loop

• The first expression is evaluated once, but the other two are evaluated with each iteration

Page 42: Structure of Programming Language Statements. Expression.

Iterative Statements: Examples

• C++ differs from C in two ways:

The initial expression can include variable

definitions (scope is from the definition to the

end of the loop body)

• Java and C#

–Differs from C++ in that the control

expression must be Boolean

Page 43: Structure of Programming Language Statements. Expression.

Iterative Statements: Logically-Controlled Loops

• Repetition control is based on a Boolean

• Design issues:

– Pre-test or post-test?

– Should the logically controlled loop be a special case of the counting loop statement ?

• General forms:

while (ctrl_expr) do

loop body loop body

while (ctrl_expr)

Page 44: Structure of Programming Language Statements. Expression.

Iterative Statements: Logically-Controlled Loops: Examples

• Pascal has separate pre-test and post-test logical

loop statements (while-do and repeat-until)

• C and C++ also have both, but the control

expression for the post-test version is treated just

like in the pre-test case (while-do and do- while)

• Java is like C, except the control expression must

be Boolean (and the body can only be entered at

the beginning -- Java has no goto

Page 45: Structure of Programming Language Statements. Expression.

Iterative Statements: Logically-Controlled Loops: Examples

• Ada has a pretest version, but no post-test

• FORTRAN 77 and 90 have neither

• Perl has two pre-test logical loops, while and

until, but no post-test logical loop

Page 46: Structure of Programming Language Statements. Expression.

Iterative Statements: User-Located Loop Control Mechanisms break and continue

• C , C++, Java, Python, Ruby, C# : break statement

Unconditional; for any loop or switch; one level only

• Java and C# have a labeled break statement:

control transfers to the label

• An alternative: continue statement; it skips the

remainder of this iteration, but does not exit the

loop

Page 47: Structure of Programming Language Statements. Expression.

Unconditional Branching

• Transfers execution control to a specified place in the program

• Represented one of the most heated debates in 1960’s and 1970’s

• Well-known mechanism: goto statement

• Major concern: Readability

• Some languages do not support goto statement (e.g., Module-2 and Java)

• C# offers goto statement (can be used in switch statements)