Statements

There are two types of statements: selection statements and iteration statements. We cover two of each type here:

if- else if -else

switch case

for loop

while loop

 

Selection Statements

if- else if -else

if( condition here is true ){ run all of these code statements } else{ run these statements}

An “if” statement creates a branch within your program so that your code 'makes decisions'. It tests a condition and, if that condition is true, some code statements will run. If the condition is false, the 'if' statement is passed over.

 

You can create an “if” statement by itself, or you can use it along with other “if” statements, “else if” statements, and “else” statements. When you use an “else if” statement, its condition is checked only when the “if” statement before it is false. When you use an “else” statement, it will always run when the “if” statement right before it is false. You cannot use “else if” and “else” without first using “if”.

 

switch case

switch (var){
case label:
// statements

break;
case label:
// statements

break;
default: (optional)
// statements
}

 

The switch case selection statement looks at the variable inside the parentheses and then picks a case to run based on the value. This is a useful tool when your variable will have one of a fixed set of values and you want to do something different based on each value. Instead of using many different if- else if statements together, the switch doesn't test each condition, but jumps directly to its match and runs the code. If you don't include a break; statement after each case, the switch case statement will run every case until it hits a break or finishes the statements.


An example using switch to decide which pin is high based on the number of button presses:

switch(buttonPresses){

case 1:
digitalWrite(12,HIGH);
break;
case 2:
digitalWrite(10,HIGH);
break;
case 3:
digitalWrite(9,HIGH);
break;
//many more case statements can go in here...
default:
digitalWrite(2,HIGH);

 

Iteration Statements

for loop

for(create_and_declare_variable ; condition_based_on_that_variable ; modify_variable_here_until_condition_is_false){ do this until the second statement in the for loop is false }

The “for” loop completes a task a set number of times, then completes and exits. The first part of the loop is the initializer. Usually, that means you create a variable called “i” that will be thrown away after the “for” loop is complete. The next part of the loop is the condition, where you determine how long you want the “for” loop to run. This is usually a comparator between your variable and a number. The last part of the “for” loop creation is the way you want the variable to be modified each time the “for” loop runs. Most of the time, you want the variable to count up by one (i++) or count down by one (i –) each time the “for” loop iterates. The code between the curly braces is what will run while the “for” loop is iterating. Here's an example:

for( int i = 0 ; i < 200 ; i ++){
analogWrite( 9, i );
delay(100);
}

Your “for” loop will analogWrite() a new value to pin 9 every 100 milliseconds for 200 iterations. Then the “for” loop is complete!

while loop

while( this_condition_is_true ){ run_this_code}

A “while” loop iterates until the condition between the parentheses is false. This statement is most useful when you're not sure what values will be in the condition. You can create a “while” loop to run forever if you want. Here's an example. Assume you have an LED on pin 3 that has its brightness set by a random value variable (brightness) between 1 and 255. No matter what the brightness is, you want to fade the LED down to 0 brightness. Here's how:

while ( brightness > 0){
analogWrite(3, (brightness -1));
delay(100);
brightness - - ;
}

Now no matter what the brightness was when the “while” loop started, it will decrease by one each time the “while” loop runs until brightness isn't greater than 0.