Download Notes on Control Flow Categories - Programming Languages | CS 403 and more Exams Programming Languages in PDF only on Docsity!
1
November 13, 2001Class 24LanguagesCS 403 - Programming
Start discussion of subprogramsstructures Finish discussion of basic control Go over exam Agenda for Today
(condition tested at end)tested at beginning) or post-test Design issue: Pre-test (condition While loops and their variantsLogically-Controlled Loops
Logically-Controlled Loops
loop statements (1.^ Pascal has separate pretest and posttest logical Language Examples:
while
- do (^) and (^) **repeat
just like in the pretest case (expression for the posttest version is treated2.^ C and C++ also have both, but the control
while (^) - (^) do (^) and (^) do (^) -
while )
5. FORTRAN 77 and 90 have neither4. Ada has a pretest version, but no posttestentered at the beginning--Java has no goto)must be Boolean (and the body can only be3. Java is like C, except the control expression
1. Ada - conditional or unconditional; for any loop;MechanismsUser-Located Loop Control any number of levels
for ... loop
LOOP1:
...
while ... loop
exit when ...
...
...
LOOP2:
end loop
end loop LOOP1;...end loop LOOP2;...exit LOOP1 when .....for ... loop
2. C , C++, and Java -MechanismsUser-Located Loop Control
(^) break
Unconditional; for any loop or
(^) switch
one level only (Java’s can have a label)
There is also has a
(^) continue
statement for
iteration, but does not exit the looploops; it skips the remainder of this
Problem: The “Goto” statementUnconditional Branching
(^) readability
1. Unsigned int constants: Pascal (with colon) Label forms: and Java Some languages do not have them:e.g., Modula-
FORTRAN (no colon)
3. Identifiers in2. Identifiers with colons: ALGOL 60, C
(^) << ... >> : Ada
4. Variables as labels: PL/I difficult to implement^ ^ Highly flexible, but make programs impossible to read andCan be assigned values and passed as parameters
- this or take another English class CS 403 is a “W” course – so it’s either Due in one more week Position paper Assignment #
^ Two general types of parameters: Parameter Matching
Positional: 1^ Two types of matching:parameters in the subprogram header. Formal parameters: Variables that appear aspassed as parameters to a subprogram Actual parameters: Variables in the caller that are
to 1st^ , 2stnd to 2^ nd , 3to 3 (^) rd , etc.rd
(^) In this case, order doesn’t matter. (^) foo(x => my_x, y => my_y)call^ ^ Keyword: Include formal/actual mappings in the
^ Two ways to categorize: Parameter Passing Methods
Copy, referencecopied^ ^ By actual implementation of what is IN, OUT, IN-OUT^ By intended semantics
caller^ Copy: Value copied to and/or from ImplementationsParameter Passing
parameters^ ^ Not a good way to implement IN or OUT^ ^ Cheap in most cases^ Reference: Address copied to callerbig arrays)^ Can be expensive for large structures (e.g.,
^ In: ImplementationCombining Semantics and
By reference: Straightforward By copy: Straightforward (copy both directions)^ In-Out:the formal parameter prior to a local definition By reference: Only works if you disallow uses of By copy: Straightforward^ Out:to formal parameters within the subroutine By reference: Only works if you disallow changes By copy: Straightforward
by reference^ Default mode for arrays is IN-OUTcopy^ Default mode for scalars is IN by C++…
No notion of true OUT parametersbehave as IN only Use const to make array parameters
If IN-OUT implemented by reference, then output is 12. If IN-OUT implemented by copy, then output is 11.} cout << x << endl;foo(x);x = 10;void main(void){} y++;x++;void foo(IN-OUT int y){^ int x; What Happens in this Case?