Quiz 4: If Statements

Home

If Statement (Conditional Statement)

The if() statement is the most basic of all programming control structures. It allows you to make something happen or not, depending on whether a given condition is true or false. It looks like this:

if (someCondition) {
   // do stuff if the condition is true
}

There is a common optional variation called else that looks like this:

if (someCondition) {
   // do stuff if the condition is true
} else {
   // do stuff if the condition is false
}

There's also the optional else if, where you can check a second condition if the first is false:

if (someCondition) {
   // do stuff if the condition is true
} else if (anotherCondition) {
   // do stuff only if the first condition is false
   // and the second condition is true
}
else {
// do stuff if both conditions are false (optional)
}

The above description was adapted from this website by Mr. Stewart.

if (conditional) and ==, !=, <, > (comparison operators)

if, which is used in conjunction with a comparison operator, tests whether a certain condition has been reached, such as an input being above a certain number. The format for an if test is:

if (someVariable > 5) {
  // do something here
}

The program tests to see if someVariable is greater than 5. If it is, the program takes a particular action.  Put another way, if the condition in the parentheses is true, the statements inside the brackets (body of the if statement) are run. If not, the program skips over the body.

The expressions being evaluated inside the parentheses require the use of one or more comparison operators:

Comparison Operators:

 x == y (x is equal to y)
 x != y (x is not equal to y)
 x <  y (x is less than y)  
 x >  y (x is greater than y) 
 x <= y (x is less than or equal to y) 
 x >= y (x is greater than or equal to y)

WARNING: Comparison Operator vs Assignment Operator

1. An extremely common logic error is using the assignment operator = (assigning a value) when you mean to use the comparison operator == (comparing two values).

For example, incorrectly using the assignment operator:

if (pin = 10)  {

sets pin to 10 (puts the value 10 into the variable pin).  This will always evaluate to true because pin is now 10.  

2. Instead, use the comparison operator:

if (pin == 10)  {

to test whether pin is equal to 10 or not.


The above description was adapted from this website by Mr. Stewart.

Check Your Understanding

1. The if statement runs if the inside of the symbols (use the symbols) is true and executes the block in the symbols (use the symbols). 

2. The if statement can come with a(n) statement to run in case it is false.

3.To check if two values are equal in an if statement, use the symbol (use the symbol) , known in programming terminology as the .

4. To set the value of a variable, use the symbol , known in programming terminology in the .

5. The line of code to check if variable pin is less than or equal to variable lastPin is .  Format like: if (pin == 10)  {

6. The line of code to check if function output digitalRead(pin) is equal to value HIGH is Format like: if (pin == 10)  {