Concepts / Boolean Logic and Conditions

Boolean Logic and Conditions

Sequential execution performs statements one after another in the order they appear in the script.

  • Programming

Three Ways Instructions Flow

Every program is a series of instructions that a computer executes. Those instructions can follow three important execution patterns: they can run one after another in a fixed order, run only when a condition is satisfied, or repeat multiple times. Understanding how these patterns work together helps you control what a program does and when it does it.

The central question is not only which instructions a program contains, but also which instruction runs next, whether a block is skipped, and how many times a repeated block runs.

Following the Program in Order

Sequential execution means that statements run one after another in the order they appear in the script. The first statement is processed, then the next statement, and so on. Nothing in this pattern changes the order or causes a block to repeat: the program moves forward through its instructions.

nextnextRead scoresCheck averagePrint summary
What statement runs next when a program executes each statement in the order it appears?

In the source scenario, the program first reads student scores in order. After that step, it checks the average score. Finally, it prints a summary. The order of these actions illustrates sequential execution: each action follows the previous one.

Making a Boolean Decision

Conditional execution checks a condition and decides whether to run or skip a block of statements. A condition can be understood through its Boolean outcome: when the condition is true, the associated block can run; when it is not true, that block is skipped. The decision changes the path the program follows.

truenot trueafter blockafter skipAverage above 70CongratulationsmessageSummarySkipped block
How does a Boolean condition determine which branch of statements runs and which statements are skipped?

Checking the class average

Trace what happens when a program processes student scores and checks whether the average score is above 70.

Sequential step: The program reads the student scores in order.

Condition check: The program checks whether the average score is above 70. This check produces the decision used by the conditional execution.

Conditional path: If the average is above 70, the program proceeds to the congratulations block. If it is not, that block is skipped.

Next instruction: The program continues to the summary after the conditional part.

The condition controls whether the congratulations block runs, while the surrounding steps retain their sequential order.

Repeating with Variation

Repeated execution runs a block of statements multiple times, usually with some variation, until a stopping condition is met. Each repetition is another pass through the block. The values or item being processed can vary from one repetition to the next, while the stopping condition determines when the repeated execution ends.

repeatrepeatcheckcontinueStudent score 1current itemStudent score 2next itemStudent score 3next itemStopping condition
What happens on each loop iteration, and how can the values or condition change between repetitions?

In the student-score scenario, the program prints a congratulations message for each student whose score is above the average. The message-producing action is repeated for multiple students, and the student being considered can vary on each repetition.

A repeated block has two important features: it runs more than once, and it eventually stops when its stopping condition is met.

Tracing the Combined Flow

Real programs combine sequential, conditional, and repeated execution. The student-score scenario shows how these patterns fit together: the program first reads the scores in order, then checks a condition about the average, then repeatedly processes qualifying students, and finally prints a summary.

nextabove 70not above 70current studentabove averagenext studentrepeatstopping conditionRead scoresCheck next studentPrint congratulationsSummaryAverage above 70Score above average
How does control flow move through a program when statements run in order, branches select actions, and loops repeat steps?
StageExecution patternWhat happens
Read the scoresSequentialScores are read in order.
Check the averageConditionalThe program decides whether the average is above 70.
Process studentsRepeatedThe program considers students multiple times, with the student being processed varying between repetitions.
Print the summarySequentialThe program performs the final summary step after the earlier processing.

The three execution patterns can appear in one program and can be nested within one another.

truefalseafter executionafter skipConditionTruerun blockNext statementFalseskip block
How do Boolean values such as true and false result from conditions and control what the program does next?

When tracing a combined program, identify the current statement, check whether a condition changes the path, identify whether a block repeats, and then continue to the next sequential step after the decision or repetition finishes.

Mistakes in Flow Tracing

  • Treating every statement as if it runs at the same time.

    Sequential execution processes statements one after another in the order they appear.

    Fix: Trace the program from the first statement to the next statement, preserving the stated order.

  • Assuming that a conditional block always runs.

    Conditional execution can run a block or skip it depending on the condition.

    Fix: Write down the condition's outcome before following the selected path.

  • Confusing one decision with repeated execution.

    A condition selects whether a block runs or is skipped, while repeated execution runs a block multiple times until a stopping condition is met.

    Fix: Mark the decision and the repeated block as separate parts of the flow.

  • Ignoring the stopping condition.

    Repeated execution continues until a stopping condition is met.

    Fix: At each repetition, identify what changes and check whether the stopping condition has been met.

Practice the Trace

MEDIUM

Trace the student-score scenario in four stages. First, identify the sequential actions. Next, identify the condition that selects a path. Then identify the block that repeats and the stopping condition. Finally, state which action occurs after the repeated processing finishes.

Hints
  • Start with the order in which the program reads scores, checks the average, processes students, and prints the summary.
  • Separate the average check from the repeated processing of individual students.
  • The final action is the summary step.

Answering the trace

State the control-flow pattern for the student-score program.

Order: The scores are read in order.

Decision: The program checks whether the average score is above 70.

Repetition: When the relevant path is taken, the program processes each student whose score is above the average.

Completion: After the processing, the program prints a summary.

The trace combines sequential execution, conditional execution, repeated execution, and sequential execution again.

What to Remember

  1. Sequential execution runs statements one after another in the order they appear.
  2. Conditional execution checks a condition and either runs or skips a block of statements.
  3. Repeated execution runs a block multiple times, usually with variation, until a stopping condition is met.
  4. Real programs combine all three patterns to make decisions, process data, and automate tasks.
  5. Tracing control flow means following the order, evaluating decisions, tracking repetitions, and identifying what runs next.

Key Takeaways

  • Sequential execution follows the order of statements in a script.
  • A condition produces a decision about whether a block runs or is skipped.
  • Repeated execution processes a block multiple times with variation until a stopping condition is met.
  • Programs become useful by combining ordered steps, decisions, and repetitions.
  • A control-flow trace should account for every sequential step, branch, repetition, and stopping point.