Sunday, June 12, 2011

3.3 For..Next Loop

3.3 For..Next Loop
to perform repetitions (iterations) a statement or a block of the program several times which is determined by the value of initial and final value.
Syntax :
1
for<enumerators> := <thefirst> to <thelast> do <statements>;

or,2

for<enumerators> := <thefirst> downto <thelast> do <statements>;

<enumerators> is the variable (type ordinal), that is the variables that have a definite value if substraction or addition with one unit. example : byte, shortInt, subrange etc. <senumerators> value can changed in <statements>
if we use the first form, then looping for .. next will repeat <statement> and will stop if the <thefirst>had passed <thelast>. if you use the second form, then the looping will stop if <thefirst> value is below <thelast> value. break statement can also be used here.
examples :
//ex 1
for i := 2 to 10 do
if data[i]> max then max := data[i];
//ex 2
for i := listbox1.items.count -1 downto 0 do
listbox1.items[i] := listbox1.items[i];
//ex 3
for i := 1 to 10 do
for j := 1 to 10 do
begin
x := 0;
for k := 1 to 10 do
x :=

No comments:

Post a Comment