Blue Flower

Apex Learning

General Info

/* this comments out to close of */      
       
       

For Loops:

Loops through the entire list and then exits based on the size of the list.

List <string> myList=new List <string> {'purple','red','blue','yellow', 'green'};
//    for (integer k=0; k<=mylist.size ()-1; k++) (option 1)

// (shorthand option)
for(string j:mylist)

{
    system.debug (j);
}

 

Break the For Loop

Allows you to stop the execution of the loop at any point.

Example 1- With a List

List <integer> values=new List <integer> {1,2,3,8,-1,7,9};
for (integer i=0; i<values.size(); i++)
//is zero less than 7? If yes iterate through the loop and print the value (first-round = 1).
{
    System.debug (values [i]);
//checks to ensure that this condition is not met; if = no: reiterate loop with i++ value; if = yes: execute break

    if(values [i]==3)
    {
        break;
    }
  
}

 Example 2- General For loop

for (integer i=1; i<=100; i++)
//i=1 is initialization clause, and only runs 1x
//after i=1 it evaluates the loop conditions each time   
{
    System.debug (i);
    if(i==5)
        break;
}

 

Continue the For Loop

Allows you to stop the current loop and move to a new one.

For Each Loop = size of list does not matter in this type of loop

Example 1

Prints all the values in the list EXCEPT 77

List <integer> myList=new List <integer> {100,7,90,77,6,5};
    for (integer value:myList)
{
    if (value==77)
    {
        continue;
    }
    System.debug(value);
}

Example 2

Prints all the values in the list EXCEPT 77 (both times)

List <integer> myList=new List <integer> {100,7,90,77,6,5,77,900};
    for (integer j:myList)
{
    if (j==77)
    {
        continue;
    }
    System.debug(j);
}

This occurs because the continue sends it back through the loop BEFORE the System.debug is executed.

Example 3

List <string> myList=new List<string> {'cat','bear', 'dog','bird', 'giraffe'};
    for (string y:myList)
{
    if (y.startsWith('b'))
    {
        continue;
    }
    System.debug(y);
}

Prints only cat, dog and giraffe.

 

Nested Loops

For each positive iteration of the outer loop the inner loop will execute its entire set of criterion. Thus for the equation below the 'b' loop will execute 0-2 for each iteration of 'a'.

Example 1

A B A B A B A B
0 0 1 0 2 0 3 0
0 1 1 1 2 1 3 1
0 2 1 2 2 2 3 2

//outer loop

for(integer a=0; a<=3;a++)
{

//inner loop
    for (integer b=0; b<=2;b++)
    {
        System.debug ('a='+a+' b='+b);
    }
}

Example 2

Desired Result:

/*
1
22
333
4444*/
for(integer i=1; i<=4;i++)
{
    //key is the value of i which allows step-wise iterations; as I increases j can execute successively more times.
    for (integer j=1; j<=i;j++)
    {
       //by printing the value of i not j, you get the value of i iterated progressively with each inner loop
        System.debug (i);
    }
 
}

 

I J Prints I J Prints I J Prints I J Prints
I=1 J=1 I= 1 I=2 J=1 I=2 I=3 J=1 I=3 I=4 J=1 I=4
  J=2>I     J=2 I=2   J=2 I=3   J=2 I=4
        J=3>I     J=3 I=3   J=3 I=4
              J=4>I     J=4 I=4
                    J=5>I  

 Example 3

/*
1
12
123
1234*/
for(integer i=1; i<=4;i++)
{
    //key is the value of i which allows step-wise iterations; as I increases j can execute successively more times.
    for (integer j=1; j<=i; j++)
    {
       //by printing the value of J not I, you get the value of J iterated progressively with each inner loop
        System.debug (J);
    }
 
}

I J Prints I J Prints I J Prints I J Prints
I=1 J=1 J= 1 I=2 J=1 J=1 I=3 J=1 J=1 I=4 J=1 J=1
  J=2>I     J=2 J=2   J=2 J=2   J=2 J=2
        J=3>I     J=3 J=3   J=3 J=3
              J=4>I     J=4 J=4
                    J=5>I  

 

 Loop Assignments

Q1

List <integer> Q1=new list<integer> {1,4,7,10,13,16};
    //method is mylist.size
    //-1 allows you display all the variables because index starts at zero
for(integer i=0;i<=Q1.size()-1;i++)
{
    System.debug(Q1[i]);
}

OR

for (integer i=1;i<=16;i=i+3)
{
    System.debug (i);
}

Q2

List <integer> Q2=new list<integer> {20,15,10,5};
    //method is mylist.size
    //-1 allows you display all the variables because index starts at zero
for(integer i=0;i<=Q2.size()-1;i++)
{
    System.debug(Q2[i]);
}

 

OR

//for decreasing values, iterate to the lowest value to close the loop

for (integer i=20;i>=5;i=i-5)
{
    System.debug (i);
}

 Q3

 for (integer i=2;i<=10;i=i+2)
{
    System.debug (i);
}