目录

if statement

if语句由一个布尔表达式后跟一个或多个语句组成。

语法 (Syntax)

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

如果布尔表达式的计算结果为true,那么将执行if语句中的代码块。 如果布尔表达式的计算结果为false,那么将执行if语句结束后(在结束大括号之后)的第一组代码。

流程图 (Flow Diagram)

如果声明

例子 (Example)

假设,我们的化学公司拥有两类客户 - 高级和普通。 根据客户类型,我们应该为他们提供折扣和其他好处,如售后服务和支持。 以下是对此的实现。

//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块将根据条件执行。

↑回到顶部↑
WIKI教程 @2018