Upload
quentin-todd
View
213
Download
0
Embed Size (px)
Citation preview
Gary Marsden Slide 1University of Cape Town
Statements & Expressions
Gary MarsdenSemester 2 – 2000
Gary Marsden Slide 2University of Cape Town
Expressions
“spare the rod and spoil the child”Expressions are comprised of operands and
operators Languages have precise precedence rules
about which operand should be evaluated first– 4 + 4 * 3 = 24 or 16
This is not universal (though almost so)– APL evaluates operators from right to left
Operators usually binary (dyadic) or unary (monadic)
Gary Marsden Slide 3University of Cape Town
Boolean
Boolean values crop up everywhereSome languages use true/false, some use
zero/non-zeroCan use short-circuit evaluation
– in a&&b, if ‘a’ is false, don’t evaluate ‘b’– in a||b, if ‘a’ is true, don’t evaluate ‘b’– side effect problems
Operators can be confusing to novices– &&, AND, and then– ||, OR, or else
Gary Marsden Slide 4University of Cape Town
Assignment statements
General form a:= b– ‘a’ is a reference and ‘b’ is a value
In C, assignment is an expression, not a statement: allows a = b = c = 5;
Type incompatibility is handled in different ways– some resolved through automatic coercion
• int -> real is OK (widening)• real -> int looses information
– Information losses Pascal not allowed, ok in C, but• float -> int is truncated• double -> float is rounded
Gary Marsden Slide 5University of Cape Town
Sequence and control
FORTRAN introduced ‘IF’– IF (C1) L1, L2, L3– really a glorified goto jumping to L1 when C1<0,
L2 if C1 = 0 and L3 if C1>0
Nested IF’s– if C1 then if C2 then S1 else S2
Dangling else solve by– else binds to inner if in Pascal and C– use ‘elsif’ in Ada and Modula 2– use switch or case
Gary Marsden Slide 6University of Cape Town
Conditional Iteration
While statement a big favourite– while C do S (S may never be executed)
Pascal introduced Repeat– repeat S until C (S executed at least once)
Less structured version – Loop– loop S end loop
• where S contains exit when C
– this allows multiple exits
Gary Marsden Slide 7University of Cape Town
Fixed iteration
The for statement– loops for a set number of times– control variable should be ordinal (Algol60 used
reals)– shouldn’t change control variable in loop body– shouldn’t force at least one execution (Algol66)– value of the loop variable at end of loop is a
problem• some languages limit loop variable scope to loop body• officially undefined in Pascal
– Languages with enumerated types also permit iterators• for x in 1..10 do
Gary Marsden Slide 8University of Cape Town
Exceptions
Introduced in PL/1, exceptions allow programs to react to exception circumstances by jumping to an exception handler
Usually there is a chunk of code (“try block” in Java, “frame” in Ada) which could throw an exception– control is passed (caught) to exception handler– if handled correctly control is returned to
• end of frame / block in Ada / Java• line after exception generator in PL/1
Gary Marsden Slide 9University of Cape Town
Sub-programs
Complexity control and stepwise refinement required that code be split into sub-programs
Some languages (Pascal,Ada) have functions and procedures
Other languages (C++) treat procedures as a special case of function– good idea
Gary Marsden Slide 10University of Cape Town
Declaring sub-programs
Usually something to denote difference between program and sub-program– Pascal: “Procedure” “Function” “Program”– Clean: “Module”
Form is similar – each allow (local) declarations
“Formal” parameters matched to “actual” during execution– FORTRAN and K&R C had no type checking on
parameters
Gary Marsden Slide 11University of Cape Town
FORTRAN’s model
Fortran supports parameter passing but also has “Common” variables
Each subroutine has a “Common” list, were values are matched on position, not name
• sub1: COMMON A,B,C• sub2: COMMON D,E,F• sub3: COMMON P,X
– this is called “overlaying”
Fortran also supports separate compilation of subroutines– promoted NAG library
Gary Marsden Slide 12University of Cape Town
Other approaches
The Algol family store code in one file to permit parameter type checking
This does little for code re-use– Modula-2 cludges round this using extra files
and EXPORT lists identical to Clean
C has always had separate compilationVariables external to a file can be inculded
using the “external” keyword– no type checking though
Gary Marsden Slide 13University of Cape Town
Parameter passing
Three basic things happen to parameters1. Value is passed in but unchanged (in)2. Value is undefined initially but a value is
placed in the parameter after subroutine completion (out)
3. Parameter passes in value which is modified by executing subroutine (in-out)
Although there are three basic concepts, each language implements these differently
Gary Marsden Slide 14University of Cape Town
Going in
Values can be passed in by “value” orby “constant-value” (Ada)
– The idea of constant-value is that the value of the parameter cannot be altered in the subroutine
– This is to stop accidental modification of parameters which were intended as “in” but end up as “in-out”
Gary Marsden Slide 15University of Cape Town
Coming out
This really only exists in Ada in its pure form
Ada uses “Call by result”This creates a instantiation of the formal
parameter for use in the subroutineAt the end of evaluation, the value of the
formal parameter is copied to the actual parameter (which must be a variable)
Gary Marsden Slide 16University of Cape Town
Updating
Two main methods– Call by value-result: Another Ada foible, like
“Call by result”• value of actual instantiated formal
– Call by reference: Most common, where a pointer is passed to the subroutine
• this is the most common form
Gary Marsden Slide 17University of Cape Town
Other thoughts
“Call-by-name” – Algol copied text of actual parameter to replace formal parameter
Default values to formal parameters– Good old Ada; a bit redundant in my opinion
Overloading – provided different formal parameters – Ada, C++, Java
Functional languages – no ‘out’ or ‘in-out’ parameters