Loops

Summary

This post will cover topics below:

  • What is Loops.
  • Type of Loops: While and For Loops.
  • various types of For Loops.
  • Exercises.

Introduction

Loops

Executes code statements repeatedly based on conditions. Instead of write the same statements for a hundred times, we can use loops to solve that issue. There are various types of loops that we can use depend on the requirements.  There are 5 procedural types of the loops in Apex. The loops are:

  • do {Statement} while (boolean_condition);
  • while (boolean_condition) {statement};
  • for (initial; condition; increment) {statement; }
  • for (variable : collection) {statement;}
  • for (variable : [soql_query]) { statement; }

 Below are loop control structure:

  • break; exits the entire loop
  • continue; skip to next iteration of the loop

While Loop

Syntax:

while(boolean_condition) {

statement(s);

}

Example:


Integer counter = 0; //declare and initialise variable

while(counter < 10) {
    System.debug('While loop ' +counter); //while loop 0 - 9
    counter++; //increment counter
}

Another Example:


Integer sum = 0, counter = 0;

//loop with condition
while(counter < 5) {
    sum += counter; //add counter into sum
    counter++; //increment counter by 1
    System.debug('sum in while ' +sum); //0, 1, 3, 6, 10
}

System.debug('sum is ' +sum); //10

For loop

First type is traditional for loop.

Syntax:

for (init_statement; condition; iteration) {

statement(s);

}

Example:


for(Integer i = 0; i < 5; i++) {
    System.debug('Value of counter is ' +i); // 0 - 4
}

For Loop - Collections

It uses to iterate collections. Collections is an array in other programming language. In Apex, it is a List or Set or Map.

Syntax:

for (variable : list_or_set) {

statement(s);

}

Example: 


List numList = new List{1, 2, 3, 4, 5}; //define List Integer variable

for(Integer i : numList) {
   System.debug(i); //print 1 - 5
}

Another Example:


List numList = new List(); //define List Integer variable
for(Integer i = 0; i < 5; i++) {
   numList.add(i); //add number into list
   System.debug(numList[i]); //print 0 - 4
}

For Loop - SOQL Query

It uses to iterate SOQL Query. SOQL is used to query an object with its related object in Salesforce.

Syntax:

for(variable : [soql_query]) {

statement(s);

}

Example


String acctName = 'KenCorp'; //declare and initial variable
for (Account a : [SELECT Id, Name from Account
                  where Name =: s ]) {
   System.debug(a); //Account:{Id=0012x000004SNHTAA4, Name=KenCorp}
}

Another Example:


//Query accName start with ken
String acctName = 'Ken'; //declare and initial variable
for (Account a : [SELECT Id, Name from Account
                  where Name LIKE :(acctName+ '%')]) {
   //Account:{Id=0012x000004SNHTAA4, Name=KenCorp}; Account:{Id=0012x000004TPL5AAO, Name=KenTest1}
   System.debug('accounts are: ' +a); 
}

Exercises

Future Tuition

Suppose tuition is $12,000 this year and tuition increase 5% each year. How many years will it take for the tuition to be doubled? (use for loop and while loop)

Math substraction with random number

Generate 5 random numbers substraction questions. Ensure that number1 has higher value than number2. Thus, it does not have negative value.

Leave a Reply