Euphoria-switch-statement
提供:Dev Guides
Euphoria-Switchステートメント
*switch* ステートメントは、式の値に応じて、特定のステートメントセットを実行するために使用されます。 多くの場合、一連の *if…elsif* ステートメントを置き換えて、プログラムをより制御しやすく、読みやすくします。
構文
単純なswitch文の構文は次のとおりです-
switch expression do
case <val> [, <val-1>....] then
-- Executes when the expression matches one of the values
case <val> [, <val-1>....] then
-- Executes when the expression matches one of the values
.....................
case else
-- Executes when the expression does not matches any case.
end if
ケースの<val>は、アトム、リテラル文字列、定数、または列挙のいずれかでなければなりません。 単一のケースに複数の値を指定するには、値をコンマで区切ります。 デフォルトでは、次のケースが発生すると、制御はスイッチブロックの最後に流れます。
例
#!/home/euphoria-4.0b2/bin/eui
atom marks = 'C'
switch marks do
case 'A' then
puts(1, "Excellent!\n" )
case 'B', 'C' then
puts(1, "Well done!\n" )
case 'D' then
puts(1, "You passed!\n" )
case 'F' then
puts(1, "Better try again!\n" )
case else
puts(1, "Invalid grade!\n" )
end switch
これは、次の結果を生成します-
Well done!
fallthru_ステートメントを使用した_switch …
*switch* の *case* ステートメントは、指定された式の値と一致したときに実行され、デフォルトで出力されます。 デフォルトでは、次のケースが発生すると、制御はスイッチブロックの最後に流れます。
特定のスイッチブロックのデフォルトは、switch文で with fallthru を使用して新しいケースが検出されるたびに次の実行可能文に制御が渡されるように変更できます-
構文
fallthru_ステートメントを使用した単純な_switch …の構文は次のとおりです-
switch expression with fallthru do
case <val> [, <val-1>....] then
-- Executes when the expression matches one of the values
break -- optional to come out of the switch from this point.
case <val> [, <val-1>....] then
-- Executes when the expression matches one of the values
break -- Optional to come out of the switch from this point.
.....................
case else
-- Executes when the expression does not matches any case.
break -- Optional to come out of the switch from this point.
end if
例
#!/home/euphoria-4.0b2/bin/eui
atom marks = 'C'
switch marks with fallthru do
case 'A' then
puts(1, "Excellent!\n" )
case 'B', 'C' then
puts(1, "Well done!\n" )
case 'D' then
puts(1, "You passed!\n" )
case 'F' then
puts(1, "Better try again!\n" )
case else
puts(1, "Invalid grade!\n" )
end switch
これは、次の結果を生成します-
Well done!
You passed!
Better try again!
Invalid grade!
次のように、オプションの break ステートメントを使用して、switchステートメント内のポイントから出力できます。
#!/home/euphoria-4.0b2/bin/eui
atom marks = 'C'
switch marks with fallthru do
case 'A' then
puts(1, "Excellent!\n" )
break
case 'B', 'C' then
puts(1, "Well done!\n" )
break
case 'D' then
puts(1, "You passed!\n" )
break
case 'F' then
puts(1, "Better try again!\n" )
break
case else
puts(1, "Invalid grade!\n" )
break
end switch
これは、次の結果を生成します-
Well done!
_switch …. label_ステートメント
*switch* ステートメントには、switchブロックに名前を付けるためのオプションの *label* を含めることができます。 この名前をネストされたswitch breakステートメントで使用して、所有しているスイッチだけでなく、囲んでいるスイッチから抜け出すことができます。
スイッチラベルはブロックに名前を付けるためだけに使用され、ラベル名は単一または複数の単語を含む二重引用符で囲まれた定数文字列でなければなりません。 labelキーワードは大文字と小文字が区別されるため、 label と記述する必要があります。
構文
単純な_switch … label_ステートメントの構文は次のとおりです-
switch expression label "Label Name" do
case <val> [, <val-1>....] then
-- Executes when the expression matches one of the values
break "LEBEL NAME"
case <val> [, <val-1>....] then
-- Executes when the expression matches one of the values
break "LEBEL NAME"
.....................
case else
-- Executes when the expression does not matches any case.
break "LEBEL NAME"
end if
例
#!/home/euphoria-4.0b2/bin/eui
atom marks = 'C'
atom scale = 'L'
switch marks label "MARKS" do
case 'A' then
puts(1, "Excellent!\n" )
case 'B', 'C' then
puts(1, "Well done!\n" )
switch scale label "SCALE" do
case 'U' then
puts(1, "Upper scale!\n" )
break "MARKS"
case 'L' then
puts(1, "Lower scale!\n" )
break "MARKS"
case else
puts(1, "Invalid scale!\n" )
break "MARKS"
end switch
case 'D' then
puts(1, "You passed!\n" )
case 'F' then
puts(1, "Better try again!\n" )
case else
puts(1, "Invalid grade!\n" )
end switch
これは、次の結果を生成します-
Well done!
Lower scale!
注-_with fallthru_ステートメントを使用していない場合、switchステートメントが自動的に表示されるため、ラベルを使用する必要はありません。