Apex-if-else-statement
提供:Dev Guides
Apex-if elseステートメント
*if* ステートメントの後にオプションの *else* ステートメントを続けることができます。これは、ブール式がfalseの場合に実行されます。
構文
if boolean_expression {
/*statement(s) will execute if the boolean expression is true*/
} else {
/*statement(s) will execute if the boolean expression is false*/
}
ブール式がtrueと評価された場合、* ifブロックのコード*が実行されます。それ以外の場合、コードのブロックが実行されます。
流れ図
例
化学会社に、プレミアムとノーマルの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 {
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');
}
「Glenmarkone」はプレミアム顧客であるため、条件に基づいてifブロックが実行され、残りのケースではelse条件がトリガーされます。