Adventures in Science: Arduino Conditional Statements

We're back to exploring programming basics, and this week: if/else statements!

A few weeks ago, we talked about arithmetic operators in Arduino/C. We're back with another riveting round of programming basics, and now we're talking about conditional statements. They're super important in most programming languages, as they allow you to change how the program flows.

To use conditional statements (and later, loops), we first look at relational operators, which, in their basic form, compare two numbers. For example, does 5 equal 7? Here are the six basic relational operators in C:

Operator Description
== Is equals?
!= Is not equals?
> Greater than?
< Less than?
>= Greater than or equal to?
<= Less than or equal to?

In C, these operators return an integer: 1 for true and 0 for false. We can feed these into the conditional statements in order to change the flow of the program:

if ( some relational operation ) {
    if above is true, execute this
} else if ( another relational operation ) {
    if first relational operation is false but second is true, execute this
} else {
    otherwise (both are false), execute this
}

While this is old news for many of you, how would you recommend wrapping these programming basics videos up for someone who might be new to it? Would you prefer to see it in a playlist? As part of a class with homework problems and answers? I'm trying to figure out how to package these into something that could be useful for a beginner, so ideas are welcome! Please share in the comments below.