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 if else. Show all posts
Showing posts with label if else. 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!!