Control Statement = control statement + statement(s) it controls
control execution path
without, all have are assignments and evaluating expressions.
two types: selection (among multiple execution paths) and repeated execution.
basically, if and while. but can add more = greater writability, but programmers only learn subset of lang.
how many statements do you allow in the control statement?
one or many. blocks. otherwise, restricted to single statement, or use of a goto. control structures can replace goto.
another q: can we enter into the middle of control structures (multiple entries) - langs differ. in terms of whether can exit multiply, not such a distinction since generally safe to do that - but there is a design philosophy.
Selection
generally 2 way vs. n way selectors (1 way "degenerate" form of the two way. fortran's 3 way selector).
design issue: what type of expression controls the selector? can we select more than a single statement? do we handle nested selection?
Two way: Logical If:
FORTRAN IV
IF (BOOL expression) statement
simple, inflexible. how do you get multiple statments? with goto circumventing.
IF (FLAG .NE. 1) GO TO 20
I = 1
J = 1
20 CONTINUE
but points off for lack of readability.
How about multiple entry? Yes. How?
ALGOL 60 and on (=Java, FORTRAN 90): two-way selector:
if (bool expr) then
statement
else
statement
only one or the other is executed.
and either of these can be compound, in which case it is the then clause and else clause, rather than statement.
how do we generate the degenerate 1-way if? how do we generate the two-way?
Nesting Selectors
Ambiguity.
if (a == 1)
if (b == 2)
result = 5;
else
result = 1;
Python: semantic interpretation based on indentation. other languages, no.
In Java, most other imperative langs - match most recent, so must indent the else.
by a rule, rather than in the syntax. but will the programmer know?
ALGOL 60 - by Syntax rather than by fiat (rule).
no if nested in another if. must nest in a begin/end clause.
How would we then write the code to get each interp? (Use {} as a shorthand.) Can get either effect in Java, but to match first if rather than second, must use {}.
Perl: then and else clauses must always be compound.
in C++, Java, the introduction of the else clause, or else by compounds, {}, marked the end of the statement or compound, and where this was a single statement with no immediate else, was ambiguity. And difference between marker of end of clause, if it is a compound of single statement.
In Ada, VB6, have end if;
how does this help?
Because "end if" closes the nested if, shows which one it matches. how do we write each in Ada?
Multiple Selection
generalization of 2-way, and can implement 2-way as multiple.
design issues
do we allow compound statements, type of expression (2 way selector needed only 2 vals), can we select more than one statement/compound? what if does not eval to one of specified vals?
FORTRAN I - the three-way selector: IF(X) 10, 20, 30
and here can go really anywhere - need not even immediately follow if, or each other. user responsible for gotos, and can omit with compiler not detecting.
Also in FORTRAN I
computed:
GO TO (label1, label2, labeln) expression
assigned:
GO TO var_or_const
C.A.R. Hoare, in ALGOL-W (1966)
case int_expression of
begin
statement1;
...
statementn;
end
and could be compounds.
Pascal similar, except
statement_i becomes:
constant_list_i: statement_i;
yet we cannot say goto caselabel in Pascal.
later Pascals: an else clause, = to default
no fallthru.
in C#: switch(n)
and they will not allow you to omit a break of a goto.
{
case 1:
cost += 25;
break;
case 2:
cost += 25;
goto case 1;
case 3:
cost += 50;
goto case 1;
default:
Console.WriteLine("Invalid selection. Please select 1, 2, or 3.");
break;
}
does not disallow multiple entry. and has fallthru. unless use break, which basically is a restricted goto.
but what if forget the break and get unwanted fallthru?
reliability vs. flexibility.
In Ada, allow ranges: 10..15, as well as 5 | 6 | 19, and have an others clause
this is good for basing on single value at the top. but sometimes, based on various boolean conditions. must use nested ifs. poor readability. some langs (FORTRAN 90, Ada, Perl) give way around this.
elsif vs a bunch of nestings and endifs
Iterative statements
execute statment 0, 1, more times
all prog langs.
sometimes by iterative constructs, sometimes by recursion.
at first, directly related to arrays. why? that was the problem domain.
design questions: how iteration controlled, where in the loop is the control mechanism?
body, pretest, posttest.
counter-controlled loops: inital, terminal values of loop var, + step size.
logically controlled loops more general, but not necessarily more commonly used. could be more complex to implement that counter-controlled.
often supported by machine architecture. sometimes lang features outlive machine architecture (Fortran's 3 way if)
more design qs:
type, scope of loop var. what value does it have at termination, can we change loop vars in the loop (what are effects of this?), are loop params eval once, or for every iteration (let us say the terminal condition changes)
FORTRAN I: do was posttest.
DO label var = init, terminal, stepsize
stepsize defaults to 1
unsigned int constants or simple positive int variables
FORTRAN 90: DO statement is pretest. fortran 77: allows loop var to be int, real, double
Thursday, March 23, 2006
Control Structures
Posted by joshwaxman at 11:43 AM
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment