Difference between revisions of "To"

From Free Pascal wiki
Jump to navigationJump to search
Line 3: Line 3:
 
[[Keyword]] used to indicate the final value of the control value in a [[For]] loop, and that the loop is to increment the control variable by 1 on each loop.  The value specified by TO should be greater than the initial value of the for loop.
 
[[Keyword]] used to indicate the final value of the control value in a [[For]] loop, and that the loop is to increment the control variable by 1 on each loop.  The value specified by TO should be greater than the initial value of the for loop.
  
== [[For|for]] to [[Do|do]] ==
+
== [[For]] to [[Do|do]] ==
 
<syntaxhighlight>
 
<syntaxhighlight>
var I:integer;
+
var i : integer;
 
begin
 
begin
  For I :=1 to 10000 do
+
  for i := 1 to 10000 do
    //...
+
  begin
End.
+
    //...
 +
  end;
 +
end;
 +
 
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
The for to allow to execute code repeatedly for a fixed number of times.
 +
 +
=== Basic example ===
 +
 +
<syntaxhighlight>
 +
var
 +
  loopValue, startValue, endValue, resultValue: integer;
 +
begin
 +
  startValue := 10;
 +
  endValue := 11;
 +
  resultValue := 0;
 +
  for loopValue := startValue to endValue do
 +
    begin
 +
      resultValue := loopValue + resultValue;
 +
    end;
 +
end;
 +
 +
</syntaxhighlight>
 +
 +
=== Start and end value same ===
 +
 +
The loop execute two times and variable resultValue value is 21.
 +
 +
<syntaxhighlight>
 +
 +
var
 +
  loopValue, startValue, endValue, resultValue: integer;
 +
begin
 +
  startValue := 10;
 +
  endValue := 10;
 +
  resultValue := 0;
 +
  for loopValue := startValue to endValue do
 +
    begin
 +
      resultValue := loopValue + resultValue;
 +
    end;
 +
 +
end;
 +
 +
</syntaxhighlight>
 +
 +
The loop execute one time and variable resultValue value is 10.
 +
 +
=== Start value bigger than end value ===
 +
<syntaxhighlight>
 +
var
 +
  loopValue, startNumber, endNumber, resultValue: integer;
 +
begin
 +
  startValue := 10;
 +
  endValue := 10;
 +
  resultValue := 0;
 +
  for loopValue := startValue to endValue do
 +
    begin
 +
      resultValue := loopValue + resultValue;
 +
    end;
 +
 +
end;
 +
 +
</syntaxhighlight>
 +
 +
The loop execute none time and variable resultValue value is 0.
 +
 +
== Read more ==
 +
 +
* [[Downto]]

Revision as of 09:07, 18 October 2015

Deutsch (de) English (en) français (fr) русский (ru)

Keyword used to indicate the final value of the control value in a For loop, and that the loop is to increment the control variable by 1 on each loop. The value specified by TO should be greater than the initial value of the for loop.

For to do

var i : integer;
begin
 for i := 1 to 10000 do
   begin
     //...
   end;
end;

The for to allow to execute code repeatedly for a fixed number of times.

Basic example

var
  loopValue, startValue, endValue, resultValue: integer;
begin
  startValue := 10;
  endValue := 11;
  resultValue := 0;
  for loopValue := startValue to endValue do
    begin
      resultValue := loopValue + resultValue;
    end;
end;

Start and end value same

The loop execute two times and variable resultValue value is 21.

var
  loopValue, startValue, endValue, resultValue: integer;
begin
  startValue := 10;
  endValue := 10;
  resultValue := 0;
  for loopValue := startValue to endValue do
    begin
      resultValue := loopValue + resultValue;
    end;

end;

The loop execute one time and variable resultValue value is 10.

Start value bigger than end value

var
  loopValue, startNumber, endNumber, resultValue: integer;
begin
  startValue := 10;
  endValue := 10;
  resultValue := 0;
  for loopValue := startValue to endValue do
    begin
      resultValue := loopValue + resultValue;
    end;

end;

The loop execute none time and variable resultValue value is 0.

Read more