IF Statement Worksheet
Question 1
Why is an IF statement known as a control structure?
An IF statement is known as a control structure because it controls the flow of a program. It decides which code runs based on whether a condition is true or false.
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 JavaScript control structures include:
• if / else if / else statements
• switch statements
• for loops
• while loops
• do...while loops
• try / catch (for error handling)
• if / else if / else statements
• switch statements
• for loops
• while loops
• do...while loops
• try / catch (for error handling)
Question 3
What is a boolean expression?
A boolean expression is a statement that evaluates to either true or false. Examples include comparisons like
5 > 3 or userAge >= 18.
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.
Example:
x = 5 assigns 5 to x, while x == 5 checks if x equals 5.
Question 5
Why is it important to properly indent your code when writing IF statements?
Proper indentation makes code easier to read, understand, and debug. It helps clearly show which statements belong inside an IF block.
Question 6
What is a code block in JavaScript?
A code block is a group of statements enclosed in curly braces
{ }.
It defines where an IF, loop, or function’s statements start and end.
Question 7
Explain what the following code does.
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 asks the user whether they are a vegetarian.
If the user types “y”, it recommends the pasta salad.
If the user types “n”, it recommends the prime rib.
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.