Sunday, June 12, 2011

3 Control Program

3.1 Control Program
Control Program used to controlling program line by line. Normally program runed sequential form first line to end line . but, with control program we can set the line will runned or not, the program will continue or repeated from particular line etc.
3.1.1 While...Do loop
While...Do The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false.
The syntax of a while statement :
while<expression>do<statements>;
examples
var i :integer;
begin
memo1.Clear;
i:=1;
whilei<=10 do
begin
memo1.Lines.Add(inttostr(i));
i:=i+1;
end;
end;

normally, the looping will stop when <expression> value False. but we can use Break to force the program exit from looping. and proceed to the line below the loop.
examples :

//looping with one statements
while Data[I] <> x do I := I + 1;

//looping with one block statements
while not Eof(InputFile) do
begin
Readln(InputFile), line);
Process(Line);
end;
//looping with one block statements
While I > 0 do
begin
if A > B then
begin
C := A + B;
D := B / A;
end
else
break
end;
I := I div 2;
end;

No comments:

Post a Comment