Before Going to Apex Loops let’s learn about loops .
What Is loops?
Loops are used to execute one or more statement to desired number of times. All Programming languages provides various types of loops . bascially lopps are used to iterate
Apex Supports Two Types Of loops:
- While loops
- For loops
Apex Loops: Apex supports two types of loops
To repeatedly execute a block of code while a given condition holds true, use a loop. Apex supports do-while, while, and for loops.
1) While Loops
A do-while loop repeatedly executes a block of code as long as a Boolean condition specified in the whilestatement remains true. Execute the following code:
Integer count = 1;
Do {
System.debug(count);
Count++;
} while (count < 11);
The while loop repeatedly executes a block of code as long as a Boolean condition specified at the beginning remains true.
Integer count = 1;
While (count < 11) {
System.debug(count);
count++;
}
2)For Loops
There are three types of for loops. The first type of for loop is a traditional loop that iterates by setting a variable to a value, checking a condition, and performing some action on the variables.
for (Integer i = 1; i <= 10; i++){
System.debung(i);}
A second type of for loop is available for iterating over a list or a set.
Integer[] myInts =new Integer[] {10,20,30,40,50,60,70,80,90,100};
For (Integer i: myInts) {
System.debug(i);
}
The third type of for loop is SOQL for Loop.
We use SOQL for loops to iterate over all of the sObject records returned by a SOQL query.
here is the Example :
for (your variable : [soql_query]) { code_block }
Hits: 238