Apex-if-statement

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

Apex-ifステートメント

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

構文

if boolean_expression {
  /*statement(s) will execute if the boolean expression is true*/
}

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

流れ図

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');
}

「Glenmarkone」はプレミアム顧客であるため、条件に基づいてifブロックが実行されます。