Control Structures: If Statements, Loops, and Logic
So far your programs have run straight through, top to bottom, doing the same thing every time. This lesson gives them the ability to make decisions and to repeat work on purpose. You will learn conditional logic with if and else, combine conditions with logical operators, and use for and while loops to repeat code without copying and pasting. By the end you will build a working light-activated nightlight and a blink animation driven by a loop.
What a control structure actually is
A control structure is code that lets your program decide what to do instead of always doing the same thing. The everyday version is an if statement: if something is true, do this; otherwise, do that. That is the whole idea. You already use this logic constantly. At a stoplight, if the light is red you stop, and if the light is green you go. A program reasons the same way. If a light sensor reports that the room is dark, turn a lamp on; otherwise, leave it off. That single sentence is a complete piece of conditional logic, and it is the basis of almost every interactive program you will ever write.
if, else if, and else
An if statement checks a condition inside parentheses. If that condition is true, the code in the following braces runs. An else block runs only when the if condition was false, so the program never runs both. When you have more than two possible cases, you chain them with else if: the program checks each condition in order and runs the first one that is true, then stops checking. A key detail to hold onto is that else is only reached when the if above it is false. If the if is true, the program executes its block and skips the rest of the chain entirely.
Comparing values and logical operators
Conditions are built from comparisons such as greater than, less than, equal to (written as double equals, ==), and not equal to (written !=). Often a single comparison is not enough. Consider going through a green light: you must be at the light AND the light must be green. Two separate things both need to be true. The AND operator, written &&, returns true only when both sides are true. The OR operator, written ||, returns true when at least one side is true. Using OR here would be wrong, because being at the light OR the light being green would send you through the intersection even when the light is red. The NOT operator, written !, flips a truth value, which is why != means not equal.
A shortcut for boolean conditions
When a variable is already a boolean, meaning it holds true or false, you do not need to compare it to true. Writing if (isAtLight == true) works, but if (isAtLight) means exactly the same thing and is cleaner. To require that the light is green as well, you combine them: if (isAtLight && lightColor == GREEN). Everything inside the parentheses of an AND must be true for the whole condition to be true. If any part is false, the entire condition is false and the block is skipped.
Loops: repeating code on purpose
A loop repeats a block of code multiple times so you do not have to write it out again and again. There are two workhorse loops. A for loop is the right choice when you know exactly how many times to repeat, for example printing the numbers 0 through 9. A while loop is the right choice when you do not know the count in advance and instead want to repeat until some condition becomes false, for example while a button is held down. A third form, the do while loop, is like a while loop but always runs its body at least once before checking the condition; it is less common but useful to recognize.
How a for loop runs, step by step
A for loop has three parts inside its parentheses, separated by semicolons. The first part sets up a counter variable, such as int i = 0, and runs once before the loop starts. The second part is the condition, such as i < 10, and is checked before every pass; the loop continues only while it is true. The third part updates the counter after each pass, such as i++, which is shorthand for i = i + 1. So the loop runs the body, adds one to i, checks whether i is still less than 10, and repeats. When i reaches 10, the condition i < 10 is false, so the loop stops without running an eleventh time. That is why a loop counting from 0 with the condition i < 10 prints 0 through 9, ten values in total.
Why you must set pinMode for every pin
Before using a pin you tell the board whether that pin will read (INPUT) or write (OUTPUT), using pinMode inside setup. This is not busywork. A microcontroller pin can only do one job at a time: reading and writing are different electrical functions, and the hardware has to be configured for the one you need before your program uses it. The compiler needs to prepare the pin ahead of time, which is why every pin you touch gets a pinMode line in setup.