Difference between revisions of "Basic Pascal Tutorial/Chapter 3/FOR..DO/ja"

From Free Pascal wiki
Jump to navigationJump to search
(Created page with "{{FOR..DO/ja}} == FOR...DO Loops (著者: Tao Yue, 状態: 原文のまま修正なし) == FOR...DO は Pascal におけるループを構成するためのものである...")
 
Line 14: Line 14:
  
 
=== FOR...DO Loop ===
 
=== FOR...DO Loop ===
In Pascal, the fixed repetition loop is the for loop. The general form is:
+
Pascal においては固定反復が for ループである。一般的な形式は次の通りである。
 
<syntaxhighlight>
 
<syntaxhighlight>
for index := StartingLow to EndingHigh do
+
for index := 開始Low to 終了High do
  statement;
+
   文;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
The index variable must be of an '''ordinal''' data. The index can be used in calculations within the body of the loop, but its value cannot be changed (i.e. <tt>count:=5</tt> will cause a program exception.
+
インデックス変数は '''順序型''' データでなくてはならない。このインデックスはループ本体の中で計算に用いられるが、その値は変えることはできない。たとえば、 <tt>count:=5</tt> とするとプログラムに例外を引き起こす。
  
 
In Pascal, the <tt>for</tt> loop can only count in increments (steps) of 1.
 
In Pascal, the <tt>for</tt> loop can only count in increments (steps) of 1.

Revision as of 19:39, 10 August 2015

Template:FOR..DO/ja

FOR...DO Loops (著者: Tao Yue, 状態: 原文のまま修正なし)

FOR...DO は Pascal におけるループを構成するためのものである。もちろん、まず "ループとは何か?"という疑問が湧くだろう。

Loops

ループするとは文や複合文が、ある条件が満たされるまで繰り返し繰り返し実行されることを意味する。

3つのタイプのループがある。

  • 固定反復(fixed repetition) - 固定された回数のみ反復する
  • プリテスト(pretest) - ブール式をテストし、TRUE ならループに入る
  • ポストテスト(posttest) - ループを実行し、それからブール式をテストする

FOR...DO Loop

Pascal においては固定反復が for ループである。一般的な形式は次の通りである。

for index := 開始Low to 終了High do
    文;

インデックス変数は 順序型 データでなくてはならない。このインデックスはループ本体の中で計算に用いられるが、その値は変えることはできない。たとえば、 count:=5 とするとプログラムに例外を引き起こす。

In Pascal, the for loop can only count in increments (steps) of 1. A loop can be interrupted using the break statement. An example of using the index is:

sum := 0;
for count := 1 to 100 do
begin
  sum := sum + count;
  if sum = 38 then break;
end;

The computer would do the sum the long way and still finish it in far less time than it took the mathematician Gauss to do the sum the short way (1+100 = 101. 2+99 = 101. See a pattern? There are 100 numbers, so the pattern repeats 50 times. 101*50 = 5050. This isn't advanced mathematics, its attribution to Gauss is probably apocryphal.).

In the for-to-do loop, the starting value MUST be lower than the ending value, or the loop will never execute! If you want to count down, you should use the for-downto-do loop:

for index := StartingHigh downto EndingLow do
  statement;

See also

While ...Do loops/ja

Repeat... Until loops/ja

For... in loops/ja


previous contents next