IF Statement Worksheet

Question 1

Why is an IF statement known as a control structure?

It is a way of controlling which code executed when the program is run.

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.

There are 3 categories of control structures in JavaScript. I will create a list with the 3 main categories and then the different types that make up that category.
  1. conditionals
    • if-else
    • switch
  2. loops
    • while
    • do-while
    • for
    • for-in
    • for-of
  3. jumps
    • break
    • continue
    • labeled statement
This information was obtained here. With a name like "International JavaScript Institute" it seemed reliable.

Question 3

What is a boolean expression?

An expression that results in true or false. For instance 5 > 4 would be true and 5 < 4 would be 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' is ==

The 'equality operator' is a boolean expression used to determine if two values are the same or different. Example: someInputOrVariable == someOtherVariableOrSetValue

A brief example of 'assignment operator' used instead of 'equality operator':
variableA = variableB

An 'assignment operator' reassigns the second value to the first variable. So it would reassign the value of variableB to be the vlaue of variableA instead of compating them.

Question 5

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

To help keep your code readable for when the inevitable debug session happens. IF statements can be a lot of lines of really short code that without proper indentation can blend together and makes it easy to lose track of where you are/what section you are in.

Question 6

What is a code block in JavaScript?

The code inside of the { }. Can also be refered to as code branches.

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.