Difference between revisions of "To"

From Free Pascal wiki
Jump to navigationJump to search
m (Fixed syntax highlighting)
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
 
{{to}}
 
{{to}}
 +
 +
 +
Back to [[Reserved words]].
 +
  
 
[[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]] to [[Do|do]] ==
 
== [[For]] to [[Do|do]] ==
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
var i : integer;
 
var i : integer;
 
begin
 
begin
Line 12: Line 17:
 
   end;
 
   end;
 
end;
 
end;
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 19: Line 23:
 
=== Basic example ===
 
=== Basic example ===
  
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
var
 
var
 
   loopValue, startValue, endValue, resultValue: integer;
 
   loopValue, startValue, endValue, resultValue: integer;
Line 31: Line 35:
 
     end;
 
     end;
 
end;
 
end;
 
 
</syntaxhighlight>
 
</syntaxhighlight>
 
=== Start and end value same ===
 
  
 
The loop execute two times and variable resultValue value is 21.
 
The loop execute two times and variable resultValue value is 21.
  
<syntaxhighlight>
+
=== Start and end value same ===
  
 +
<syntaxhighlight lang=pascal>
 
var
 
var
 
   loopValue, startValue, endValue, resultValue: integer;
 
   loopValue, startValue, endValue, resultValue: integer;
Line 50: Line 52:
 
       resultValue := loopValue + resultValue;
 
       resultValue := loopValue + resultValue;
 
     end;
 
     end;
 
 
end;
 
end;
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 58: Line 58:
  
 
=== Start value bigger than end value ===
 
=== Start value bigger than end value ===
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
var
 
var
 
   loopValue, startNumber, endNumber, resultValue: integer;
 
   loopValue, startNumber, endNumber, resultValue: integer;
 
begin
 
begin
 
   startValue := 10;
 
   startValue := 10;
   endValue := 10;
+
   endValue := 9;
 
   resultValue := 0;
 
   resultValue := 0;
 
   for loopValue := startValue to endValue do
 
   for loopValue := startValue to endValue do
Line 69: Line 70:
 
       resultValue := loopValue + resultValue;
 
       resultValue := loopValue + resultValue;
 
     end;
 
     end;
 
 
end;
 
end;
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Latest revision as of 11:54, 1 March 2020

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


Back to Reserved words.


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;

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

Start and end value same

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 := 9;
  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