Difference between revisions of "Basic Pascal Tutorial/Chapter 3/FOR..DO/ja"
(corrected template name) |
|||
Line 22: | Line 22: | ||
インデックス変数は '''順序型''' データでなくてはならない。このインデックスはループ本体の中で計算に用いられるが、その値は変えることはできない。たとえば、 <tt>count:=5</tt> とするとプログラムに例外を引き起こす。 | インデックス変数は '''順序型''' データでなくてはならない。このインデックスはループ本体の中で計算に用いられるが、その値は変えることはできない。たとえば、 <tt>count:=5</tt> とするとプログラムに例外を引き起こす。 | ||
− | + | Pascal では、 <tt>for</tt> ループは1の増加のみがカウントできる。 | |
− | + | ループは ''break'' 文で中断できる。 | |
− | + | インデックスを利用した例を次に示す。 | |
<syntaxhighlight> | <syntaxhighlight> | ||
sum := 0; | sum := 0; | ||
Line 34: | Line 34: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | コンピュータは、終了になるまで延々合計(<tt>1+100 = 101. 2+99 = 101</tt>) を求め続ける。しかし、それは数学者ガウスが簡単な近道で合計を求めたやり方、 (<tt>1+100 = 101. 2+99 = 101</tt>. See a pattern? There are 100 numbers, so the pattern repeats 50 times. <tt>101*50 = 5050</tt>. This isn't advanced mathematics, its attribution to Gauss is probably apocryphal.). | |
In the <tt>for-to-do</tt> 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 <tt>for-downto-do</tt> loop: | In the <tt>for-to-do</tt> 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 <tt>for-downto-do</tt> loop: |
Revision as of 09:38, 11 August 2015
│
български (bg) │
English (en) │
français (fr) │
日本語 (ja) │
中文(中国大陆) (zh_CN) │
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 とするとプログラムに例外を引き起こす。
Pascal では、 for ループは1の増加のみがカウントできる。 ループは break 文で中断できる。 インデックスを利用した例を次に示す。
sum := 0;
for count := 1 to 100 do
begin
sum := sum + count;
if sum = 38 then break;
end;
コンピュータは、終了になるまで延々合計(1+100 = 101. 2+99 = 101) を求め続ける。しかし、それは数学者ガウスが簡単な近道で合計を求めたやり方、 (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
previous | contents | next |