if语句由布尔表达式后跟一个或多个语句组成。
语法:
如果布尔表达式的计算结果为true,那么if语句中的代码块将被执行。 如果布尔表达式的计算结果为false,那么第一组代码在if语句结束后(在关闭大括号之后)将被执行。
- if boolean_expression {
- /* statement(s) will execute if the boolean expression is true */
- }
流程图:
假设,我们的化工公司有两类客户:高级和正常。 根据客户类型,我们应该提供折扣和其他好处,如售后服务和支持。 下面是这个的实现。
因为“Glenmarkone”是一个高级客户,所以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');
- }
转载本站内容时,请务必注明来自W3xue,违者必究。