Loops in Apex

8 minute read
0
Loops in Apex

In Apex programming languages, loops are used to execute a set/piece of code repeatedly with the desired number of iteration till condition is true. There are basically three types of loops in Apex.

  • for loop
  • while loop
  • do-while loop

For loop in depth: The Apex for loop is used to iterate a piece of the program several times, till then condition is satisfied. If the number of iteration is fixed, it is advisable to use for loop.

Apex Simple For Loop

A simple for loop is the same as C/C++/Java. We can initialize the variable, check condition and then increment/decrement value. It consists of 4 parts:

  1. Initialization: It is the initiation of condition which is executed once when the loop get started. We can initialize the variable, or we can use an already initialized this variable. It is completely optional condition within the for loop.
  2. Condition: It is the second condition which is executed each and every time to evaluate the condition of the loop. It continues execution till the condition is true. It must return Boolean value either true or false. It is again an optional condition.
  3. Statement: The statement of the loop is executed each and every time until the second condition is false.
  4. Increment/Decrement: It increments/decrements of the variable value. It is an optional condition.

Syntax of for loop:

for (initialization;condition;increment/decrement) {
  
//statement or code to be executed  

}


Flow of for loop:

Example:

//Apex Program to demonstrate the example of for loop  
//which prints table of 2  

public class ForLoopDemo {
    
    public void printForLoop() {
        
        for(integer i=1;i<=10;i++) {
            
            System.debug(i*2);
        }
    }
}

Apex While Loop

The Apex while loop is used to iterate a piece of the program several times, till condition is true. If the number of iteration is not fixed, it is advisable to use while loop.

Syntax of While loop:

while(condition){  
//code to be executed  
}  

Example of While loop:

public class WhileDemo {  
public void getcount() {  
    integer i=1;  
    while(i<=10){  
        System.debug(i);  
    i++;  
    }  
}  
}


Java Infinitive While Loop

If you pass true in the while loop, it will become infinitive while loop.

Syntax infinitive while loop:

while(true){  
//code to be executed  
}

Example of infinitive while loop:

public class WhileInfinitiveDemo {  
public void printInfinitiveValue()
 {  
    while(true){  
        System.debug('infinitive while loop');  
    }  
 }  
}

Apex do-while Loop

The Apex do-while loop is used to iterate a piece of the program several times. If the number of iteration is not fixed and you must have to execute the loop at least once in a life of loop, it is advisable to use do-while loop.

The Apex do-while loop is executed at least once because condition is checked after loop body in Apex do-while loop.

Syntax of do-while loop:

do{  
//code to be executed  
}while(condition);  

Flow of do-while loop:

Example of Do-while loop:

public class DoWhileDemo {  
public void printValue() {  
    integer i=1;  
    do{  
        System.debug(i);  
    i++;  
    }while(i<=10);  
 }  
}

Post a Comment

0Comments
Post a Comment (0)