入力 X:
if (0 <= X and X < 49)
output "abc"
else if (50 <= X and X < 70)
output "def"
else if (70 <= X and X < 85)
output "ghi"
else if (85 <= X and X < 100)
output "jkl"
endif
入力 X:
if (0 <= X and X < 49)
output "abc"
else if (50 <= X and X < 70)
output "def"
else if (70 <= X and X < 85)
output "ghi"
else if (85 <= X and X < 100)
output "jkl"
endif
これは、次のものと論理的に同等であると考えることができます。
if(a) {
// code
} else {
if(b) {
// code
} else {
// code
}
}
したがって、この点では、ネストされていると言えます。C および同様の言語では、利用可能な「elseif」ステートメントがないため、これがまさにそのように機能します。中括弧はオプションですが、わかりやすくするために含めました。
それらはネストされていますが、ネストされていないようにフォーマットされています。
あなたのコードは次と同じです:
if (0 <= X and X < 49)
output "abc"
else
if (50 <= X and X < 70)
output "def"
else
if (70 <= X and X < 85)
output "ghi"
else
if (85 <= X and X < 100)
output "jkl"
endif
endif
endif
endif
これはネストされていません:
if (0 <= X and X < 49)
output "abc"
endif
if (50 <= X and X < 70)
output "def"
endif
if (70 <= X and X < 85)
output "ghi"
endif
if (85 <= X and X < 100)
output "jkl"
endif
これは、if ステートメントを持つすべての (?) 言語で有効です (endif の代わりに {} を使用するようなものは無視します)。
ただし、一部の言語には実際の "elseif" (または "elif") コマンドがあり、その場合はネストしませんが、"else if" と記述すると、単に異なる形式のネストであると見なすことができます。
それは実際の言語と、それがどのように書かれているかによって異なります。
たとえば、VB を使用すると、これらのIf
ステートメントはネストされません。
If 0 <= x And x < 49 Then
output("abc")
ElseIf 50 <= x And x < 70 Then
output("def")
ElseIf 70 <= x And x < 85 Then
output("ghi")
ElseIf 85 <= x And x < 100 Then
output("jkl")
End If
これらのIf
ステートメントはネストされていますが、次のようになります。
If 0 <= x And x < 49 Then
output("abc")
Else
If 50 <= x And x < 70 Then
output("def")
Else
If 70 <= x And x < 85 Then
output("ghi")
Else
If 85 <= x And x < 100 Then
output("jkl")
End If
End If
End If
End If
はい、ネストされていると思います。あなたのコードは正確に同等です
if (0 <= X and X < 49)
output "abc"
else
if (50 <= X and X < 70)
output "def"
else
if (70 <= X and X < 85)
output "ghi"
else
if (85 <= X and X < 100)
output "jkl"
endif
空白のみを変更したことに注意してください。言語が句を評価するときif...else if...else
、真の句が見つかるまで (または final に到達するまでelse
)、各句をテストします。ネストif
された はまったく同じ方法で評価されます。elsif
また、明示的なキーワードがある場合は、必ずしもそうではないことに注意してください。
私が気付いたもう1つのことは、以下はあなたのコードと同等ではありません:
if (0 <= X and X < 49)
output "abc"
if (50 <= X and X < 70)
output "def"
if (70 <= X and X < 85)
output "ghi"
if (85 <= X and X < 100)
output "jkl"
すべての条件が真の場合に、すべてのテキストが出力されないようにするために、ネストが必要です。
Given the syntax shown, I think the answer should be "No", contrary to the accumulated wisdom of the other answers.
You show:
if (0 <= X and X < 49)
output "abc"
else if (50 <= X and X < 70)
output "def"
else if (70 <= X and X < 85)
output "ghi"
else if (85 <= X and X < 100)
output "jkl"
endif
This is clearly a single if
... endif
statement, and therefore there is no nesting.
If there were multiple endif
statements, it would be nested:
if (0 <= X and X < 49)
output "abc"
else
if (50 <= X and X < 70)
output "def"
else
if (70 <= X and X < 85)
output "ghi"
else
if (85 <= X and X < 100)
output "jkl"
endif
endif
endif
endif
So, in your language, it appears that the keyword elif
(used by the Bourne shell) and elsif
(used by Perl) and ElseIf
(used by Visual Basic) is spelled else if
.
If there was no explicit endif
to mark the end of the statement, then I would be in agreement with the other answers - that the if
statements (plural!) are nested, though the layout is perfectly sensible and recommended.
これはやや無意味なセマンティクスのゲームです。関連する言語の構文によって異なります。
たとえば、C に似た構文では、通常、else 句にネストされていると見なされ、その事実をわかりにくくするために中括弧が省略されます。この場合、Turnor の例は正しい。
Python や VB などの他の言語では、「else-if」は独自のアトミック コンストラクトです。その場合、「if」は「else」の中にあるとは見なされないため、「入れ子」とは言えません。
if (0 <= X and X < 49)
output "abc"
else if (50 <= X and X < 70)
output "def"
endif
確かに言うのに十分なほど擬似コードの構文を定義していませんが、末尾の「endif」は疑わしいです。その存在は、C ブレースを省略したスタイルには適合しません。そして、そのうちの 1 つしかなく、それ以上ではないという事実 —</p>
else
if (50 <= X and X < 70)
output "def"
endif
endif
— 中括弧付き (または開始/終了) モデルとも一致しないことを意味します。したがって、その構文から判断すると、私はあなたの疑似コード言語を「アトミックelse-if」キャンプに入れ、まったく恣意的に言います:いいえ、あなたのifステートメントはネストされていません。
(ただし、endif がオプションまたは空白に依存する言語を常に定義することができます。または、上記のプログラムが「Hello world」を出力し、すべてのファイルを削除する言語を定義した可能性があります。組み込みのメールリーダーがあります。 )
言語によっては、ネストされたループとして実装される場合があります。しかし、あなたが論理的に書いた方法では、それは 1 つとは見なされません。
いいえそうではありません。
ネストされたステートメントは、If ... If のように、同じステートメント内に現れるステートメントです。If ... ElseIf はすべて同じ「ステートメント」内にあります。
編集時: 気にしないでください。これについては私が間違っていたことに同意します。
noif
は、別のテスト済みの内部if
でテストされているため、そうではありませんtrue
。
特に、あなたの例:
if (0 <= X and X < 49) output "abc"
else if (50 <= X and X < 70) output "def"
else if (70 <= X and X < 85) output "ghi"
else if (85 <= X and X < 100) output "jkl"
endif
次のように書き換えることができます。
if (0 <= X and X < 49) output "abc"; return; end if
if (50 <= X and X < 70) output "def"; return; end if
if (70 <= X and X < 85) output "ghi"; return; end if
if (85 <= X and X < 100) output "jkl"; return; end if
// X < 0 or X >= 100
コメント:
if ステートメントは、ネストするために別の if 内にネストする必要はありません。else -Bill the Lizard 内にネストできます
要点; 私はこれについて間違っていることに同意します。