Euphoria-if-statement

提供:Dev Guides
移動先:案内検索

if …​ elsif …​ else …​ endifステートメント

_if_ステートメント

*if* ステートメントは、ブール式とそれに続く1つ以上のステートメントで構成されます。

構文

_if_ステートメントの構文は-

if expression then
   -- Statements will execute if the expression is true
end if

ブール式の評価がtrueの場合、ifステートメント内のコードブロックが実行されます。 falseと評価された場合、ifステートメントの終了後の最初のコードセットが実行されます。

#!/home/euphoria-4.0b2/bin/eui

integer a = 10
integer b = 20

if (a + b) < 40 then
   printf(1, "%s\n", {"This is true if statement!"})
end if

if (a + b) > 40 then
   printf(1, "%s\n", {"This is not true if statement!"})
end if

これは、次の結果を生成します-

This is true if statement!

_if …​ else_ステートメント

*if* ステートメントの後にオプションの *else* ステートメントを続けることができます。これはブール式がfalseの場合に実行されます。

構文

_if …​ else_ステートメントの構文は次のとおりです-

if expression then
   -- Statements will execute if the expression is true
else
   -- Statements will execute if the expression is false
end if

#!/home/euphoria-4.0b2/bin/eui

integer a = 10
integer b = 20

if (a + b) < 40 then
   printf(1, "%s\n", {"This is inside if statement!"})
else
   printf(1, "%s\n", {"This is inside else statement!"})
end if

これは、次の結果を生成します-

This is inside if statement!

_if …​ elsif …​ else_ステートメント

*if* ステートメントの後には、任意の数のオプションの *elsif ... else* ステートメントを続けることができます。これは、単一のif ... elsifステートメントを使用してさまざまな条件をテストするのに非常に便利です。

構文

_if …​ elsif …​ else_ステートメントの構文は次のとおりです-

if expression1 then
   -- Executes when the Boolean expression 1 is true
elsif expression2 then
   -- Executes when the Boolean expression 2 is true
elsif expression3 then
   -- Executes when the Boolean expression 3 is true
else
   -- Executes when none of the above condition is true.
end if

#!/home/euphoria-4.0b2/bin/eui

integer a = 10
integer b = 20

if (a + b) = 40 then
   printf(1, "Value of (a + b ) is  %d\n", a + b )
elsif (a + b) = 45 then
    printf(1, "Value of (a + b ) is  %d\n", a + b )
elsif (a + b) = 30 then
    printf(1, "Value of (a + b ) is  %d\n", a + b )
else
    printf(1, "Value of (a + b ) is  %d\n", 0 )
end if

これは、次の結果を生成します-

Value of (a + b ) is  30

_if …​ label …​ then_ステートメント

*if* ステートメントには、最初の *then* キーワードの直前にラベル句を含めることができます。 *elsif* 句にラベルを付けることはできません。

_if…lable_は、ifブロックとラベルの名前に単一または複数の単語を含む二重引用符で囲まれた定数文字列を指定するためだけに使用されます。 labelキーワードは大文字と小文字が区別されるため、 label と記述する必要があります。

構文

ラベル句の構文は次のとおりです-

if expression label "Label Name" then
   -- Executes when the boolean expression  is true
end if

#!/home/euphoria-4.0b2/bin/eui

integer a = 10
integer b = 20

if (a + b) = 40 label "First IF Block" then
   printf(1, "Value of (a + b ) is  %d\n", a + b )
elsif (a + b) = 45 then
   printf(1, "Value of (a + b ) is  %d\n", a + b )
elsif (a + b) = 30 then
   printf(1, "Value of (a + b ) is  %d\n", a + b )
else
   printf(1, "Value of (a + b ) is  %d\n", 0 )
end if

これは、次の結果を生成します-

Value of (a + b ) is  30

ネストされた_if …​ else_ステートメント

*if…else* ステートメントをネストすることは常に有効です。 つまり、1つのif-elseステートメントを別のif-elseステートメント内に含めることができます。

構文

ネストされた_if …​ else_の構文は次のとおりです-

if expression1 then
    -- Executes when the boolean expression1  is true
   if expression2 then
       -- Executes when the boolean expression2  is true
   end if
end if

#!/home/euphoria-4.0b2/bin/eui

integer a = 10
integer b = 20
integer c = 0

if c = 0 then
   printf(1, "Value of c is equal to %d\n", 0 )
   if (a + b) = 30 then
      printf(1, "Value of (a + b ) is  equal to %d\n", 30)
   else
      printf(1, "Value of (a + b ) is equal to  %d\n", a + b )
   end if
else
   printf(1, "Value of c is equal to %d\n", c )
end if

これは、次の結果を生成します-

Value of c is equal to 0
Value of (a + b ) is  equal to 30