Difference between revisions of "Basic Pascal Tutorial/Chapter 3/IF/ja"
Line 3: | Line 3: | ||
3Ca - IF (著者: Tao Yue, 状態: 原文のまま修正なし) | 3Ca - IF (著者: Tao Yue, 状態: 原文のまま修正なし) | ||
− | <tt>IF</tt> | + | <tt>IF</tt> 文はブール式の結果をもとに分岐を可能にする。 1方向の分岐の形式は次のようになる。 |
<syntaxhighlight> | <syntaxhighlight> | ||
− | if | + | if ブール式 then |
真の場合の命令文; | 真の場合の命令文; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | もし、ブール式が <tt>true</tt> と評価されたなら、命令文が実行される。そうでない場合には、スキップされる。 | |
The <tt>IF</tt> statement accepts only one statement. If you would like to branch to a compound statement, you must use a <tt>begin-end</tt> [[block]] to enclose the statements: | The <tt>IF</tt> statement accepts only one statement. If you would like to branch to a compound statement, you must use a <tt>begin-end</tt> [[block]] to enclose the statements: |
Revision as of 10:13, 10 August 2015
│
български (bg) │
English (en) │
français (fr) │
日本語 (ja) │
中文(中国大陆) (zh_CN) │
3Ca - IF (著者: Tao Yue, 状態: 原文のまま修正なし)
IF 文はブール式の結果をもとに分岐を可能にする。 1方向の分岐の形式は次のようになる。
if ブール式 then
真の場合の命令文;
もし、ブール式が true と評価されたなら、命令文が実行される。そうでない場合には、スキップされる。
The IF statement accepts only one statement. If you would like to branch to a compound statement, you must use a begin-end block to enclose the statements:
if BooleanExpression then
begin
Statement1;
Statement2
end;
There is also a two-way selection:
if BooleanExpression then
StatementIfTrue
else
StatementIfFalse;
If the Boolean expression evaluates to FALSE, the statement following the else will be performed. Note that you may not use a semicolon after the statement preceding the else. That causes the computer to treat it as a one-way selection, leaving it to wonder where the else came from.
If you need multi-way selection, simply nest if statements:
if Condition1 then
Statement1
else
if Condition2 then
Statement2
else
Statement3;
Be careful with nesting. Sometimes the computer won't do what you want it to do:
if Condition1 then
if Condition2 then
Statement2
else
Statement1;
The else is always matched with the most recent if, so the computer interprets the preceding block of code as:
if Condition1 then
if Condition2 then
Statement2
else
Statement1;
You can get by with a null statement:
if Condition1 then
if Condition2 then
Statement2
else
else
Statement1;
Or you could use a begin-end block. But the best way to clean up the code would be to rewrite the condition.
if not Condition1 then
Statement1
else
if Condition2 then
Statement2;
This example illustrates where the not operator comes in very handy. If Condition1 had been a Boolean like: (not(a < b) or (c + 3 > 6)) and g, reversing the expression would be more difficult than NOTting it.
Also notice how important indentation is to convey the logic of program code to a human, but the compiler ignores the indentation.
previous | contents | next |