Skip to content

Latest commit

 

History

History
120 lines (79 loc) · 3.68 KB

6. flowcontrol.md

File metadata and controls

120 lines (79 loc) · 3.68 KB

Flow Control

Earlier, you were told that you could alter the "flow" of a program: Here's how.

If Statements

Say we wanted to do something if the user had run our program for the first time. Our code might look like this:

if (firstTime) {
    // ...Do stuff if firstTime is true
}

//...Rest of the program

The if allows us to choose whether or not to run the code in the {} that follow afterwards.

The value inside the () should be a boolean. If it's true, then the code inside the {} will run, otherwise, it won't.

There are also else statements, which run code if an if statement doesn't run.

if (firstTime) {
    // ...Do stuff if firstTime is true
}
else {
    // ...Do stuff if firstTime is false
}

//...Rest of the program

Finally, there are also else if statements, which are just an if statement that runs if another if statement doesn't.

if (firstTime) {
    // ...Do stuff if firstTime is true
}
else if (secondTime) {
    // ...Do stuff if firstTime is false
    // and secondTime is true
}
else {
    // ...Do stuff if firstTime and
    // secondTime are false
}

//...Rest of the program

For Loop

The for loop can do 2 things in JavaScript.

The first is that we can iterate over all the items in an array and run a piece of code for each item.

The second is that we can iterate over a sequence of numbers. This doesn't sound to useful, but it is.

Here's an example of the first kind of for loop:

let names = ["Andrew", "John", "James"];

for (let name of names) {
    // ...Do stuff with the name.
}

Whatever code is inside the {} will be run for each item inside the names variable. We can access that item by using the name variable.

Here's an example of the second kind of for loop:

let names = ["Andrew", "John", "James"];

for (let i = 0; i < names.length; i++) {
    let name = names[i];
    // ...Do stuff with i and name.
}

This loop has a lot more things going on in it.

Let's start with the code inside the () (let i = 0; i < names.length; i++)

This is actually comprised of 3 statements.

The first one is the "initialisation" statement. This is a single "line" of code that is run before the for loop starts.

The second statement is the "condition". This is a line of code that produces a true or false value. This one is checking whether i is less than the length of names. As long as this statement is true, the for loop will keep going.

The third statement runs at then end of each iteration of the for loop. In this case, it increases i by 1.

You'll probably have noticed that this for loop does the same thing as the first one, but in a much longer way. The reason why we use this second type of for loop at all is that sometimes, we need to know the index of each item, as well as the item itself. In this second for loop, we can see that the index is i.

While Loops

While loops generally aren't used as much as for loops.

Here's an example while loop:

let names = ["Andrew", "John", "James"];

let i = 0;
while (i < names.length) {
    let name = names[i];

    // ...Do stuff with i and name

    i++;
}

And you thought that the for loop was confusing.

Inside the () in the while loop is a single statement. This the "condition". Much like the condition in a for loop, it provides either a true or false value. As long as the value is true, the while loop will keep running.

The while loop generally isn't used like I've shown you. (Looping over a list.) A (good) use case for a while loop might be in a guessing game, where you keep running the same code (asking the user a question) until they get the right answer.