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 the program.
It determines which code runs depending 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:
- else
- else if
- switch statements
- for loops
- while loops
- do...while loops
- try / catch (error handling)
Question 3
What is a boolean expression?
A boolean expression is any expression that evaluates to either true or false.
Examples: x > 10, age == 18, isOnline === true.
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 and determine if they are equal.
The assignment operator (=) is used to store a value in a variable.
Example: x = 5 assigns 5 to x.
x == 5 checks if x is equal to 5.
Example: x = 5 assigns 5 to x.
x == 5 checks if x is equal to 5.
Question 5
Why is it important to properly indent your code when writing IF statements?
Proper indentation makes IF statements easier to read and understand.
It helps you see which lines of code belong inside the IF, ELSE IF, or ELSE blocks.
Without indentation, it's easy to get confused and introduce errors.
Question 6
What is a code block in JavaScript?
A code block in JavaScript is a group of statements enclosed in curly braces { }.
Code blocks are used in IF statements, loops, and function bodies.
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 checks the user’s input and prints a recommendation based on a boolean expression.
If the input equals "y", the first code block runs.
If the input equals "n", the second block runs.
The IF and ELSE IF statements use code blocks to determine which message to output.
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.