الحلقات التكرارية في دلفي - viv - 17-06-19
Continue
كود :
var
i : Integer;
s : string;
begin
s := '';
// A big loop
for i := 1 to 9 do
begin
// Skip loop processing for certain values of i
if (i = 3) or (i = 7) then Continue;
s := s + IntToStr(i);
s := s + ' ';
end;
// Show the string created by the above loop
ShowMessage('s = '+s);
end;
إقتباس :s = 1 2 4 5 6 8 9
Do
كود :
var
i : Integer;
begin
// A for statement - the do keyword precedes a single statement
for i := 1 to 3 Do
ShowMessage('For loop, i = '+IntToStr(i));
// A while statement - the do precedes a statement block
while i < 6 Do
begin
ShowMessage('While loop, i = '+IntToStr(i));
Inc(i);
end;
end;
إقتباس :For loop, i = 1
For loop, i = 2
For loop, i = 3
While loop, i = 4
While loop, i = 5
DownTo
كود :
var
i : Integer;
begin
// Loop 5 times
for i := (10 div 2) DownTo 1 do
ShowMessage('i = '+IntToStr(i));
end;
إقتباس : i = 5
i = 4
i = 3
i = 2
i = 1
كود :
var
c : char;
begin
// Loop 5 times
for c := 'E' DownTo 'A' do
ShowMessage('c = '+c);
end;
إقتباس :c = E
c = D
c = C
c = B
c = A
كود :
var
suit : (Hearts, Clubs, Diamonds, Spades);
begin
// Loop 3 times
for suit := Diamonds DownTo Hearts do
ShowMessage('Suit = '+IntToStr(Ord(suit)));
end;
إقتباس :Suit = 2
Suit = 1
Suit = 0
For
كود :
var
i : Integer;
begin
// Loop 5 times
For i := 1 to (10 div 2) do
ShowMessage('i = '+IntToStr(i));
end;
إقتباس :i = 1
i = 2
i = 3
i = 4
i = 5
كود :
var
c : char;
begin
// Loop 5 times - downwards
For c := 'E' downto 'A' do
ShowMessage('c = '+c);
end;
إقتباس : c = E
c = D
c = C
c = B
c = A
كود :
var
suit : (Hearts, Clubs, Diamonds, Spades);
begin
// Loop 3 times
For suit := Hearts to Diamonds do
ShowMessage('Suit = '+IntToStr(Ord(suit)));
end;
إقتباس :Suit = 0
Suit = 1
Suit = 2
Repeat
كود :
var
num, sqrNum : Integer;
begin
num := 1;
sqrNum := num * num;
// Display squares of integers until we reach 100 in value
Repeat
// Show the square of num
ShowMessage(IntToStr(num)+' squared = '+IntToStr(sqrNum));
// Increment the number
Inc(num);
// Square the number
sqrNum := num * num;
until sqrNum > 100;
end;
إقتباس :1 squared = 1
2 squared = 4
3 squared = 9
4 squared = 16
5 squared = 25
6 squared = 36
7 squared = 49
8 squared = 64
9 squared = 81
10 squared = 100
Until
كود :
var
num, sqrNum : Integer;
begin
num := 1;
sqrNum := num * num;
// Display squares of integers until we reach 100 in value
Repeat
// Show the square of num
ShowMessage(IntToStr(num)+' squared = '+IntToStr(sqrNum));
// Increment the number
Inc(num);
// Square the number
sqrNum := num * num;
Until sqrNum > 100;
end;
إقتباس :1 squared = 1
2 squared = 4
3 squared = 9
4 squared = 16
5 squared = 25
6 squared = 36
7 squared = 49
8 squared = 64
9 squared = 81
10 squared = 100
While
كود :
var
num, sqrNum : Integer;
begin
num := 1;
sqrNum := num * num;
// Display squares of integers until we reach 100 in value
While sqrNum <= 100 do
begin
// Show the square of num
ShowMessage(IntToStr(num)+' squared = '+IntToStr(sqrNum));
// Increment the number
Inc(num);
// Square the number
sqrNum := num * num;
end;
end;
إقتباس :1 squared = 1
2 squared = 4
3 squared = 9
4 squared = 16
5 squared = 25
6 squared = 36
7 squared = 49
8 squared = 64
9 squared = 81
10 squared = 100
|