Control Flow & Functions
So last week, your programs were pretty straightforward — they just ran from the first line to the last line, one after the other. Every single instruction got executed exactly once. But think about it... real programs do not work that way, right? They need to make choices — like "if this user is old enough, let them in, otherwise show an error." And they need to repeat things — like "keep asking for a password until they get it right."
That is what this week is all about. We are learning two of the most powerful ideas in programming: conditional statements (making decisions) and loops (repeating things). On top of that, we will get into functions — which are basically reusable chunks of code that you give a name to. Instead of copy-pasting the same code everywhere, you write it once, put it in a function, and just call it whenever you need it. We will even touch on recursion — which is when a function calls itself. Sounds wild, but it is actually a really elegant way to solve certain problems.
| Concept | What It Does | Real-World Analogy |
|---|---|---|
if/else | Makes decisions based on conditions | A traffic light — go if green, stop if red |
for loop | Repeats code a known number of times | Counting laps around a track |
while loop | Repeats code until a condition becomes false | Stirring soup until it boils |
| Function | A reusable block of code with a name | A recipe you can follow whenever you need it |
| Recursion | A function that calls itself | Russian nesting dolls — each doll contains a smaller version of itself |