Download presentation
Presentation is loading. Please wait.
Published byQuentin Todd Modified over 9 years ago
1
Gary MarsdenSlide 1University of Cape Town Statements & Expressions Gary Marsden Semester 2 – 2000
2
Gary MarsdenSlide 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)
3
Gary MarsdenSlide 3University of Cape Town Boolean Boolean values crop up everywhere Some languages use true/false, some use zero/non-zero Can 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
4
Gary MarsdenSlide 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
5
Gary MarsdenSlide 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 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
6
Gary MarsdenSlide 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
7
Gary MarsdenSlide 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
8
Gary MarsdenSlide 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
9
Gary MarsdenSlide 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
10
Gary MarsdenSlide 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
11
Gary MarsdenSlide 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
12
Gary MarsdenSlide 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 compilation Variables external to a file can be inculded using the “external” keyword –no type checking though
13
Gary MarsdenSlide 13University of Cape Town Parameter passing Three basic things happen to parameters 1.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
14
Gary MarsdenSlide 14University of Cape Town Going in Values can be passed in by “value” or by “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”
15
Gary MarsdenSlide 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 subroutine At the end of evaluation, the value of the formal parameter is copied to the actual parameter (which must be a variable)
16
Gary MarsdenSlide 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
17
Gary MarsdenSlide 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.