Title: CSE 452: Programming Languages
1CSE 452 Programming Languages
2Outline
- Control Structures
- Selection Statements
- Iterative Statements
- Unconditional Branches
- Subprograms and Procedures
3Iterative 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)
4Iterative Statements
- Counter-Controlled Loops
- for (i init_value i lt terminal_value
istepsize) -
-
- 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
5Counter-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
6Counter-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_valueinit_v
aluestep_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
7Counter-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
8Counter-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)
9Counter-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
10Counter-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 Cs for statement are optional
- If expr_2 is absent, it is an infinite loop
11Counter-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
12Counter-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
13Counter-Controlled Loops
- include ltstdio.hgt
- main()
-
- int count, i 2
- count 2
- / count 5 /
- if (count 2 0)
- goto loop
-
- for (i0 ilt5 i)
- loop printf("id\n", i)
-
count 2 i 2 i 3 i 4 count
5 i 0 i 1 i 2 i 3 i 4
14Counter-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 i0 i lt len i)
- Java
- Differs from C in that the control expression
must be Boolean
15Logically-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?
16Logically-Controlled Loops
- Pascal has separate pretest and posttest logical
loop statements (while-do and repeat-until) - C and C also have both
- while (count gt 0)
-
-
-
- do
-
- while (count gt 0)
- Legal to branch into both while and do loop bodies
17Logically-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 gt 0)
-
-
- until (count 0)
-
-
18User-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?
19User-Located Loop Control
- Exit statement
- Unconditional unlabeled exit break (C, C)
- for (index0 indexlt10 index)
-
- if (value lt 0) break
-
- Unconditional labeled exit break (Java, C),
last (Perl) - C outerLoop for (row0 rowltnumRows row)
- for (col 0 col lt numCols
col) - sum matrixrowcol
- if (sum gt 1000)
- break outerLoop
-
- Perl LINE while (ltSTDINgt)
- last LINE if // ...
-
20User-Located Loop Control
- Skip the rest of loop body
- C/C have unlabeled control statement (continue)
- while (sum lt 1000)
- value getNextValue()
- if (value lt 0) continue
- sum value
-
- Java, Perl, and C have statements similar to
continue, except they can include labels that
specify which loop is continued
21Iteration Based on Data Structures
- Loops are controlled by number of elements in a
data structure - Perl, Javascript, PHP, and C have such
statements - Perl _at_values (1, 2, 3, 4, 5)
- foreach value (_at_values)
- print Value is value\n
-
- \
- C String strList Bob, John,
Carol - foreach (String name in strList)
-
22Iteration 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 structures 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.
23Unconditional 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 gotos - Label forms
- Unsigned int constants Pascal (with colon)
FORTRAN (no colon) - Identifiers with colons ALGOL 60, C
- Variables as labels PL/I
24Subprograms
- 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 subprograms execution terminates
25Subprograms
- 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)
26Subprograms
- 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 subprograms
protocol but do not include their bodies - Function declarations in C/C are called
prototypes
27Parameters
- 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)
-
28Parameters
- 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 gt My_Length,
- List gt My_Array,
- Sum gt My_Sum )
- Advantage order is irrelevant
- Disadvantage user must know the formal
parameters names
29Parameters
- 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 gt
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) -
30Procedures 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
31Design 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?
32Local 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
33Parameter 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
34Parameter 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)
35Parameter Passing Implementation
- Pass by result (out mode)
- Locals 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
36Parameter 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
37Parameter 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)
38Parameter Passing Implementation
- Pass-by-reference
- Collisions due to array elements can also cause
aliases - void fun(int first, int second)
- fun(listi, listj) / where ij /
- void fun1(int first, int a)
- fun1(listi, 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)
-
-
39Parameter 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
40Pass-by-value
- int m8, i5
- 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
41Pass-by-reference
- int m8, i5
- 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
42Pass-by-value-result
- int m8, i5
- 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
43Pass-by-name
- array A 1..100 of int array A
1..100 of int - int i5 int i5
- foo(Ai,i) foo(Ai)
- print Ai prints A6 print Ai
prints A5 - ... 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 - Ai 7 Ai 7
-