# Comp 101 For Loops

## For Loops

For loops are much more powerful than while loops. Both kinds of loops allow us to repeatedly step through a piece of code, but for loops allow us to do this with more control. When writing a while loop, do you often forget the i++ at the end, which can leads to infinite loops and the white screen of death? However, have no fear, for loops are here!

via GIPHY

## What does a for loop look like?

A for loop has the for keyword and then 3 separate parts within the parenthesis that give the loop requirements on where we want to start looping, and when we want to stop and how to increment our counter variable.

for (1. declare a counter variable and initialize to a start value; 2. Loop until this condition becomes false; increment/decrement the counter variable) {

...}

Very often, we use for loops to traverse through arrays. For loops are most powerful when we know exactly how many times we want to loop. Although we may not know the exact number of elements in any given array of the top of our heads, we can use <array>.length as our upper loop bound.

In code, the above pseudocode for array traversal looks like this...

```for (let i = 0; i < array.length; i++) {
...
}
```

*** This may seem strange at first, but the more practice you get writing for loops, the more natural they become and you will see the advantage they have over while loops. ***

NOTICE: This loop will continue looping until part 2 (i < array.length) becomes false! Also, the condition in part 2 will change depending on what we want this for loop to do!

## Let's look at an example...

Let's loop through a simple array of numbers and print out all the numbers greater than 10.

```let array: number[] = [2, 6, 77, 85, 34, 2, 1, 0];
for (let i: number = 0; i < array.length; i++) {
if (array[i] > 10) {
print(array[i]);
}
}
```

NOTICE: The variable i is an arbitrary counter variable. I could have named this variable "SB2019" and it would've held the same functionality. Programmers will almost always use i or j as convention. This is especially helpful when working through arrays as we can think of as standing for the index of the array.

ALSO NOTICE: We initialized i to be 0 so that we start first index in the array, array[0]. Then we looping until one less the length of the array, 8. We do this because the last index of the array is array[7], if we went all the way to 8 we'd be outside the bounds of the array. This setup allows us to loop through the entire array without going over! I'd familiarize with yourself with this logic, because it will prove itself very useful throughout the course!

## Why use a for loop over a while loop?

Now you know how for loops work, why are they more advantageous than while loops in certain situations?

For loops allow us to keep better track of exactly where we are in the looping cycle, as the looping conditions are all helpfully together in the for loop parentheses. Additionally, by initializing and incrementing the counter even before we put any code into our loop, we're minimizing the chances of accidentally creating an infinite loop!

"So why have while loops at all?" you may ask.

Imagine we wrote a very simple loop to remind us to do our 101 homework while class is in sessions. Since it's Carolina, and we may have a hurricane, water crisis and snow all in the same week, we don't know exactly how many days we'll be in class at the beginning of the semester, so instead of setting up a number variable to keep track of the number of days of class, we could just use a boolean variable called inClass, which we could set to false whenever we wanted.

Therefore, the condition for the homework reminder would be while inClass is true, which we couldn't represent in a for loop as our condition is a boolean variable rather than an arithmetic comparison.

Therefore, we'd use a while loop looking a little something like this:

```let inClass = true;
// the loop below print as long as inClass is true, and would stop if
// there was a water crisis/hurricane/snow etc..
while (inClass) {
print("Wow! I can't wait to get started on the next 101 problem set!");
if (waterCrisis || hurricane || snow) {
inClass = false;
}
} ```

via GIPHY