Apex-if-elseif-else-statement

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

Apex-if elseif elseステートメント

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

構文

*if ... else if ... else* ステートメントの構文は次のとおりです-
if boolean_expression_1 {
  /*Executes when the boolean expression 1 is true*/
} else if boolean_expression_2 {
  /*Executes when the boolean expression 2 is true*/
} else if boolean_expression_3 {
  /*Executes when the boolean expression 3 is true*/
} else {
  /*Executes when the none of the above condition is true*/
}

化学会社に、プレミアムとノーマルの2つのカテゴリの顧客がいるとします。 顧客のタイプに基づいて、アフターサービスやサポートなどの割引やその他の特典を提供する必要があります。 次のプログラムは、同じ実装を示しています。

//Execute this code in Developer Console and see the Output
String customerName = 'Glenmarkone';//premium customer
Decimal discountRate = 0;
Boolean premiumSupport = false;
if (customerName == 'Glenmarkone') {
   discountRate = 0.1;//when condition is met this block will be executed
   premiumSupport = true;
   System.debug('Special Discount given as Customer is Premium');
}else if (customerName == 'Joe') {
   discountRate = 0.5;//when condition is met this block will be executed
   premiumSupport = false;
   System.debug('Special Discount not given as Customer is not Premium');
}else {
   discountRate = 0.05;//when condition is not met and customer is normal
   premiumSupport = false;
   System.debug('Special Discount not given as Customer is not Premium');
}