Apex 条件分岐if文の使い方のサンプル

環境
Salesforce

構文

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
if ([Boolean_condition])
// Statement 1
else
// Statement 2
if ([Boolean_condition]) // Statement 1 else // Statement 2
if ([Boolean_condition])
// Statement 1
else
// Statement 2

else の部分は常に省略可能で、最も近い if にグループ化されます。

操作例
1なら「東京」、2なら「大阪」、3なら「福岡」を表示すう

サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
//1 東京、2 大阪、3 福岡
Integer i = 1;
String tokyo = '東京';
String oosaka = '大阪';
String fukuoka = '福岡';
if (i == 1) {
System.debug(tokyo);
} else if (i == 2){
System.debug(oosaka);
} else {
System.debug(fukuoka);
}
//1 東京、2 大阪、3 福岡 Integer i = 1; String tokyo = '東京'; String oosaka = '大阪'; String fukuoka = '福岡'; if (i == 1) { System.debug(tokyo); } else if (i == 2){ System.debug(oosaka); } else { System.debug(fukuoka); }
//1 東京、2 大阪、3 福岡
Integer i = 1;

String tokyo = '東京';
String oosaka = '大阪';
String fukuoka = '福岡';

if (i == 1) {
    System.debug(tokyo);
} else if (i == 2){
    System.debug(oosaka);
} else {
    System.debug(fukuoka);
}

 

Apex

Posted by arkgame