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.
loops can be expressions, with neg and pos values. eval at beginning of DO to arrive at an iteration count. It is this value, not the loop parameters, that control the loop. so one could change them.
no multiple entry, yes multiple exits.
ALGOL 60: a case in flexibility
[for_stmt] -) for var := [list_element] {, [list_element]} do [statement]
[list_element] -) [expression]
| [expression] step [expression] until [expression]
| [expression] while [Boolean_expr]
See this website: http://logos.cs.uic.edu/476/notes/overheads/ch7controlstructures.pdf
Pascal For Statement
for var = init (to | downto) final do statement
it grows or shrinks in steps of 1. loop var cannot be changed in the body.
after loop, loop var undefined. loop var is an ordinal, with scope of the loop.
init and final checked at the start, and not after, and so may be changed in the loop.
why do this? efficiency, reliability?
Ada For statement
for variable in [reverse] discrete_range loop
...
end loop
and note that loop var has loop scope, will mask variables with wider scope.
C, C++:
We know this.
for (int i = 9; i < style="font-weight: bold;">
any of the elements in the for may be missing.
scope until end of function.
Logically Controlled Loops (rather than counting loops)
posttest, pretest?
should it be special form of a counting loop?
examples of pretest, posttest in C++, Java.
do while vs. while.
how would we implement them (=operational semantics)
FORTRAN 90: has neither.
Ada: pretest, no posttest. why?
Perl: while and until.
how so? see here: http://vergil.chemistry.gatech.edu/resources/programming/perl-tutorial/control.html while ($food < $stomach_ache) { $plop++; $fizz--; $oh_what_a_relief_it_is *= 5; }
unless
statement above, it is easier to say "until something is true" (Schwartz 61). Thus, Perl provides the until
statement: until ($x > 6) {
print "$x is the number!\n";
$x++;
}do, while
loops, but Perl also adds do, until
loops: # do while example
do {
$junk++;
} while ($junk < aok =" @array_hooray[$x];">
and of course the for loop.
might as well talk about Perl conditionals here as well:
Pascal has a repeat...until statement, which is the reverse of C's do...while, same as Perl's do...until.if
and else
statements are used as conditionals as in any other language. For example: if ($cow < $sheep) { print "baaah....\n"; } else { print "moo....\n"; }
unless
statement is specific to Perl, in which you want to always just evaluate the false part instead of doing something when not true. For example: unless ($age <>
Think about it - when we have break statements, they are the reverse of a standard while conditional. repeat...until maintains that.
Post-tests not freq used. dangerous because loop always executed at least once, and programmer might forget this. put after loop body helps make this a bit clearer.
to be continued...
Tuesday, March 28, 2006
Iterative Statements
Posted by joshwaxman at 9:54 AM
Subscribe to:
Post Comments (Atom)
3 comments:
useful blog. i got answers for my assignment through here. tanx a lot. ;)
Post a Comment