1.1-Introductie-Programmeren

Theory Loops 2

Competence: I can use the enhanced for-loop to iterate over list elements.

Specific learning goals:

The assignments can be found in the separate modules.

Summary

If you want to perform the same operation on all elements in an ArrayList, a loop is essential. We show here two ways that are useful for an ArrayList.

An important property of the ArrayList is that it can grow and shrink during usage, however this means that if you want to create a loop this has to be based on the actual amount of elements in the list. This can be achieved with the while-loop and the enhanced for-loop.

The while loop uses an index to iterate over the elements in the list. By using this index items can be retrieved from the list based on their position. Using this index and the number of elements in the list, it is easy to create a condition that will properly end before the list runs out of elements.

The enhanced for-loop simplifies this process at the cost of a little freedom. Elements are automatically linked to some kind of predefined variable allowing for easy access, but the programmer no longer has access to the index (or position) or the element and is unable to manipulate the list (so adding / removing elements is not allowed). The enhanced for-loop therefore should only be used to view the contents of lists or perform other read-based operations (like searching).

While-loop

The while loop is the simplest way of iterating over any list. Using the counter as an index variable and by getting elements from certain positions, it is rather easy to navige through a list.

Example

Suppose we have an ArrayList with the variable name `names` that holds 2 elements. The code below prints all two elements in this list.

1. int index = 0;
2. while (index <names.size ()) {
3.      SaxionApp.printLine (names.get (index));
4.      index ++
5. }

Below is a brief summary of what is happening in this code:

line 1 initializes the variable index with 0.

Iteration 1:

  1. is 0 less than the size of the list? Yes.
  2. print element with index 0
  3. index becomes 1

Iteration 2:

  1. is 1 smaller than the size of the list? Yes.
  2. print element with index 1
  3. index becomes 2

Iteration 3:

  1. is 2 smaller than the size of the list? No

End of loop.

Enhanced for-loop

With the enhanced for loop, the entire list is run through without the programmer having to define the stop point. This comes at the cost of not knowing exactly where you are in the list (so the position is hidden) and you are unable to manipulate the list in any way (so no additions / removal of items is allowed).

The enhanced for-loop is designed to be a “simpler” way of accessing all elements in a list.

Example

We have another ArrayList with the variable name `names` and the list holds 2 elements. The code below prints all two elements in this list.

1. for (String singleName: names) {
2.    SaxionApp.printLine (singleName);
3. }

The for statement is structured as follows:

(You should read this line as for all singleNames in the list of names do..)

Below is a brief summary of what is happening in this code:

Iteration 1:

  1. take the first element from names and put it in the variable singleName
  2. print singleName

Iteration 2:

  1. take the second element from names and put it in the variable singleName
  2. print singleName

There is no further iteration as there were only two elements in the list!