To/ru
Deutsch · English · Français · Русский
Ключевое слово To используется для указания того, что значение переменной-счетчика в цикле For увеличивается на 1 на каждом шаге цикла. Значение переменной-счетчика, указанное после слова to, должно быть больше, чем начальное значение в инструкции цикла For.
Цикл For to do
var i : integer;
begin
for i := 1 to 10000 do
begin
// инструкции цикла
end;
end;Цикл for выполняет инструкции кода фиксированное число раз.
Основной пример
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;Цикл выполнится два раза и значение переменной resultValue станет равным 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;Цикл выполнится один раз и значение переменной resultValue станет равным 10.
Начальное значение больше конечного значения
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;Цикл не выполнится ни разу и значение переменной resultValue останется равным 0.
См. также
From the Free Pascal wiki, page "To/ru", as it stood on 2020-03-01 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.