do-while Loop – Operators and Control Statements – Apex Programming – Salesforce Certified Platform Developer I Study Guide

do-while Loop: The do-while procedural loop is similar to the while loop, but it executes the code block at least once, regardless of whether the condition is true or false. It’s similar to the while loop, but with a key difference: the do-while loop always executes its block of code at least once before checking the loop condition. After the first execution, it then checks the condition and continues executing as long as the condition remains true.

Here’s an example using the do-while loop in Apex to input a number from the user and ensure it’s within a valid range:

Integer count = 1;

do {

System.debug(‘Count: ‘ + count);

count++;

} while (count <= 0);

In this example the initial value of count is 1, which is not less than or equal to

  • Therefore, the condition is false from the beginning, and the loop will not execute beyond the initial iteration.

The output of the code will be

Count: 1

Notice that the loop executes the code block at least once before checking the condition. This can be useful when you want to ensure that a certain piece of code runs before condition checking, regardless of whether the condition is initially met or not.

The do-while loop is particularly helpful when you need to ensure that an action is performed before condition evaluation, or when you want to guarantee at least one execution of a loop.

break Statement: The break statement is used to exit a loop prematurely. In a loop (such as a for, while, or do-while loop), the break statement is used to exit the loop prematurely. When a break statement is encountered within the loop’s block of code, the program immediately exits the loop, and the subsequent iterations are not executed.

Example in a Loop:

  • Declare and initialize a list of integers List<Integer> numbers = new List<Integer>{1, 2, 3, 4, 5};
  • Use a for loop to iterate over the list

for (Integer num : numbers) {

if (num == 3) {

break;

}

System.debug(‘Number: ‘ + num);

}

In this example, we have a list of integers called numbers which contains the values 1, 2, 3, 4, and 5. The for loop iterates over each element in the list and assigns it to the variable num. Inside the loop, we check if num is equal to 3 using the if statement. If it is, we use the break statement to exit out of the loop prematurely.

When you run this code, you will see the numbers 1 and 2 printed in the debug logs. The break statement is used to terminate the execution of a loop prematurely. In this case, when num is equal to 3, the loop is exited, and no further numbers are processed.

continue Statement: The continue statement is used to skip the rest of the current iteration and proceed to the next iteration of a loop. It’s used when a certain condition is met, and you want to bypass the rest of the loop’s code for the current iteration and move on to the next iteration.

In a loop (such as a for, while, or do-while loop), the continue statement is used to skip the remaining code within the current iteration and move to the next iteration of the loop.

for (Integer i = 1; i <= 5; i++) {

if (i == 3) {

continue;

}

System.debug(‘Current i: ‘ + i);

}

In this example, when i is 3, the continue statement is encountered. This causes the loop to skip the rest of the code within the current iteration and move directly to the next iteration. As a result, the output would be

Current i: 1

Current i: 2

Current i: 4

Current i: 5

The continue statement is particularly useful when you want to avoid executing certain code within a loop iteration based on a specific condition. It allows you to control the flow of the loop without prematurely exiting the entire loop, as would be the case with the break statement.

Remember that the continue statement should be used thoughtfully to ensure that your loop continues to behave as expected. Misusing it could lead to unexpected results or an infinite loop if used incorrectly.