cse 452 programming languages n.
Skip this Video
Loading SlideShow in 5 Seconds..
CSE 452: Programming Languages PowerPoint Presentation
Download Presentation
CSE 452: Programming Languages

CSE 452: Programming Languages

149 Vues Download Presentation
Télécharger la présentation

CSE 452: Programming Languages

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. CSE 452: Programming Languages Control Flow

  2. Outline • Control Structures • Selection Statements • Iterative Statements • Unconditional Branches • Subprograms and Procedures

  3. Iterative Statements • Repeated execution of a statement or compound statement • accomplished either by iteration or recursion; • here we look at iteration, because recursion is a unit-level control (e.g., using functions) • General design issues for iteration control statements: • How is the iteration controlled? • Counter-controlled vs logical-controlled • Where should the control mechanism appear in the loop? • pretest (before loop body is executed) vs posttest (after loop body is executed)

  4. Iterative Statements • Counter-Controlled Loops for (i = init_value; i < terminal_value; i+=stepsize) { … } • Design Issues: • What are the type and scope of the loop variable? • What is the value of the loop variable at loop termination? • 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? • Should the loop parameters be evaluated only once, or once for every iteration? Loop parameters Loop variable

  5. Counter-Controlled Loops • FORTRAN 95 • Syntax: Do label var = initial, terminal [, stepsize] • stepsize can be any value but zero • parameters can be expressions • Design choices: • Loop var must be integer; loop parameters can be expressions • The loop var cannot be changed in the loop, but loop parameters can because they are evaluated only once, it does not affect loop control • Single entry structure – loop can be entered through Do statement

  6. Counter-Controlled Loops • Fortran 95: Do label var = init_expression, terminal_expression [, step_expression] Operational Semantics for Fortran 95 Do statement init_value = init_expression terminal_value = terminal_expression step_value = step_expression do_var = init_value iteration_count = max(int((terminal_value–init_value+step_value)/step_value), 0) loop: if iteration_count  0 goto out [loop body] do_var = do_var + step_value iteration_count = interation_count – 1 goto loop out: …

  7. Counter-Controlled Loops • Another form of Do statement for FORTRAN 95 [name:] DO variable = initial, terminal [, stepsize] … END DO [name] • Uses a special word for closing: END DO

  8. Counter-Controlled Loops • Ada • Syntax: for var in [reverse] discrete_range loop ... end loop; • reverse indicates that values of discrete range are assigned in reverse order • Step size is always one (or next element in discrete_range)

  9. Counter-Controlled Loops • Ada Design choices: • Type of the loop var is that of the discrete range • discrete range is subrange of integer or enumeration type, such as 1..10 or Monday..Friday • Scope of loop var is the loop body (it is implicitly declared); loop var does not exist outside the loop Count : Float := 1.35; for Count in 1..10 loop Sum := Sum + Count; end loop; … Count gets the value of 1.35 • The loop var cannot be changed in the loop, but the discrete range can; it does not affect loop control • The discrete range is evaluated just once

  10. Counter-Controlled Loops • C-based languages • Syntax: for ([expr_1] ; [expr_2] ; [expr_3]) loop body • Loop body can be single, compound, or null statement • 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 for (i = 0, j = 10; j == i; i++) … • All expressions of C’s for statement are optional • If expr_2 is absent, it is an infinite loop

  11. Counter-Controlled Loops for ([expr_1] ; [expr_2] ; [expr_3]) loop body • Operational Semantics: expr_1 % initialization (evaluate once) loop: if expr_2 = 0 goto out % loop control (each iter) [loop_body] expr_3 % increment loop counter? goto loop out: …

  12. Counter-Controlled Loops • Design choices for C: • There is no explicit loop variable or loop parameters • All involved variables can be changed in the loop body • It is legal to branch into loop body • The first expression is evaluated once, but the other two are evaluated with each iteration

  13. Counter-Controlled Loops • C++ • Differs from C in two ways: • The control expression can also be Boolean • The initial expression can include variable definitions (scope is from the definition to the end of the loop body) for (int i=0; i < len; i++) { … } • Java • Differs from C++ in that the control expression must be Boolean

  14. Logically-Controlled Loops • Repetition control is based on a Boolean expression, rather than a counter • More general than counter-controlled loops • Every counting loop can be built with a logical loop, but not vice-versa • Design Issues: • Pretest or postest? • Should logically controlled loop be a special form of counting loop statement or a separate statement?

  15. Logically-Controlled Loops • Pascal has separate pretest and posttest logical loop statements (while-do and repeat-until) • C and C++ also have both while (count > 0) { … } do { … } while (count > 0); • Legal to branch into both while and do loop bodies

  16. Logically-Controlled Loops • Ada has a pretest version, but no posttest • FORTRAN 77, 90, and 95 have neither • Perl has two pretest logical loops, while and until, but no posttest logical loop while (count > 0) { … } until (count == 0) { … }

  17. User-Located Loop Control • User modify the control flow of program • Design issues: • Should the conditional mechanism be an integral part of the exit? • Should only one loop body be exited or can enclosing loops also be exited?

  18. User-Located Loop Control • Exit statement: • Unconditional unlabeled exit: break (C, C++) for (index=0; index<10; index++) { … if (value < 0) break; } • Unconditional labeled exit: break (Java, C#), last (Perl) C#: outerLoop: for (row=0; row<numRows; row++) for (col = 0; col < numCols; col++) { sum += matrix[row][col]; if (sum > 1000) break outerLoop; } Perl: LINE: while (<STDIN>) { lastLINE if /^$/; ... }

  19. User-Located Loop Control • Skip the rest of loop body: • C/C++ have unlabeled control statement (continue) while (sum < 1000) { value = getNextValue(); if (value < 0) continue; sum += value; } • Java, Perl, and C# have statements similar to continue, except they can include labels that specify which loop is continued

  20. Iteration Based on Data Structures • Loops are controlled by number of elements in a data structure • Perl, Javascript, PHP, and C# have such statements Perl: @values = (1, 2, 3, 4, 5); foreach $value (@values) { print “Value is $value\n”; } C#: String[] strList = {“Bob”, “John”, “Carol” }; foreach (String name in strList) …

  21. Iteration Based on Data Structures • More general approach • uses a user-defined data structure and a user-defined function (called an iterator) to go through the structure’s elements • Java has a Collection interface that contains two methods: boolean add(Object obj) - adds elements to collection Iterator iterator() - used to visit the elements in the collection one by one.

  22. Unconditional Branching • Transfers execution control to a specified location in the program goto label • Problem: readability • Some languages do not have them: e.g., Java • Loop exit statements are restricted and somewhat camouflaged goto’s • Label forms: • Unsigned int constants: Pascal (with colon) FORTRAN (no colon) • Identifiers with colons: ALGOL 60, C • Variables as labels: PL/I

  23. Subprograms • Two fundamental abstraction facilities in programming language: • Process abstraction – represented by subprograms • Data abstraction • General characteristics of subprograms: • A subprogram has a single entry point • The caller is suspended during execution of the called subprogram • Control always returns to the caller when the called subprogram’s execution terminates

  24. Subprograms • A subprogram definition is a description of the actions of the subprogram abstraction • A subprogram call is an explicit request that the subprogram be executed • A subprogram is active if, after being called, it has begun execution but has not yet completed that execution • A subprogram header is the first line of the definition • Specifies that the following syntactic unit is a subprogram of some particular kind - using a special word (function, procedure, etc) • Provides name of subprogram • Specifies the list of formal parameters Fortran: Subroutine Adder(parameters) Ada: procedure Adder(parameters)

  25. Subprograms • The parameter profile (signature) of a subprogram is the number, order, and types of its parameters • The protocol of a subprogram is its parameter profile plus, if it is a function, its return type • Subprograms can have declarations as well as definitions • Subprogram declaration provides the subprogram’s protocol but do not include their bodies • Function declarations in C/C++ are called prototypes

  26. Parameters • A formal parameter is a dummy variable listed in the subprogram header and used in the subprogram • An actual parameter represents a value or address used in the subprogram call statement void doNothing (int formal_param) { … } main() { int actual_param; doNothing(actual_param); }

  27. Parameters • Actual/Formal Parameter Correspondence • Binding of actual to formal parameters • Positional parameters • First actual param bound to first formal param, etc • Keyword parameters • Name of formal param to which actual param is bound is specified with actual param Ada: Sumer( Length => My_Length, List => My_Array, Sum => My_Sum ); • Advantage: order is irrelevant • Disadvantage: user must know the formal parameter’s names

  28. Parameters • Default values of formal parameters • Allowed by C++, Fortran 95, Ada and PHP • Default value is used if no actual parameter is passed to the formal parameter Ada: function Compute_Pay( Income : Float; Exemptions : Integer := 1; Tax_Rate : Float ) return Float pay := Compute_Pay (20000.00, Tax_Rate => 0.15); • C# allows methods to accept variable number of params of the same type public void DisplayList(params int[] list ) { foreach (int nextValue in list) { Console.WriteLine(“Next value {0}”, nextValue); } }

  29. Procedures and Functions • Procedures provide user-defined statements • Functions provide user-defined operators • Value produced by function is returned to the calling code, effectively replacing the call itself float power(float base, float exp) result = 3.4 * power(10.0, x); • C-based languages • have only functions (but they can behave like procedures) • Can be defined to return no value if the return type is void

  30. Design Issues for Subprograms • What parameter passing methods are provided? • Are parameter types checked? • Are local variables static or dynamic? • What is the referencing environment of a passed subprogram? • Are parameter types in passed subprograms checked? • Can subprogram definitions be nested? • Can subprograms be overloaded? • Are subprograms allowed to be generic? • Is separate or independent compilation supported?

  31. Local Referencing Environments • Local variables: variables defined inside subprograms • their scope is the body of subprogram in which they are defined • Stack-dynamic: bound to storage when subprogram begins execution, unbound when its execution terminates • Advantages: • Support for recursion • Storage for local variables of active subprogram can be shared with local variables of inactive subprograms • Disadvantages: • Allocation/deallocation time • Indirect addressing (indirectness because the place in stack where a particular local variable is stored can only be determined at run time) • Subprograms cannot be history sensitive • Cannot retain data values of local variables between calls • Static: bound to storage at compile-time

  32. Parameter Passing: Semantic Models • Semantic models for formal parameters • In mode – can receive data from corresponding actual parameters • Actual value is either copied to caller, or an access path is transmitted • Out mode – can transmit data to actual parameters • Inout mode – can do both receive/transmit data

  33. Parameter Passing: Implementation • Pass by value (in mode) • Value of actual parameter is used to initialize formal parameter, which acts as a local variable void foo (int a) { a = a + 1; } void main() { int b = 2; foo(b); } • Normally implemented by copying actual parameter to formal parameter • Can also be implemented by transmitting access path to the value of actual parameter as long as cell is write protected • Disadvantages: • Requires more storage (duplicated space) • Cost of the moves (if the parameter is large)

  34. Parameter Passing: Implementation • Pass by result (out mode) • Local’s value is passed back to the caller • No value transmitted to the subprogram • Formal parameter acts as local variable, but just before control is transferred back to caller, its value is transmitted to actual parameter • Disadvantages: • If value is copied back (as opposed to access paths), need extra time and space • Pass-by-result can create parameter collision e.g. procedure sub1(y: int, z: int); ... sub1(x, x); • Value of x in the caller depends on order of assignments at the return

  35. Parameter Passing: Implementation • Pass by value-result (or pass-by-copy) • Combination of pass-by-value and pass-by-result • Formal parameter acts as local variable in subprogram • Actual parameter is copied to formal parameter at subprogram entry and copied back at subprogram termination • Share disadvantages of pass-by-result and pass-by-value • Requires multiple storage for parameters • Requires time for copying values • Problems with parameter collision

  36. Parameter Passing: Implementation • Pass by reference (or pass-by-sharing) • transmits an access path (e.g., address) to the called subprogram • Called subprogram is allowed to access actual parameter in the calling program unit • Advantage: • passing process is efficient (no copying and no duplicated storage) • Disadvantages: • Slower accesses to formal parameters due to additional level of indirect addressing • Allows aliasing void fun (int &first, int &second); … fun(total, total);

  37. Parameter Passing: Implementation • Pass-by-reference • Collisions due to array elements can also cause aliases void fun(int &first, int &second) fun(list[i], list[j]); /* where i=j */ void fun1(int &first, int *a); fun1(list[i], list); • Collisions between formal parameters and nonlocal variables that are visible int *global; void sub(int *param) { void main() { extern int *global; extern int *global; … … } sub(global); … }

  38. Parameter Passing: Implementation • Pass by Name • Another type of inout mode • Actual parameter is textually substituted for the corresponding formal parameters • Actual binding of value and address is delayed until formal parameter is assigned or referenced • Advantage: • flexibility of late binding • Disadvantage: • very expensive related to other parameter passing • Not used in any widely used language • Another Example: • Used at compile time by macros, and for generic subprograms in C++

  39. Pass-by-value int m=8, i=5; foo(m); print m; # prints 8 # since m is passed by-value ... proc foo (byval b) { b = i + b; # b is byval so it is essentially a local variable # initialized to 8 (the value of the actual back in # the calling environment) # the assignment to b cannot change the value of m back # in the main program }

  40. Pass-by-reference int m=8, i=5; foo(m); print m; # prints 13 # since m is passed by-reference ... proc foo (byref b) { b = i + b; # b is byref so it is a pointer back to the actual # parameter back in the main program (containing 8 initially) # the assignment to b actually changes the value in m back # in the main program # i accesses the variable in the main via scope rules }

  41. Pass-by-value-result int m=8, i=5; foo(m); print m; # prints 13 # since m is passed by-value-result ... proc foo (byvres b) { b = i + b; # b is byves so it copies value of the actual # parameter (containing 8 initially) # new value of b is copied back to actual parameter # in the main program # i accesses the variable in the main via scope rules }

  42. Pass-by-name array A [1..100] of int; array A [1..100] of int; int i=5; int i=5; foo(A[i],i); foo(A[i]); print A[i]; # prints A[6] print A[i]; # prints A[5] ... # so prints 7 ... # not sure what # good example # a problem here... proc foo (name B,name k) { proc foo (name B) { k = 6; int i = 2; B = 7; B = 7; } } # text substitution does this proc foo { proc foo { i = 6; int i = 2; A[i] = 7; A[i] = 7; } }