Looping is a super powerful tool inside our bag of magical programming tricks. Loops not only greatly reduce redundancy in our code, but can also facilitate the processing of data stored in arrays. By using a loop, we are able to apply the same process to all the elements of an array.

Loops commonly make use of a *counter variable* of some sort that gets incremented with each iteration. This is super convenient when working with arrays! Why? Because the only way to access individual elements stored in array is to access the data at specific *indices*. A counter variable getting incremented with each pass through a loop is a perfect means of traversing through the array at different indices.

Let's check out some examples of what this might look like:

1. Printing out elements of an array.

let i: number = 0; let arr: number[] = [1, 2, 3, 4, 5, 6]; while (i < arr.length) { print(arr[i]); i++; }

This will result in *1, 2, 3, 4, 5, 6* being printed. As long as the counter variable *i* is less than the lengh of the array, the value of *arr* at the index specified by *i* will be printed. What if we wanted to print every other element of the array?

You could do something like this:

let i: number = 0; let arr: number[] = [1, 2, 3, 4, 5, 6]; while (i < arr.length) { print(arr[i]); i = i + 2; }

This will only print the *odd* numbers in this array. (Note that they are at *even* indices, however).

This would print the *even* numbers in the array.

let i: number = 0; let arr: number[] = [1, 2, 3, 4, 5, 6]; while (i < arr.length) { print(arr[i + 1]); i = i + 2; }

2. Modifying elements in an array.

let i: number = 0; let arr: number[] = [1, 2, 3, 4, 5, 6]; while (i < arr.length) { arr[i] = arr[i] + i; i++; } print(arr);

Here we are adding the current value of *i, *which corresponds to the current index of the array, to each element. Resulting output: *1,3,5,7,9,11.*

3. Finding the max of an array.

let max: number = 0; let i: number = 0; let arr: number[] = [1, 2, 3, 4, 5, 6]; while (i < arr.length) { if (arr[i] > max) { max = arr[i]; } i++; } print(max);

*6*, the maximum value in this array (6), will be printed out.

4. Finding the sum of an array.

let sum: number = 0; let i: number = 0; let arr: number[] = [1, 2, 3, 4, 5, 6]; while (i < arr.length) { sum = sum + arr[i]; i++; } print(sum);

Output: 21.

5. Concatenating elements of an array.

let result: string = ""; let i: number = 0; let arr: string[] = ["G", "D", "T", "B", "A", "T", "H"]; while (i < arr.length) { result = result + arr[i]; i++; } print(result);

Output: *GDTBATH.* Compare this concatenation algorithm to the previous sum algorithm!

6. Rotating items in an array.

let rotate = (arr: number[], n: number): number[] => { let i: number = 0; let temp: number = arr[0]; while (i < n) { while ( i < arr.length - 1 ) { arr[i] = arr[i + 1]; i++; } } arr[i] = temp; return arr; };

This function is a little more complicated than the examples we've looked at above - not only is this a function that processes arrays, but it also makes use of nested loops. The function takes in an array of numbers and a number *n* which specifies how many spaces to the left to shift each element in the array.

let arr: number[] = [1, 2, 3, 4, 5, 6]; print(rotate(arr, 0)); print(rotate(arr, 1)); print(rotate(arr, 2));

The above code snippet will print the following:

*1, 2, 3, 4, 5, 6*

*2, 3, 4, 5, 6, 1*

*3, 4, 5, 6, 1, 2*