3.0 Controlling Program Flow
Control structures are the logical backbone of any program. They allow a sketch to make decisions, execute different blocks of code based on specific conditions, and perform repetitive tasks efficiently. By moving beyond simple linear execution, these structures enable the creation of dynamic and responsive applications that can react to changing inputs and events.
3.1 Conditional Statements
Decision-making structures evaluate one or more conditions to determine which statements to execute, guiding the program along different paths.
- if statement: The most basic control structure. It executes a block of code only if the expression contained within its parentheses evaluates to true.
- if…else statement: This structure provides an alternative path for the program. If the if expression is false, the block of code within the else statement is executed instead.
- if…else if…else statement: This is a powerful method for testing multiple, mutually exclusive conditions. The program evaluates each if and else if condition in sequence until one is found to be true. If none are true, the final else block is executed.
- switch…case statement: Similar to an if…else if chain, the switch…case statement allows you to control program flow based on the various possible values of a single variable or expression. It is often more readable when dealing with many discrete conditions.
- Conditional Operator (? :): This is the only ternary operator in C/C++. It provides a compact, one-line syntax for a simple if…else statement. It takes the form condition ? value_if_true : value_if_false.
3.2 Loops
A loop is a control structure that allows a statement or group of statements to be executed multiple times, which is essential for tasks that require repetition, such as reading sensor data or animating LEDs.
- while loop: This loop executes a block of code continuously as long as the expression in its parentheses remains true. The variable being tested must be changed within the loop; otherwise, the program will be stuck in an infinite loop.
- do…while loop: This structure is similar to the while loop, with a key difference: the condition is tested at the end of the loop body. This guarantees that the statements inside the loop will be executed at least once, even if the condition is initially false.
- for loop: This loop is designed to execute a block of statements a predetermined number of times. Its concise syntax handles the initialization of a counter, the testing of a condition, and the manipulation (e.g., incrementing) of the counter, all within the parentheses.
- Nested Loops: It is possible to use one loop inside another. This is useful for tasks that involve iterating over multi-dimensional data, such as a grid or a matrix.
- Infinite Loops: An infinite loop is one that has no terminating condition, causing it to run forever. While sometimes useful (as the main loop() function is itself an infinite loop), they are often the result of a logical error in the code.
Once you can control the flow of your program, the next step is to organize your code into more manageable and reusable pieces.