Академический Документы
Профессиональный Документы
Культура Документы
3-1
3-2
3-3
unallocated cells
7 true 3.14 allocated cells
undefined
3-4
3-5
Simple variables
When a simple variable is declared, a storage cell is allocated for it. Assignment to the simple variable updates that storage cell. At the end of the block, that storage cell is deallocated. Animation (Ada):
declare n: Integer; begin n := 0; n := n+1; end;
1 0 ?
3-6
The component variables can be inspected and updated either totally or selectively.
3-7
xmas today today xmas xmas today 2004 ? 2004 ? ? ? ? 2004 dec ?dec dec ? ? ? 2525 ? 25 ? ?
3-8
3-9
dateA dateB dateA dateB 2004 ? 2005 2004 2004 jan jan jan ? jan 1 1 1 ? 1
3-11
dateR dateS
2004 1 1
2004 12 25
3-12
3-13
Lifetime (1)
Every variable is created (or allocated) at some definite time, and destroyed (or deallocated) at some later time when it is no longer needed. A variables lifetime is the interval between its creation and destruction. A variable occupies storage cells only during its lifetime. When the variable is destroyed, the storage cells that it occupied may be deallocated (and subsequently allocated for some other purpose).
3-14
Lifetime (2)
A global variables lifetime is the programs run-time. It is created by a global declaration. A local variables lifetime is an activation of a block. It is created by a declaration within that block, and destroyed on exit from that block.
3-15
Commands
A command (or statement) is a PL construct that will be executed to update variables. Commands are characteristic of imperative and OO (but not functional) PLs.
Forms of commands:
skips assignments
procedure calls
sequential commands conditional commands iterative commands.
3-16
Skips
A skip is a command with no effect.
Typical forms:
; in C and Java null; in Ada.
3-17
Assignments
An assignment stores a value in a variable.
Single assignment:
V = E; in C and Java V := E; in Ada
3-18
Procedure calls
A procedure call achieves its effect by applying a procedure to some arguments. Typical form:
P(E1, , En);
Here P determines the procedure to be applied, and E1, , En are evaluated to determine the arguments. Each argument may be either a value or (sometimes) a reference to a variable. The net effect of the procedure call is to update variables. The procedure achieves this effect by updating variables passed by reference, and/or by updating global variables. (But updating its local variables has no net effect.)
3-19
Sequential commands
Sequential, conditional, and iterative commands (found in all imperative/OO PLs) are ways of composing commands to achieve different control flows. Control flow matters because commands update variables, so the order in which they are executed makes a difference. A sequential command specifies that two (or more) commands are to be executed in sequence. Typical form:
C1 C2
3-20
Conditional commands
A conditional command chooses one of its subcommands to execute, depending on a condition. An if-command chooses from two subcommands, using a boolean condition.
3-21
If-commands (1)
Typical forms (Ada and C/Java, respectively):
if E then C1 else C2 end if; if (E) C1 else C2
E must be of type Boolean
3-22
If-commands (2)
Generalisation to multiple conditions (in Ada):
if E1 then C1 elsif E2 then C2 elsif En then Cn else C0 end if;
E1, , En must be of type Boolean
if E1, , Ei-1 all yield false but Ei yields true, then Ci is executed; otherwise C0 is executed.
3-23
Case-commands (1)
In Ada:
case E is when v1 => C1 when vn => Cn when others => C0 end case;
E must be of some primitive type other than Float
Case-commands (2)
In C and Java:
switch (E) { case v1: C1 case vn: Cn default: C0 }
E must be of integer type
if the value of E equals some vi, then Ci, , Cn, C0 are all executed; otherwise only C0 is executed.
3-25
3-26
3-28
Iterative commands
An iterative command (or loop) repeatedly executes a subcommand, which is called the loop body. Each execution of the loop body is called an iteration. Classification of iterative commands:
Indefinite iteration: the number of iterations are not predetermined. Definite iteration: the number of iterations are predetermined.
3-29
Indefinite iteration
Indefinite iteration is most commonly supported by the while-command. Typical forms (Ada and C/Java):
while E loop C end loop; while (E) C
3-30
Definite iteration
Definite iteration is characterized by a control sequence, a predetermined sequence of values that are successively assigned (or bound) to a control variable. Ada for-command:
for V in R loop C end loop;
R must be of some primitive type other than Float
the control sequence consists of all values in the range R, in ascending order.
3-31
Implementation notes
Each variable occupies storage space throughout its lifetime. That storage space must be allocated at the start of the variables lifetime (or before), and deallocated at the end of the variables lifetime (or later). The amount of storage space occupied by each variable depends on its type. Assume that the PL is statically typed: all variables types are declared explicitly, or the compiler can infer them.
3-32
3-33
3-34