SFDC Stop - Always the latest about Salesforce


Full Tutorial Series with videos, free apps, live sessions, salesforce consulting and much more.


Telegram logo   Join our Telegram Channel
Showing posts with label Operators. Show all posts
Showing posts with label Operators. Show all posts

Wednesday, 4 December 2024

Control Flow Statements in Apex | if else | switch case | loops - Salesforce Apex Tutorial Part 25 to 30

Aloha Trailblazers,

In this post, I'm sharing the code snippet for Coding with Confidence: The Fun Way to Learn Salesforce Apex tutorial series part 25 to 30:
// * Control Flow statements: Used to control the flow of code execution. You can skip a block of code or have a block of code executed repeatedly. These statements include: if-else (conditional) statements, switch statements and loops

// * if - else / Conditional Statements

if(<Boolean>) {

} else {

}

// * if Statement
Integer num = 10;
System.debug('Start...');
if(num > 50) {
    System.debug('num is greater than 50');
}
System.debug('End...');

// * if Statement with else
Integer num = 100;
System.debug('Start...');
if(num > 50) {
    System.debug('num is greater than 50');
    System.debug('num is greater than 50');
    System.debug('num is greater than 50');
} else {
    System.debug('num is not greater than 50 and is: ' + num);
    System.debug('num is not greater than 50 and is: ' + num);
    System.debug('num is not greater than 50 and is: ' + num);
}
System.debug('End...');

// * Repeated if else statements, if else chaining
Integer num = 50;
System.debug('Start...');
if(num > 50) {
    System.debug('num is greater than 50');
} else if(num < 50) {
    System.debug('num is less than 50');
} else if(num == 50) {
    System.debug('num is equal to 50');
} else {
    System.debug('num is null');
}
System.debug('End...');

// * nested if else
enum SalesforceRelease {
    SPRING,
    SUMMER,
    WINTER
}

Integer age = 10;
SalesforceRelease currentRelease = SalesforceRelease.WINTER;

if(currentRelease == SalesforceRelease.SPRING) {
    if(age >= 18) {
        System.debug('Richard will drive a bike.');
    } else {
        System.debug('Richard will board a bus.');
    }
} else if(currentRelease == SalesforceRelease.SUMMER) {
    if(age >= 18) {
        System.debug('Richard will drive a car.');
    } else {
        System.debug('Richard will board a metro.');
    }
} else if(currentRelease == SalesforceRelease.WINTER) {
    if(age >= 18) {
        System.debug('Richard will drive a truck.');
    } else {
        System.debug('Richard will stay home.');
    }
}

// * Switch Statements

switch on expression { // * Value can be Integer, Long, sObject, String (case sensitive) or Enum
    when value1 {

    }
    when value2 {

    }
    when else {

    }
}

Integer i = 20;
switch on i {
    when 1 {
        System.debug('i is 1');
    }
    when 2 {
        System.debug('i is 2');
    }
    when null {
        System.debug('i is null');
    }
    when else {
        System.debug('i is none of these');
    }
}

enum SalesforceRelease {
    SPRING,
    SUMMER,
    WINTER,
    AUTUMN
}

SalesforceRelease currentRelease = SalesforceRelease.AUTUMN;

switch on currentRelease {
    when SPRING, SUMMER {
        System.debug('Richard will ride a bike');
    }
    when WINTER {
        System.debug('Richard will drive a car');
    }
    when null {
        System.debug('Richard will do nothing');
    }
    when else {
        System.debug('Richard will stay at home');
    }
}

// * Loops

// * While loop

while(condition) {
    // * Code
}

Integer i = 100;
while(i<11) {
    System.debug('Value of i = ' + i);
    i++;
}

Integer i = 1;
while(i<6) { // * Infinite loop
    System.debug('Value of i = ' + i);
}

// * Do while loop

do {
    // * Code
} while(condition);

Integer i=100;
do {
    System.debug('Value of i = ' + i);
    i++;
} while(i<11);

List<Integer> numbers = new List<Integer>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(4);
numbers.add(5);
numbers.add(6);
numbers.add(7);
numbers.add(8);
numbers.add(9);
numbers.add(10);
System.debug(numbers);

List<Integer> numbers = new List<Integer>();
Integer i = 1;
while(i<101) {
    numbers.add(i);
    i++;
}
System.debug(numbers);
System.debug(numbers.size());

// * For Loop

// * Traditional for loop
for(initialization; condition; operation) {
    // * Code
}

for(Integer i=1; i<6; i++) {
    System.debug('Value of i = ' + i);
}

for(Integer i=1, j=0; i<6; i++) {
    System.debug('Value of i = ' + i + ' & j = ' + j);
}

// * List/Set iteration for loop
for(variable : <list or set>) {
    // * Code
}

Set<Integer> numbers = new Set<Integer>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for(Integer num : numbers) {
    if(num==5) {
        // break;
        continue;
    }
    System.debug('Current element of set = ' + num);
}

// * Adding/Removing elements while iterating a collection is not allowed

// * Break and Continue keywords

Learn Apex Control Flow Statements: If-Else Explained in Depth - Salesforce Apex Tutorial Part 25

Learn Apex Control Flow Statements: Switch Statements Explained - Salesforce Apex Tutorial Part 26

While Loop Tutorial (Beginner-Friendly) | Control Flow Statements - Salesforce Apex Tutorial Part 27

Do-While Loop Tutorial (Newbie Friendly) | Control Flow Statement - Salesforce Apex Tutorial Part 28

For Loop Tutorial (Beginner-Friendly) | Control Flow Statements - Salesforce Apex Tutorial Part 29

Break and Continue Keywords in Loop | Control Flow Statements - Salesforce Apex Tutorial Part 30


Check out the tutorials and let me know your feedback in the comments down below.

Happy Trailblazing!!

Monday, 7 October 2024

Expressions and Operators in Apex - Salesforce Apex Tutorial Part 17 to Part 24

Aloha Trailblazers,


In this post, I'm sharing the code snippet for Coding with Confidence: The Fun Way to Learn Salesforce Apex tutorial series part 17 and above:
// * Expressions and Operators in Apex

/*
? What is an Expression?
* An expression is a combination of variables, operators, and method calls that results in a specific value. There can be different types of expressions:
*/

// * Literal expression

Integer num = 2+3;
System.debug('2+3=' + num);

/*
*   New instance of an object, list or map
*   Left hand of an assignment operators
*/

List<Integer> numbers = new List<Integer>();
Map<Integer, String> studentsMap = new Map<Integer, String>();

// * Other expressions include SOQL, static method invocation etc.

/*
? What are Expression Operators?
* Expression operators can join expressions to create compound expressions. For example:
*/

Integer num = (2+3) + (5-4);
System.debug(num);

// * Different types of operators in apex:

Integer num = 1;
System.debug('Value of num = ' + num);

// * Assignment operator
num = 10;
System.debug('Value of num after assigning 10 = ' + num);

// * Addition operator
Integer num;
num = 5 + 10;
System.debug('Value of num (5+10) = ' + num);

// * Subtraction operator
num = 5 - 10;
System.debug('Value of num (10-5) = ' + num);

// * Multiplication operator
Integer num;
num = 10 * 5;
System.debug('Value of num (10*5) = ' + num);

// * Division operator
Decimal num;
num = 10.0 / 3.0;
System.debug('Value of num (10/5) = ' + num);

Integer num = 0;
System.debug('Value of num, Prefix = ' + ++num + ', Postfix = ' + num++ + ', Postfix = ' + num++);
System.debug('Value of num = ' + num);

// * Increment operator
num++;
System.debug('Value of num after increment = ' + num);

// * Decrement operator
num--;
System.debug('Value of num after decrement = ' + num);

// * Prefix and Postfix concept

// * Part 2

// * Negation operator
Integer num = -10;
System.debug('Value of negative num = ' + -num);

Integer num = 5;
// * Addition Assignment operator
num += 15;
// num = num + 15;
System.debug('Value of num = ' + num);

Integer num = 5;
// * Multiplication Assignment operator
num *= 2; // * num = num * 2;
System.debug('Value of num = ' + num);

Integer num = 5;
// * Subtraction Assignment operator
num -= 5; // * num = num - 5;
System.debug('Value of num = ' + num);

Integer num = 10;
// * Division Assignment operator
num /= 5; // * num = num / 5;
System.debug('Value of num = ' + num);

Boolean a = false, b = false, c;

// * AND operator
c = a && b;
System.debug('a && b = ' + c);

Boolean a = true, b = true, c;
// * OR operator
c = a || b;
System.debug('a || b = ' + c);

Boolean a = true, b = false, c;
// * NOT operator - Unary operator
c = !a;
System.debug('!a = ' + c);
c = !b;
System.debug('!b = ' + c);

// * A note about Null pointer exception. This exception occurs when you're trying to use a variable and the value of that variable is null.

Boolean a = true, b, c = false;
a = c || b;
System.debug('Result = ' + a);

// * Less than operator
Boolean c = 25 < 25;
System.debug('Is 5 less than 10? -> ' + c);

// * Greater than operator
Boolean c = 10 > 25;
System.debug('Is 10 greater than 5? -> ' + c);

// * Less than or equal to operator
Boolean c = 25 <= 25;
System.debug('Is 5 less than or equal to 10? -> ' + c);

// * Greater than or equal to operator
Boolean c = 10 >= 20;
System.debug('Is 10 greater than or equal to 5? -> ' + c);


// * Equality operator - this checks if the values are equal
Integer num1 = 5, num2 = 5;
Boolean result = num1==num2;
System.debug('Are num1 and num2 equal? -> ' + result);

Integer num1 = 5, num2 = 10;
Boolean result = num1==num2;
System.debug('Are num1 and num2 equal? -> ' + result);

// * Inequality operator
Integer num1 = 5, num2 = 5;
Boolean result = num1!=num2;
System.debug('Are num1 and num2 not equal? -> ' + result);

// * Exact equality operator
Integer num1 = 5, num2 = 5;
Boolean result = num1===num2;
System.debug('Are num1 and num2 equal? -> ' + result);

List<String> words1 = new List<String>{'Richard', 'Hendricks'};
List<String> words2 = words1;
Boolean result = words1===words2;
System.debug('words1 and words2 are equal? -> ' + result);

// words2 = new List<String>{'Richard', 'Hendricks'};
// result = words1===words2;
// System.debug('words1 and words2 are equal? -> ' + result);

// * Exact Inequality operator
result = words1!==words2;
System.debug('words1 and words2 are not equal? -> ' + result);

// * Paranthesis - Operator Precedence
Integer result = 22+2-4/2*5;
System.debug('Value of result = ' + result);

Integer result = (22+2-4)/(2*5);
System.debug('Value of result = ' + result);

// * Ternary Operator
Integer age = 15;
String result = age >= 18 ? 'Richard can drive' : 'Richard cannot drive';
System.debug(result);

/*
* Safe navigation operator - It is represented by ?.
* This operator follows the rule, if the left hand side of chain is null, the right hand side isn't evaluated and null is returned. Otherwise, right hand side is evaluated and it's value is returned. This saves the code from throwing a NullPointerException
*/
List<Integer> numbers = new List<Integer>{1, 2, 3, 4, 5};
Integer size = numbers?.size();
System.debug('List size = ' + size);

// * Note: Safe navigation operator cannot be used at the left side of an assignment

Integer a = 10, b = null;
a?.b = 5;

/*
* Null Coalescing Operator - It is represented by ??
* This operator returns the left hand argument if the left hand argument isn't null. Otherwise it returns the right hand argument. This operator is left-associative, it means that only the left-hand operand is evaluated first. The right-hand operand is evaluated ONLY if the left-hand operand is null
*/

Integer num = 20;
Integer notNullNum = num ?? 10;
System.debug('Number = ' + num);
System.debug('Not null number = ' + notNullNum);

// * Note: Null coalescing operator cannot be used at the left side of an assignment
Integer num;
Integer num1 = 10;
// num??num1++; // * a++ => a = a+1; num??num1++ => num??num1 = num??num1 + 1;
num??num1 = 11;

// * Operator Precedence: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_expressions_operators_precedence.htm

YouTube Tutorials

You can check out the tutorials below as well:

Expressions and Expression Operators in Apex - Salesforce Apex Tutorial Part 17

Arithmetic Operators in Apex - Salesforce Apex Tutorial Part 18

Increment and Decrement Operators, Prefix and Postfix in Apex - Salesforce Apex Tutorial Part 19

Negation and Arithmetic Assignment Operators in Apex - Salesforce Apex Tutorial Part 20

Boolean Operators in Apex, Truth Table and Null Pointer Exception - Salesforce Apex Tutorial Part 21

Greater/Less Than, Greater/Less Than Equal To Boolean Operators - Salesforce Apex Tutorial Part 22

Equality, Inequality, Exact Equality & Exact Inequality Operators - Salesforce Apex Tutorial Part 23

Operators: Precedence, Ternary, Safe Navigation & Null Coalescing - Salesforce Apex Tutorial Part 24


Check out the tutorials and let me know your feedback in the comments down below.

Happy Trailblazing!!