5.2 Using for loop

The concept

One of the most powerful part of computer is it can perform repetitive tasks very efficiently. In order for computers to do that, we need to introduce loop.

List<String> seasons = new List<String>{'Winter', 'Spring', 'Summer', 'Fall'};
for(String season: seasons) 

List<Integer> numberList = new List<Integer>();
for(Integer i = 0; i < 10; i++) 
    numberList.add(i + 1);

Integer sum = 0;
for(Integer num: numberList)
    sum += num;
System.debug('Sum is: ' + sum);

The result should be similar to the following:

A bit explanation

for loop has two formats. The first one is more traditional:

for(initialisation; escape condition; afterthought)

Initialisation is the initialise of the looping variable. This is done for 1 time only which is before the loop begins.

Escape condition is when this statement becomes true, the loop ends.

Afterthought is executed every time one single loop run ends. So the loop works in the way described in the following picture:


Another format is usually called the foreach or for … in format. The grammar is:

for(T var: [list or set])

Here T is the type. var is the variable name. It basically means this will iterate through all the elements in the list or set. We will explain what is set in the container chapter.

For loop is a very powerful tool. It is used almost everywhere in programming world. We will talk more about using loop and condition statements with examples in next chapter.


Try to write a program to calculate 3 + 6 + 9 + … + 99 .

Next post

5.3 Using while loop

Subscribe to Sfdcinpractice

Subscribe to get the latest blogs and tutorials of sfdcinpractice. No spam, no trash, only the awesome posts from sfdcinpractice. 


  1. Paolo - May 30, 2017 @ 1:39 pm

    List seasons = new List {‘Winter’, ‘Spring’, ‘Summer’, ‘Fall’};// you forgot to define the list type ()
    for(String season: seasons)

    • LanceShi - May 30, 2017 @ 1:42 pm

      You are right. Thank you for that. I will update the post.

  2. Paolo - May 30, 2017 @ 1:40 pm

    List seasons = new List {‘Winter’, ‘Spring’, ‘Summer’, ‘Fall’};
    for(String season: seasons)

  3. Parul Tripathi - July 4, 2017 @ 4:58 pm

    Hi Lance ,

    I like your blogs a lot and have been following them for learning Apex from basic to have good foundation on programming basics.

    While I was trying the code mentioned above , I am getting error .
    I guess the Type is missed for line 7 while creating new numberlist.

    List numberList = new List();// Syntax as per my understanding :List numberlist = new List();

    Please correct me if I am wrong .

    Thanks again,


    • LanceShi - July 4, 2017 @ 11:15 pm

      Thanks for pointing this out. Yes, it is my fault. I have updated the code. Sorry about that.

  4. Parul Tripathi - July 4, 2017 @ 5:01 pm

    Hi Lance,

    when I am writing a comment to this blog and adding keyword for the list at line no 7. After posting the keyword is disappeared. Seems like some issue.

Leave a Reply

Your email address will not be published / Required fields are marked *