Comp 101 While Loops

While Loops

A while loop is a statement that allows us to do some functionality over and over again. When you're listening to music on iTunes there's a little button that allows you to play a song on repeat: this is a loop.

Loops can go on for forever...

via GIPHY

...if we don't tell them to stop. 

What do loops look like?

while (...) {
    <do something>
}

This is the same thing as saying, while whatever is in the parenthesis is true, do something over and over. The loop will start at the opening bracket and end at its corresponding closing bracket. 

Can we stop a loop?

The answer is yes. In fact, you always want to provide a condition that will allow our loop to stop. In the coding world we almost never want something to continue forever and ever and ever and ever and ever and ever and ever and ever...see my point?

When we write a while loop we have to provide a condition within the parenthesis that evaluates to true or false (just like when we right if-then statements). If the condition evaluates to true then we will go inside the while loop and run whatever is inside the brackets until our condition evaluates to false

If our condition evaluates to false, we will NOT enter the while loop and instead we will jump to the while loop's closing bracket and the rest of the program will keep running normally. 

Let's look at some examples...

In the movie Forest Gump, Forest keeps running until everyone screams stop. Let's do this in code!

If you've never seen Forest Gump, take a study break and go watch it. Kris will understand. 
let run: boolean = true;
while (run === true) {
    print("Run Forest Run!");
    run = await promptBoolean("Should Forest keep running?");
}
print("Stop Forest Stop!")

In the above code "Run Forest Run" will print and we will be prompted for a boolean over and over again until we respond with false which will make us exit the loop and print "Stop Forest Stop".


According to the code below, how many times will run be printed?

let num: number = 5;
while (num < 25) {
    print("Run ");
    num = num + 5;
    print("Forest ")
}
num = 20;
print("Stop Forest!");


via GIPHY

Note: If you're ever writing while loops and you get a "stack overflow" error it's probably because you have an infinite loop!!!
Answer: Run will be printed times.

Why is my loop not working?

Usually, this comes in the form of a blank screen, then after while the page will crash - but don't worry! This is common mistake that is very easily fixed!

//incorrect:
let i = 0;
while (i < 101) {
     print("I love COMP101");
}
print("Done");
//the number 'i' will never be >= to 101,
// therefore the while loop will INFINITELY execute
//the string "Done" will never print

//correct:
let i = 0;
while (i < 101) {
     print("I love COMP101");
     i++; 
     // is equivalent to i = i + 1;
}
print("Done");
//i increments up by 1 each time the loop executes
//when the code has executed 110 times 
//(or is equal to 109),
// the while loop will end and "Done" will be printed

Loops and conditionals

The body of the loop doesn't just have to consist of basic assignment statements, we can have all sorts of code in our loops, including conditionals!

let i = 0
while (i < 10) {
    if (i < 5) {
      print("Keep Running Forest!");
    } else {    
     print("slow Down Forest!");
    }   i++;
}
/* this would print:
    Keep Running Forest!
    Keep Running Forest!
    Keep Running Forest!
    Keep Running Forest!
    Keep Running Forest!
    slow Down Forest!
    slow Down Forest!
    slow Down Forest!
    slow Down Forest!
*/