Selections
Summary
This post will cover topics below:
- One or two-way if statements.
- Nested if statements.
- Ternary operator.
- Switch statements.
- Exercise.
Introduction
Selection statements
Let us choose actions with two or more alternative conditions. Conditions are boolean expressions which compare two values. The result of comparison is a boolean value: true or false.
Let us choose actions with two or more alternative conditions. Conditions are boolean expressions which compare two values. The result of comparison is a boolean value: true or false.
There are several types of selection statements:
- One-way OR two-way if statements
- Nested if statements
- switch statements
Switch statements have several types and more flexible in apex. Switch expression can accept literal value, method, and sObjects. sObject is a Salesforce object that will discuss in another post.
One or two-way if statements
Syntax:
if(boolean-expression) {
statement(s);
}
else if(boolean-expression) {
statement(s);
}
else {
statement(s);
}
Info - Syntax
else if (), else -
are OPTIONAL.
//define variables
Double radius = 5, area, pi = 3.1415;
if(radius > 0) { //if radius above 0
area = radius * radius * pi; //perform calc
System.debug('Area of cicrle is ' +area); //result - 78.5375
}
else {
System.debug('radius is negative')
}
Nested If Statement
An if statement within if statements.
Syntax:
if(boolean-expression) {
if(boolean-expression) {
statement(s);
}
}
else {
statement(s);
}
Example:
Boolean isTrue = True; //declare Boolean variable with value of True
System.debug('value of Boolean is ' +isTrue); //print - True
Ternary Conditional Operation
Shortcut of if else statements.
Syntax:
boolean-expression ? expression_if_true : expression_if_false;
Info
The symbol ? and : appear together in conditional expression. It is called ternary operator because it uses three operands.
Switch Statement
Syntax:
switch on expression {
when value1 {
//statement(s)
}
when value2, value3 {
//statement(s)
}
when else {
//statement(s)
}
}
Note
Switch expression can be one of the following data types:
Integer, Long, sObject, String, Enum
Integer, Long, sObject, String, Enum
Info
Switch expression can be a method, enum, method, and sObject
Example – single and multiple values:
Integer num1 = 3; //declare variable
switch on num1 {
when 0 { //if 0, print num1
System.debug('num1 is ' +num1);
}
when 1 { //if 1, print num1
System.debug('num1 is ' +num1);
}
when 2, 3 { //if 2 or 3, print num1
System.debug('num1 is ' +num1);
}
when else {
System.debug('num1 is ' +num1);
}
}
Example – sObject
switch on sobject {
when Account a {
System.debug('account ' +a);
}
when Contact c {
System.debug('Contact ' +c);
}
when null {
System.debug('null');
}
when else {
System.debug('default');
}
}
Exercise
- Compute BMI
Enter your weight in kg and height in cm. calculate BMI with formula:
bmi = weightKg / (heightInMeters * heightInMeters)
bmi:
- below 16 – serious underweight
- 16 – 18 – underweight
- 18 – 24 – normal weight
- 24 – 29 – overweight
nested if
Suppose x = 3 and y = 2.
- What is the output if x = 3 and y = 4
- What is the output if x = 2 and y = 2
nested if
Use your own gender and age. If you are a man or woman and your age above 35, print on the screen “Do more cardio and eat healthy food to maintain your health”. If you are a man or woman and your age below 35, print on the screen “Do cardio and eat delicious food to maintain your health”.
Use your own gender and age. If you are a man or woman and your age above 35, print on the screen “Do more cardio and eat healthy food to maintain your health”. If you are a man or woman and your age below 35, print on the screen “Do cardio and eat delicious food to maintain your health”.