Concepts / Conditional Statements and Decision Making

Conditional Statements and Decision Making

Conditional statements are one of these patterns, but they become most useful when you understand how they fit with the other three.

  • Programming

Four Ways Programs Work

Every program is built from a small set of fundamental patterns. These patterns address four core programming needs: carrying out instructions in order, making decisions, repeating actions, and organizing code for reuse. Conditional statements are one of these patterns, but they become most useful when you understand how they fit with the other three.

Sequential codeinstructions in orderConditional codedecisionsRepetitive coderepeated blocksStore and reusefunctions
What is the difference between sequential code, conditional code, repetitive code, and store-and-reuse code?

Reading a Decision

Conditional code allows a program to make a decision based on whether a condition is true or false. In Python, this pattern is associated with if statements. Instead of following one uninterrupted path, the program can branch into different paths. The selected path depends on the condition being evaluated.

Conditiontrue or falseTrue actionselected pathContinue programFalse actionother pathtruefalsethenthen
How does a program choose between different actions when a condition is true or false?
Worked example

Choosing an Action

A program must choose between two actions depending on whether a condition is true or false.

  1. 1
    Evaluate

    The program examines the condition.

  2. 2
    Choose

    If the condition is true, the program follows the true path. If the condition is false, it follows the other path.

  3. 3
    Continue

    After the selected action, the program continues with the rest of its logic.

Result

The scenario uses conditional code because it selects between different paths based on a condition.

Tracing the Four Patterns

The four patterns solve different problems. Sequential code executes instructions in order. Conditional code makes a decision and branches into different paths depending on whether a condition is true or false. Repetitive code uses loops to repeat a block of instructions. Store and reuse packages code into functions so it can be called whenever needed.

PatternProgramming problemRecognizing feature
Sequential codeExecute instructions in orderOne instruction follows another
Conditional codeMake a decisionAn if statement chooses between paths
Repetitive codeRepeat a blockA loop executes instructions multiple times
Store and reuseAvoid duplicating codeA function packages code for later calls

The four patterns can be identified by the problem they solve and the way they control or organize code.

Looprepeat controlRepeated blockiteration 1FollowinginstructionsRepeated blocknext iterationrunsrepeatsthen
What changes as a loop repeats a block, and how does the repeated activity eventually move on?
Program flowneeds an operationFunctionpackaged codeProgram flowcontinuescallscontinues
How does a program move from its main flow into a reusable function and continue after using it?

Combining Patterns

The patterns are not isolated. They can combine and nest inside one another. For example, a function can contain a loop, and that loop can include an if statement. The sequential steps inside the selected branch provide the actions. A loop can also call a function multiple times. These combinations allow a small set of patterns to express the logic of real programs.

Complete programcombined logicSequential codeordered stepsConditional codeif statementRepetitive codeloopStore and reusefunctioncontainscontainscan containcan includeselects
How do sequential instructions, decisions, loops, and functions connect to control a complete program?
In the real world

Imagine a program that processes a collection of objects. A function can package the processing operation, a loop can apply it multiple times, a conditional statement can choose an action for each item, and sequential instructions can carry out the chosen action in order. This is a generated illustration of how the patterns can be nested; the important idea is that each pattern keeps its own role while contributing to one larger program.

Recognizing the Pattern

EASY

For each scenario, identify the main programming pattern: a program carries out three instructions in a fixed order; a program chooses one of two actions based on whether a condition is true; a program repeats the same block several times; a programmer packages a block of code so it can be called whenever needed.

Hints
  • Look for an ordered sequence, a choice, repetition, or packaging for reuse.
  • An if statement indicates conditional code.
  • A loop indicates repetitive code.
  • A function indicates store and reuse.
Worked example

Classifying Four Scenarios

Classify each scenario by the fundamental programming pattern it demonstrates.

  1. 1
    Ordered instructions

    This is sequential code because the instructions execute in order.

  2. 2
    Choice based on a condition

    This is conditional code because an if statement makes a decision between paths.

  3. 3
    Repeated block

    This is repetitive code because a loop repeats a block.

  4. 4
    Packaged operation

    This is store and reuse because a function packages code that can be called when needed.

Result

The pattern can be identified by asking whether the scenario orders instructions, makes a decision, repeats a block, or packages code for reuse.

Mistakes in Pattern Recognition

  • Common mistake

    Treating conditional code as the only fundamental pattern.

    What it looks like

    Focusing on the if decision while overlooking the ordered instructions, loops, or functions around it.

    Why it fails

    Programs are built from four patterns, and real programs combine and nest them.

    Do this instead

    Identify the decision, then inspect the surrounding code for sequential, repetitive, and store-and-reuse patterns.

  • Common mistake

    Calling every repeated action sequential code.

    What it looks like

    Describing a loop only as a list of instructions written in order.

    Why it fails

    Sequential code executes instructions in order, while repetitive code uses a loop to repeat a block.

    Do this instead

    Look for whether the block is executed multiple times.

  • Common mistake

    Confusing a function with a conditional statement.

    What it looks like

    Assuming that packaging code into a function is itself a decision.

    Why it fails

    A function represents store and reuse; conditional code makes a decision with an if statement.

    Do this instead

    Classify the code by its purpose: functions package code for reuse, while if statements choose between paths.

  • Common mistake

    Ignoring nesting.

    What it looks like

    Labeling a function as only store and reuse even when it contains a loop and an if statement.

    Why it fails

    The patterns can combine and nest inside one another.

    Do this instead

    Identify both the outer pattern and the patterns used inside it.

Key Takeaways

  1. 1
    Sequential code executes instructions in order.
  2. 2
    Conditional code uses if statements to make decisions and branch into different paths.
  3. 3
    Repetitive code uses loops to repeat blocks of instructions.
  4. 4
    Store and reuse packages code into functions that can be called whenever needed.
  5. 5
    The four patterns combine and nest to form the logic of complete programs.

At a glance

  • Conditional code makes decisions by choosing between paths based on whether a condition is true or false.
  • The four fundamental patterns are sequential code, conditional code, repetitive code, and store and reuse.
  • Each pattern solves a different programming problem: ordering, deciding, repeating, or reusing.
  • Patterns can combine and nest, such as a function containing a loop with an if statement inside it.
  • Recognizing these patterns helps you design, debug, and extend programs.

Related concepts

More concepts starting with C