Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Control Flow: Difference between revisions

From Groundhog Learning
Updated via bot
Β 
(No difference)

Latest revision as of 15:54, 21 October 2025

The order in which a computer program runs its instructions.

Details

By default, a program runs statements from start to bottom.

Think of a program like following a cake recipe:

πŸ€– Example: Default Flow

preheat oven
mix ingredients
pour batter into pan
bake
decorate

By default, you follow the steps in order β€” this is the normal execution flow.



Sometimes, execution change depending on a condition:

If it’s a chocolate cake, add cocoa powder before baking.

πŸ€– Example: Conditional Flow

preheat oven
mix ingredients
if chocolate cake:
    add cocoa powder
pour batter into pan
bake
decorate

In programming, conditional statements select the statements to run based on a condition.



Or it might repeat one or more steps many times:

Stir the batter until smooth.

πŸ€– Example: Loop Flow

preheat oven
mix ingredients
if chocolate cake:
    add cocoa powder
while batter is lumpy:
    stir batter
pour batter into pan
bake
decorate

In programming, iteration statements repeat statements based on a condition.



And sometimes, we may want to jump out and skip to another part of the recipe:

Keep stirring the batter until smooth, but if it starts to overflow, stop immediately.

πŸ€– Example: Loop with Break

preheat oven
mix ingredients
if chocolate cake:
    add cocoa powder
while batter is lumpy:
    stir batter
    if batter is overflowing:
        stop stirring
pour batter into pan
bake
decorate

In programming, jump statements transfer the execution flow to a different line based on a condition.

Related Concepts

References