|
Delphi |
|
for loop is used when you know the number of iterations. Loop variable must be local declared. It can be of any ordinal type:
var
i: Integer;
c: Char;
...
for i:=1 to 10 do // some code
for c:='A' to 'Z' do // some code
If initial value less than final value downto must be used instead of to:
for i:=100 downto 1 do // some code
In earlier versions of Delphi you can modify the loop variable within the loop code, but it is not recommended due to code optimization in the last versions of Delphi:
for i:=1 to 10 do
begin
if i=ValueWeWantToIgnore-1 then Inc(i); // < don't use this way!
// some code
end;
|
|
C# |
|
The for loop executes a statement or a block of statements repeatedly until a specified expression evaluates to false.
The for loop is handy for iterating over arrays and for sequential processing.
for( int i = 1; i < 100; i++ )
{
// do something
}
You can use any start and end value and any counter operation:
for( int i = 100; i > -100; i-=5 )
{
// do something
}
|
|
|
|
|
|
Simple and flexible syntax - no more strange downto. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|