Concepts / Introduction to Loops

Introduction to Loops

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

  • 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.

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.

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

Following the control flow

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

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

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.

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.

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.

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.

truefalseafter blockafter decisionAverage above 70conditionProcess qualifyingstudentscondition is truePrint summarySkip processing blockcondition is false
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.

repeatrepeatnext iterationno students remainStudent 1check scoreStudent 2check scoreNext studentcontinue while studentsremainAll studentsprocessedstopping condition
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.

Flow-Control Summary

  1. Sequential execution performs statements one after another in the order they appear.
  2. Conditional execution checks a condition and 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 the order, decisions, repetitions, and stopping points helps explain what a program does.

Key Takeaways

  • 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.