IF Statement Worksheet

Question 1

Why is an IF statement known as a control structure?

An IF statement is called a control structure because it controls the flow of a program by deciding which code should run based on certain conditions.

Question 2

There are other control structures in JavaScript that we'll be learning about soon. Use your google skills to look up the other control structures in JavaScript and list them below.

Other control structures in JavaScript include: switch statements, for loops, while loops, do...while loops, and try...catch statements.

Question 3

What is a boolean expression?

A boolean expression is a statement that can only evaluate to either true or false.

Question 4

What is the 'equality operator', and what is it used for? How is it different from the assignment operator?

The equality operator (== or ===) is used to compare two values to see if they are equal. The assignment operator (=) is used to assign a value to a variable. They perform completely different tasks.

Question 5

Why is it important to properly indent your code when writing IF statements?

Proper indentation makes your code easier to read, understand, and debug. It helps show which statements belong to which blocks of code.

Question 6

What is a code block in JavaScript?

A code block is a group of statements enclosed in curly braces { } that are executed together.

Question 7

What is a code block in JavaScript?


		let input = prompt("Are you a vegetarian (y/n)?");

		if(input == "y"){
			console.log("I recommend the pasta salad.");
		}else if(input == "n"){
			console.log("I recommend the prime rib");
		}
		
This code uses an IF/ELSE IF statement to check the user's input and recommend a meal based on their response. The code block contains the statements that run depending on the condition.

Coding Problems

Coding Problems - See the 'script' tag below this h3 tag. You will have to write some JavaScript code in it.

Always test your work! Check the console log to make sure there are no errors.