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.
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.
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.
Choosing an Action
A program must choose between two actions depending on whether a condition is true or false.
- 1Evaluate
The program examines the condition.
- 2Choose
If the condition is true, the program follows the true path. If the condition is false, it follows the other path.
- 3Continue
After the selected action, the program continues with the rest of its logic.
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.
| Pattern | Programming problem | Recognizing feature |
|---|---|---|
| Sequential code | Execute instructions in order | One instruction follows another |
| Conditional code | Make a decision | An if statement chooses between paths |
| Repetitive code | Repeat a block | A loop executes instructions multiple times |
| Store and reuse | Avoid duplicating code | A 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.
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.
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
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.
Classifying Four Scenarios
Classify each scenario by the fundamental programming pattern it demonstrates.
- 1Ordered instructions
This is sequential code because the instructions execute in order.
- 2Choice based on a condition
This is conditional code because an if statement makes a decision between paths.
- 3Repeated block
This is repetitive code because a loop repeats a block.
- 4Packaged operation
This is store and reuse because a function packages code that can be called when needed.
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 likeFocusing on the if decision while overlooking the ordered instructions, loops, or functions around it.
Why it failsPrograms are built from four patterns, and real programs combine and nest them.
Do this insteadIdentify 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 likeDescribing a loop only as a list of instructions written in order.
Why it failsSequential code executes instructions in order, while repetitive code uses a loop to repeat a block.
Do this insteadLook for whether the block is executed multiple times.
Common mistake Confusing a function with a conditional statement.
What it looks likeAssuming that packaging code into a function is itself a decision.
Why it failsA function represents store and reuse; conditional code makes a decision with an if statement.
Do this insteadClassify the code by its purpose: functions package code for reuse, while if statements choose between paths.
Common mistake Ignoring nesting.
What it looks likeLabeling a function as only store and reuse even when it contains a loop and an if statement.
Why it failsThe patterns can combine and nest inside one another.
Do this insteadIdentify both the outer pattern and the patterns used inside it.
Key Takeaways
- 1Sequential code executes instructions in order.
- 2Conditional code uses if statements to make decisions and branch into different paths.
- 3Repetitive code uses loops to repeat blocks of instructions.
- 4Store and reuse packages code into functions that can be called whenever needed.
- 5The 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.