Logo If statement tutorial

You should inspect your programming project and see if you at some point need a condition to be true or false. If you do, then you need an if statement. This is how you do it (the code below is not real code, it's what is known as pseudo code and is used to visualize how code looks or should look, so if you use this code, it won't work as expected):

JavaScript

if (condition) { //Do something }

Pretty simple, right? If translated to plain English, it would sound something like this:
if the condition inside the parentheses is true, do whatever is inside the curly brackets.

Still pretty simple. But let's look at some real code. let's create a very simple dice simulation. We'll start simple and build upon what we've already built.

there are far better ways to create a dice roll simulation, but this tutorial is about if statements, so while the following will work, just know there are better ways.

A roll of a dice results in a random number between 1 and 6, so we need something to generate a random value. A built in object, Math, can help us out. Specifically the function Math.random() will generate a random decimal number between 0 and 1.
Let's create a 50/50 condition with Math.random() like this: Math.random() < 0.5 and put it in the if statement:

JavaScript

if (Math.random() < 0.5) { alert("There is a 50% chance this will display!"); }

We can't quite call it a dice yet, so let's continue to built more functionality to this.

I want to introduce the else statement, which is basically just a way to say: if the statement is not true, do this instead:

JavaScript

if (Math.random() < 0.5) { alert("There is a 50% chance this will display!"); } else { alert("Otherwise this will display!"); }

Let's also multiply the random value by 6, so it resembles a dices values. That way we can check for less than 1, less than 2, less than 3, etc. like so:

JavaScript

if (Math.random() * 6 < 1) { alert("There is a 1/6th chance this will display!"); } else { alert("Otherwise this will display!"); }

Now we have a chance to roll 1 on the dice, but what about the rest of the numbers on the dice?

It's time to introduce the else if statement, but before we dive into that, we should put the random value in a variable:

JavaScript

let diceRoll = Math.random() * 6; if (diceRoll < 1) { alert("There is a 1/6th chance this will display!"); } else { alert("Otherwise this will display!"); }

Great! Let's now look at this else if thing. Do this:

JavaScript

let diceRoll = Math.random() * 6; if (diceRoll < 1) { alert("We've rolled 1 on the dice!"); } else if (diceRoll < 2) { alert("We've rolled 2 on the dice!"); } else if (diceRoll < 3) { alert("We've rolled 3 on the dice!"); } else if (diceRoll < 4) { alert("We've rolled 4 on the dice!"); } else if (diceRoll < 5) { alert("We've rolled 5 on the dice!"); } else if (diceRoll < 6) { alert("We've rolled 6 on the dice!"); }

Basically, the else if statement is just a way to say: if the first condition is not true, then check the next condition. If this next condition is not true, then check the next condition. And so on, and so on. You can have as many else if's as you want.

Hope you enjoyed this tutorial and gained some new knowledge, cheers...