Apex - if 语句
if 语句由一个布尔表达式后跟一个或多个语句组成。
语法
if boolean_expression { /* 如果布尔表达式为 true,则语句将执行 */ }
如果布尔表达式的计算结果为 true,则 if 语句内的代码块将被执行。 如果布尔表达式的计算结果为 false,则将执行 if 语句结束后(右大括号之后)的第一组代码。
流程图
示例
假设我们的公司有两类客户:高级客户和普通客户。 根据客户类型,我们应该为他们提供折扣和其他福利,例如售后服务和支持。 以下是该方法的一个实现。
//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 块将根据条件执行。