If-Else Statements

On a previous page we looked at comparison and logical operators, which return a boolean value (a true/false value). We can use these true/false values to dictate what our programs do.

Suppose we have an age variable that contains the age of a person, and we want to print "You can drive!" if the person is at least 16 years old, and print "Sorry, you can't drive yet." otherwise. To do this we use a conditional statement, also referred to as an if-else statement. The code below shows this.

var age = 15;

if (age >= 16) {
	print("You can drive!");
} else {
	print("Sorry, you can't drive yet.");
}

In the code above, try changing the value of age to 18 and running the code! In this code, we used the if statement. The if statement takes a condition within parentheses (such as age >= 16) and then executes the following code block if that condition is true. If the condition is false, then the code within the else block will run. Like functions, we use curly braces, { and }, to indicate the blocks of code we want to run.


Let's look at another example of using if-else statements. Let's define a function named greaterThanTen that will look at a number, and tell us if it's greater than 10 or not. Check out the code below.

function greaterThanTen(x) {
	if (x > 10) {
		print(`${x} is greater than 10.`);
	} else {
		print(`${x} is NOT greater than 10.`);
	}
	return;
}

greaterThanTen(8);
greaterThanTen(12);

The "Switch" Statement

We can use multiple if/else statements, but this can get messy. Another option is to use a switch statement, which checks a value and then runs a block of code based on that value. This will make the most sense with an example. Suppose we want to take the name of a coin, and then print out the value of that coin based on its name. The code below shows this.

var coinName = "nickel";

switch (coinName) {
	case "penny":
		print("0.01");
		break;
	case "nickel":
		print("0.05");
		break;
	case "dime":
		print("0.10");
		break;
	case "quarter":
		print("0.25");
		break;
}

In the code above, change coinName to "quarter" and then run the code. In this switch statement, we use the syntax switch (coinName) to indicate that our switch statement will look at the coinName variable and use that value to dictate what to do. Each "case" is indicated with the case keyword. When you land in one of these cases, the program will run all code after that until it hits a break keyword, so we generally add break to the end of each of our cases.

In the code above, change coinName to "nickle" (notice the spelling!). The switch statement doesn't recognize this value of coinName so it won't execute anything! Notice that the output is empty. To handle the "leftovers" we use a default case, which will catch anything that doesn't fall into the other cases. Let's adjust the previous code to use a default case.

var coinName = "nickle";

switch (coinName) {
	case "penny":
		print("0.01");
		break;
	case "nickel":
		print("0.05");
		break;
	case "dime":
		print("0.10");
		break;
	case "quarter":
		print("0.25");
		break;
	default:
		print("I don't recognize that coin name.");
		break;
}

In the code above, the default case will pick up anything that isn't "penny", "nickel", "dime", or "quarter".

Activities

Activity: Height for a Ride

Suppose a ride at an amusement park has a height requirement. To ride the ride, you must be at least 36 inches tall. Use the editor below to do the following:

  1. Define a variable height that contains a number that represents a person's height in inches.
  2. If the person is tall enough to ride the ride, print "Hop on!".
  3. If the person is not tall enough to ride the ride, print "Sorry, you aren't tall enough.".
print("Your code here.");

Activity: Printing the Day of the Week

JavaScript can tell what time/day it is. In the code below, the first line is getting the day of the week in number form, where 0 is Sunday, 1 is Monday, 2 is Tuesday, and so on. Use the code editor below to print the name of the day of the week based on the day number n. That is, if n = 1 you should print "Monday".

Once you've done this, change the value of n to various values between 0 and 6 and make sure it works as expected.

var n = new Date().getDay();

print(n);
print("Your code here.");