Apex Loops

Apex Loops

Loops are used to repeat a block of code multiple times. Instead of writing the same lines again and again, loops allow you to automate repetition based on a condition. 

In Apex, the most commonly used loops are: 

• for loop
• while loop
• do-while loop

1. for Loop

The for loop is used when you know how many times you want to run the code. 

Syntex: 

for (initialization; condition; update) {
// Code to repeat
}

Part Description
Initialization Starting point (e.g., int i = 0)
Condition Checked before each loop
Update Changes the loop variable (i++)

Example:

for (Integer i = 1; i <= 5; i++) {
System.debug(‘Number: ‘ + i);
}

Code Explanation:

Integer i = 1: Initializes a counter variable i with value 1.
• i <= 5: Loop runs as long as i is less than or equal to 5.
• i++: Increments i by 1 after each loop iteration.
• System.debug(‘Number: ‘ + i);: Prints the current value of i with the prefix ‘Number: ‘.

Output:

Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

2. while Loop

The while loop is used when you don’t know the exact number of repetitions, but want to loop as long as a condition is true. 

Syntex: 

while (condition) {
// Code to repeat
}

Example:

Integer i = 1;

while (i <= 3) {
System.debug(‘Count: ‘ + i);
i++;
}

Code Explanation:

hile (i <= 3): The loop continues to run as long as i is less than or equal to 3.
System.debug(‘Count: ‘ + i);: Prints the current value of i with the prefix ‘Count: ‘.
i++;: Increments i by 1 after each iteration to move toward the loop’s exit condition.

Output:

Count: 1
Count: 2
Count: 3

3. do-while Loop

This is like the while loop, but it always executes at least once, even if the condition is false. 

Syntex: 

do {
// Code to repeat
} while (condition);

Example:

Integer i = 5;

do {
System.debug(‘Hello: ‘ + i);
i–;
} while (i > 3);

Code Explanation:

• Integer i = 5;: Initializes a counter variable i with value 5.
• do { … } while (i > 3);:
• The do block runs at least once, even if the condition is false
• After executing the block, the condition i > 3 is checked.
• If true, the loop repeats.
• System.debug(‘Hello: ‘ + i);: Prints the current value of i with the prefix ‘Hello: ‘.
• i–;: Decrements i by 1 after each iteration.

Output:

Hello: 5
Hello: 4

Key Difference: while checks before running; do-while checks after running once 

Code Example

1. for loop for Sum

Integer sum = 0;

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

System.debug(‘Sum = ‘ + sum);

Code Explanation:

Integer sum = 0;: Initializes a variable sum to store the total, starting at 0.
for (Integer i = 1; i <= 5; i++): • Loop starts with i = 1 and continues while i <= 5, incrementing i by 1 each time.
sum += i;: Adds the current value of i to sum during each loop iteration.
System.debug(‘Sum = ‘ + sum);: Prints the final value of sum after the loop ends.

Output:

Sum=15

2. while loop for Countdown

Integer count = 3;

while (count > 0) {
System.debug(‘Countdown: ‘ + count);
count–;
}

Code Explanation:

Integer count = 3;: Initializes a counter variable count with value 3.
while (count > 0): The loop runs as long as count is greater than 0.
System.debug(‘Countdown: ‘ + count);: Prints the current value of count with the prefix ‘Countdown: ‘.
count–;: Decreases count by 1 after each iteration to move toward ending the loop.

Output:

Countdown: 3
Countdown: 2
Countdown: 1

.3. do-while loop with Condition False

Integer num = 0;

do {
System.debug(‘This runs at least once.’);
} while (num > 0);

Code Explanation:

• Integer num = 0;: Initializes the variable num with value 0.
• do { … } while (num > 0);:
• The code inside the do block runs once before the condition is checked.
• After the first execution, the condition num > 0 is false, so the loop stops.
• System.debug(‘This runs at least once.’);: Prints a message showing that the block executes at least once, regardless of the condition.

Output:

This runs at least once.

Tasks for Practice

Task 1: Use a for loop to print the first 10 natural numbers.

Task 2: Use a while loop to print numbers from 10 to 1 (reverse order).

Task 3: Use a for loop to print the multiplication table of 4 (e.g., 4 × 1 = 4 ... 4 × 10 = 40)

Task 4: Use a do-while loop to print “Welcome” 3 times.

Task 5: Use a loop to calculate the factorial of a number (e.g., 5! = 5×4×3×2×1 = 120)