Concepts / Introduction to Loops

Introduction to Loops

Loops belong to repeated execution: a block of statements runs multiple times, usually with some variation, until a stopping condition is met.

  • Programming

Three Ways Programs Run

Every program is a series of instructions that a computer executes. These instructions do not all run in the same way. Some run one after another in a fixed order. Some run only when a condition is true. Others repeat multiple times. These three patterns are sequential execution, conditional execution, and repeated execution.

Key point

Loops belong to repeated execution: a block of statements runs multiple times, usually with some variation, until a stopping condition is met.

Tracing the Score-Processing Program

A useful way to understand program flow is to trace a program one stage at a time. Consider a program that processes a list of student test scores. It first reads the scores in order. It then checks whether the average score is above 70. If that condition is true, it prints a congratulations message for each student whose score is above the average. Finally, it prints a summary.

Read scoresin orderCheck averageabove 70?Skip messagescondition is falsePrint congratulationsfor a qualifying studentPrint summaryCheck studentscore above average?Next studentrepeat the checknext statementnoyesyesnocontinuemore studentsall students processedcontinue
What happens as control moves through sequential statements, checks a condition, repeats work for students, and finishes with a summary?
Worked example

Following the control flow

Identify which execution pattern is represented at each stage of the score-processing program.

  1. 1
    Read the scores

    The program reads the scores in the order they appear. This is sequential execution because the statements proceed one after another.

  2. 2
    Check the average

    The program checks whether the average score is above 70. This is conditional execution because the result determines whether a block of statements runs or is skipped.

  3. 3
    Process students

    When the average is above 70, the program checks students and prints a congratulations message for each student whose score is above the average. This is repeated execution because the work is performed for multiple students.

  4. 4
    Print the summary

    After the earlier work is complete, the program prints a summary. This returns to sequential execution because the summary follows the preceding statements.

Result

The program combines sequential execution, conditional execution, and repeated execution rather than using only one pattern.

How Each Pattern Controls Flow

Sequential execution is the basic order of a program. Statements are performed one after another in the order they appear in the script. Unless another execution pattern changes the flow, the next statement follows the current statement.

Conditional execution introduces a decision. The program checks a condition and decides whether to run or skip a block of statements. In the score-processing scenario, the condition is whether the average score is above 70. The result of that check determines whether the congratulations-processing block is used.

Repeated execution introduces a return to an earlier block. The program runs the block multiple times, usually with some variation, until a stopping condition is met. In the score-processing scenario, the student being checked changes as the program processes the list of students.

Average above 70conditionProcess qualifyingstudentscondition is truePrint summarySkip processing blockcondition is falsetruefalseafter blockafter decision
How does a condition determine which statements run and which statements are skipped?

What Changes on Each Repetition

A loop is not simply a block that runs forever. Repeated execution continues until a stopping condition is met. Something usually varies between repetitions. In the source scenario, the program moves through different students, checks each student's score against the average, and prints a congratulations message only for a student whose score is above that average.

Student 1check scoreStudent 2check scoreNext studentcontinue while studentsremainAll studentsprocessedstopping conditionrepeatrepeatnext iterationno students remain
How does the program return to the repeated statements, and what changes on each iteration?

Practice the Trace

MEDIUM

Using the student-score scenario, describe the order in which the program performs its major actions. Identify the sequential stages, the condition that controls whether the student-processing block runs, the repeated work inside that block, and the stopping condition for the repetition.

Hints
  • Start with the actions that happen in a fixed order.
  • Name the question the program checks before deciding whether to process students.
  • For repeated execution, identify what changes from one student check to the next.
  • End by identifying what happens after all students have been processed.
  • Common mistake

    Treating every statement as sequential execution

    What it looks like

    Assuming that the congratulations message runs for every student without considering the condition.

    Why it fails

    Conditional execution can cause a block of statements to run or be skipped.

    Do this instead

    Check the condition first, then determine whether the block runs.

  • Common mistake

    Describing repetition without identifying what changes

    What it looks like

    Saying that the program repeats but not identifying which student is being checked.

    Why it fails

    Repeated execution usually includes variation and continues until a stopping condition is met.

    Do this instead

    Track the changing item and the condition that ends the repetition.

  • Common mistake

    Ignoring the statements after a loop

    What it looks like

    Stopping the trace after the last student is checked.

    Why it fails

    The source scenario performs sequential execution again by printing a summary after the repeated processing.

    Do this instead

    Continue tracing after the repeated block and include the final sequential statements.

Flow-Control Summary

  1. 1
    Sequential execution performs statements one after another in the order they appear.
  2. 2
    Conditional execution checks a condition and runs or skips a block of statements.
  3. 3
    Repeated execution runs a block multiple times, usually with variation, until a stopping condition is met.
  4. 4
    Real programs combine all three patterns to make decisions, process data, and automate tasks.
  5. 5
    Tracing the order, decisions, repetitions, and stopping points helps explain what a program does.

At a glance

  • Sequential execution follows the order of statements in a script.
  • Conditional execution uses a condition to decide whether a block runs or is skipped.
  • Repeated execution returns to a block and performs it multiple times with variation until a stopping condition is met.
  • Programs commonly combine sequential, conditional, and repeated execution.
  • A reliable trace follows the program's order, checks each decision, tracks each repetition, and continues through the final statements.

Related concepts

More concepts starting with I