×
Apex 编程教程Apex 环境Apex 示例Apex 数据类型Apex 变量Apex 字符串Apex 数组Apex 常量Apex 决策Apex 循环Apex 集合Apex 类Apex 类方法Apex 对象Apex 接口Apex DMLApex 数据库方法Apex SOSLApex SOQLApex 安全性Apex 调用Apex 触发器Apex 触发设计模式Governer Limits调节器限制Apex 批量处理Apex 调试Apex 测试Apex 部署Apex 字符串Apex 数组

Apex if else语句


Apex 决策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代码块,否则将执行代码块。

流程图:

流程图


示例:
假设,我们的化工公司有两类客户:高级和正常。 根据客户类型,我们应该提供折扣和其他好处,如售后服务和支持。 下面是这个的实现。
//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条件将被触发。

Apex 决策Apex 决策


分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)