Skip to main content

Lesson 5.2: Combine State, Decisions, Loops, and Functions

Each programming tool has a different job.

  • Variables store changing state.
  • Conditions choose which branch runs.
  • Loops repeat work.
  • Functions name and organize work.
  • Lists keep related values in order.

A strong program uses the smallest tool that fits each part. A single choice needs an if, not a loop. Known repeated work needs a loop, not copied blocks. A value needed by later commands belongs in a variable or returned result.

Trace combined programs one layer at a time. First follow the main sequence. At a function call, trace that function. At a loop, record the repeat number. At a condition, record its Boolean result.

Guided practice

Store [2, 6, 9] in a list named amounts. Create a function that accepts one amount. If the amount is at least 5, print keep; otherwise print check.

Loop through the list and call the function for each item. Add a variable that counts how many items were processed, then print the final count.

Before running, predict the three decisions and the final count. Use Step to locate the first difference if a result is wrong.

Loading the block workspace...

Ready to move on?

Only mark complete if you genuinely understand the material. Your progress will be saved in this browser.

Stuck on this lesson?

Ask about anything on this page. It can see which lesson you have open and which part you are reading.