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 what happens in a program 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.
In JavaScript, control structures manage the flow of a program by determining which code blocks execute and how often. Beyond the if statement, key control structures include:
Conditional Statements: if...else: Executes code blocks based on whether a condition is true or false. else if: Checks additional conditions if previous ones are false.
switch: Selects code to execute from multiple options based on a variable's value
Loops:
for: Repeats code a specific number of times.
while: Continues executing code as long as a condition remains true.
do...while: Executes code once before checking the condition, then repeats if true.
for...in: Iterates over the enumerable properties of an object.
for...of: Iterates over iterable objects like arrays or strings.
Jump Statements:
break: Exits the current loop or switch statement.
continue: Skips the current iteration of a loop and proceeds to the next one.
return: Exits a function and optionally returns a value.
throw: Throws a user-defined exception.
Exception Handling:
try...catch...finally: Handles exceptions, allowing code to run safely by catching errors and executing cleanup code.
Conditional Statements: if...else: Executes code blocks based on whether a condition is true or false. else if: Checks additional conditions if previous ones are false.
switch: Selects code to execute from multiple options based on a variable's value
Loops:
for: Repeats code a specific number of times.
while: Continues executing code as long as a condition remains true.
do...while: Executes code once before checking the condition, then repeats if true.
for...in: Iterates over the enumerable properties of an object.
for...of: Iterates over iterable objects like arrays or strings.
Jump Statements:
break: Exits the current loop or switch statement.
continue: Skips the current iteration of a loop and proceeds to the next one.
return: Exits a function and optionally returns a value.
throw: Throws a user-defined exception.
Exception Handling:
try...catch...finally: Handles exceptions, allowing code to run safely by catching errors and executing cleanup code.
Question 3
What is a boolean expression?
A boolean expression is a part of code that checks if something is true or false. It helps the program make decisions based on whether a condition is met.
Simple Examples:
Comparisons:
5 > 3 (Is 5 greater than 3?) — This is true.
10 == 10 (Is 10 equal to 10?) — This is true.
Simple Examples:
Comparisons:
5 > 3 (Is 5 greater than 3?) — This is true.
10 == 10 (Is 10 equal to 10?) — This is 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 in JavaScript is ==. It’s used to compare two values to see if they are equal.
If they are the same, it returns true; if they are different, it returns false.
If they are the same, it returns true; if they are different, it returns false.
Question 5
Why is it important to properly indent your code when writing IF statements?
Indenting your code in IF statements is important because:
Makes Code Easier to See: Indenting helps show what belongs to the IF statement and what doesn’t, so you can understand the code better.
Shows the Code Blocks Clearly: With indentation, you can see which lines are part of the IF statement. This is useful if you have IF statements inside other IF statements.
Helps Avoid Mistakes: Indented code makes it easier to spot errors, like a line that’s not in the right place.
Good Practice: Most programmers indent their code, so others will expect it. This makes it easier for others to read and work with your code.
Makes Code Easier to See: Indenting helps show what belongs to the IF statement and what doesn’t, so you can understand the code better.
Shows the Code Blocks Clearly: With indentation, you can see which lines are part of the IF statement. This is useful if you have IF statements inside other IF statements.
Helps Avoid Mistakes: Indented code makes it easier to spot errors, like a line that’s not in the right place.
Good Practice: Most programmers indent their code, so others will expect it. This makes it easier for others to read and work with your code.
Question 6
What is a code block in JavaScript?
A code block in JavaScript is a group of commands put inside curly braces { }.
It makes JavaScript treat everything inside as one part. Code blocks are used in IF statements,
loops, and functions to tell JavaScript what commands to run for that section.
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.