May 2019
Intermediate to advanced
546 pages
12h 41m
English
The while loop executes a block of statements for an indefinite number of times, as long as a Boolean condition remains true. The basic syntax is as follows:
while (Boolean_Condition){ // execute the code block}
The difference from the do-while loop is the check of the condition. In the while loop, the condition is checked before the execution of the code; in the do-while loop, the condition is checked after the execution of the code, which results in the fact that the code will certainly be executed at least once!
The same logic for counting from 1 to 10 in a while loop looks like this:
Integer iCount = 1;while (iCount < 11){ System.debug(iCount); iCount++;}
This gives exactly the same output, but with less code!
Read now
Unlock full access