{ CS 1621 Fall 2005 Demo of for loop behavior in Pascal } program fortest; var i, low, high: integer; function wacky(var i: integer):integer; begin i := i - 2; writeln('Wacky i is ', i:1); wacky := i; end; function wackier(var i: integer):integer; begin i := i + 2; writeln('Wackier i is ', i:1); wackier := i; end; begin low := 1; high := 10; for i := low to high do begin writeln('i is now ', i:1, ' and high is ', high:1); { i := i - 1; } { this causes a syntax error in standard pascal } { it compiles and causes infinite loop in turbo } high := high + 1; { this does not affect the iterations } end; writeln; writeln('i is now ', i:1); writeln; { Now let's do some wacky stuff! Let's see how side-effects affect the loop. Note that the upper bound is calculated before the lower bound (I don't know if that is specified in the language or not) and that the functions are called only one time -- when the loop is first entered } for i := wacky(i) to wackier(i) do begin writeln('i is now ', i:1); end; end.