Notes
In Apex, the break
keyword is used to exit a loop prematurely, before its normal termination condition is met. When the break
statement is encountered inside a loop (such as a "for" or "while" loop), the loop immediately terminates, and the program execution continues with the next statement after the loop.
Here's an example of using the break
statement in a "for" loop:
for (Integer i = 0; i < 10; i++) {
System.debug('Iteration ' + i);
if (i == 5) {
System.debug('Breaking out of the loop at iteration 5');
break;
}
}
Here's a quick example of using break
in a "while" loop:
Integer counter = 0;
while (true) {
System.debug('Counter: ' + counter);
counter++;
if (counter == 3) {
System.debug('Breaking out of the loop at counter 3');
break;
}
}
Video
Video does not exists.