Table of Contents

    What is Control Flow?

    Programming Mastery

    What is Control Flow?

    Learn how control flow decides the order in which program instructions execute, how programs make decisions, and how repeated actions are handled using sequence, selection, and iteration.

    What is Control Flow?

    Control flow means the order in which instructions or statements are executed in a program.

    In simple words, control flow decides which statement runs first, which statement runs next, whether a decision should be made, and whether some instructions should be repeated.

    Control flow is the path a program follows while executing instructions.

    By default, most programs run from top to bottom, one instruction after another. But real programs often need more flexibility. Sometimes a program must make a decision. Sometimes it must repeat a task many times. Sometimes it must choose between multiple paths. Control flow makes all of this possible.

    Easy Real-Life Example

    Control Flow as Following a Route

    Imagine you are going from home to school. Normally, you follow one route step by step. But if there is traffic, you may choose another route. If you are waiting for a bus, you may keep waiting until the bus arrives.

    Similarly, a program follows instructions step by step, but it can also make decisions and repeat actions based on conditions.

    Why is Control Flow Important?

    Control flow is important because without it, a program would only execute fixed instructions in a straight line. It would not be able to make decisions, repeat tasks, or respond to different situations.

    Importance of Control Flow

    • It decides the execution order of program instructions.
    • It allows a program to make decisions.
    • It helps a program repeat tasks.
    • It allows programs to respond to different user inputs.
    • It makes programs dynamic instead of fixed.
    • It supports problem-solving using structured logic.
    • It helps build calculators, login systems, games, menus, and real-world applications.
    • It is required for conditions, loops, and branching logic.

    Control Flow in a Program

    A program is made of statements. Control flow decides how those statements are executed.

    Simple Sequential Flow

    DISPLAY "Start Program"
    DISPLAY "Read Input"
    DISPLAY "Process Data"
    DISPLAY "Show Output"
    DISPLAY "End Program"

    In this example, each statement runs in order from top to bottom. This is the simplest type of control flow.

    Main Types of Control Flow

    In beginner programming, control flow is usually understood through three main structures:

    Sequence

    Instructions run one after another in order.

    Selection

    The program chooses a path based on a condition.

    Iteration

    The program repeats instructions multiple times.

    1. Sequence

    Sequence means executing statements one after another in the order they are written.

    This is the default flow of most programs.

    Example

    DISPLAY "Enter your name:"
    INPUT userName
    
    DISPLAY "Welcome"
    DISPLAY userName

    The program first displays a message, then reads the user name, then displays a welcome message. The steps happen in order.

    Student Result Example

    INPUT studentName
    INPUT marks
    
    DISPLAY studentName
    DISPLAY marks

    This is sequence because the statements are executed line by line.

    2. Selection

    Selection means choosing which block of code should run based on a condition.

    Selection is used when a program needs to make decisions.

    Example

    INPUT marks
    
    IF marks >= 35 THEN
        DISPLAY "Pass"
    ELSE
        DISPLAY "Fail"
    END IF

    Here, the program checks a condition. If marks are greater than or equal to 35, it displays Pass. Otherwise, it displays Fail.

    Common Selection Statements

    Selection Type Purpose Example Use
    IF Runs code only when a condition is true. Check if marks are passing marks.
    IF ELSE Chooses between two paths. Pass or fail result.
    ELSE IF Checks multiple conditions. Grade calculation.
    CASE / SWITCH Chooses from multiple fixed options. Calculator operation menu.

    3. Iteration

    Iteration means repeating a block of code multiple times.

    Iteration is also called looping. It is useful when the same task needs to be performed again and again.

    Example

    SET counter = 1
    
    WHILE counter <= 5 DO
        DISPLAY counter
        SET counter = counter + 1
    END WHILE

    This program displays numbers from 1 to 5. The display statement repeats until the condition becomes false.

    Common Loop Types

    Loop Type Purpose Example Use
    FOR Repeats a known number of times. Print numbers from 1 to 10.
    WHILE Repeats while a condition is true. Keep asking until valid input is entered.
    DO WHILE Runs once first, then checks condition. Show a menu at least once.

    Sequence vs Selection vs Iteration

    Control Flow Type Meaning Simple Example
    Sequence Run statements in order. Input marks, calculate result, display result.
    Selection Choose a path based on condition. If marks are at least 35, display Pass.
    Iteration Repeat statements. Keep asking for input until it is valid.

    Control Flow Example: Simple Calculator

    A calculator uses control flow to choose which operation should run.

    INPUT firstNumber
    INPUT secondNumber
    INPUT operation
    
    IF operation == "+" THEN
        DISPLAY firstNumber + secondNumber
    
    ELSE IF operation == "-" THEN
        DISPLAY firstNumber - secondNumber
    
    ELSE IF operation == "*" THEN
        DISPLAY firstNumber * secondNumber
    
    ELSE IF operation == "/" THEN
        IF secondNumber != 0 THEN
            DISPLAY firstNumber / secondNumber
        ELSE
            DISPLAY "Division by zero is not allowed"
        END IF
    
    ELSE
        DISPLAY "Invalid operation"
    END IF

    This example uses sequence, selection, and nested selection. First, inputs are read in sequence. Then the program selects the correct operation. Inside division, another decision checks whether division by zero is happening.

    Control Flow Example: Student Result

    INPUT studentName
    INPUT marks
    
    IF marks >= 90 THEN
        DISPLAY "Grade A"
    ELSE IF marks >= 75 THEN
        DISPLAY "Grade B"
    ELSE IF marks >= 35 THEN
        DISPLAY "Pass"
    ELSE
        DISPLAY "Fail"
    END IF

    This program uses control flow to choose the correct result based on marks.

    Control Flow Example: Input Validation

    DISPLAY "Enter marks:"
    INPUT marks
    
    WHILE marks < 0 OR marks > 100 DO
        DISPLAY "Invalid marks. Enter marks between 0 and 100:"
        INPUT marks
    END WHILE
    
    DISPLAY "Marks accepted"

    This example uses iteration. The program keeps asking for marks until the user enters a valid value.

    Control Flow and Pseudocode

    In pseudocode, control flow is written using simple keywords such as:

    • IF, ELSE, ELSE IF for decisions.
    • WHILE, FOR, REPEAT for loops.
    • CASE or SWITCH for multiple fixed choices.
    • START and STOP for algorithm boundaries.

    Control Flow and Flowcharts

    Control flow can also be shown using flowcharts. Flowcharts visually show how a program moves from one step to another.

    Flowchart Symbol Meaning
    Oval Start or Stop
    Parallelogram Input or Output
    Rectangle Process or calculation
    Diamond Decision or condition
    Arrow Direction of control flow

    How Control Flow Helps Debugging

    Understanding control flow helps programmers find where a program is going wrong.

    Debugging Questions

    • Which statement runs first?
    • Which condition is being checked?
    • Is the condition true or false?
    • Which branch of code is executed?
    • Is the loop repeating correctly?
    • Does the loop stop at the right time?
    • Is any statement skipped unexpectedly?
    • Is the program stuck in an infinite loop?

    Common Beginner Mistakes

    Mistakes

    • Thinking every program only runs from top to bottom.
    • Forgetting that conditions can change the path of execution.
    • Writing conditions incorrectly.
    • Creating loops that never stop.
    • Using too many nested conditions.
    • Not testing all possible branches.
    • Not updating loop variables.
    • Mixing input, processing, and output without structure.

    Better Habits

    • Trace the program step by step.
    • Use clear conditions.
    • Test both true and false cases.
    • Make sure loops have a stopping condition.
    • Use meaningful variable names.
    • Keep nested conditions simple.
    • Use flowcharts for complex logic.
    • Use dry runs and trace tables.

    Best Practices for Control Flow

    Good control flow makes a program easier to read, test, and maintain.

    Recommended Practices

    • Write logic in a clear step-by-step order.
    • Use sequence for simple linear tasks.
    • Use selection when a decision is needed.
    • Use iteration when a task must repeat.
    • Avoid unnecessary nesting.
    • Use clear condition expressions.
    • Test all branches of selection logic.
    • Test loop starting and ending conditions.
    • Use comments only when logic needs explanation.
    • Use dry runs to verify program flow.

    Prerequisites Before Learning Control Flow

    To understand control flow properly, students should already know a few basic programming concepts.

    Basic Prerequisites

    • What is a program?
    • Statements and expressions.
    • Variables and constants.
    • Data types.
    • Operators and expressions.
    • Comparison operators.
    • Logical operators.
    • Input and output.
    • Algorithm and pseudocode basics.
    • Dry run and trace table basics.

    Practice Activity: Identify the Control Flow

    Identify whether each scenario uses sequence, selection, or iteration.

    Scenario Control Flow Type
    A program asks for name and then displays a greeting. ________________________
    A program checks whether marks are pass or fail. ________________________
    A program prints numbers from 1 to 10. ________________________
    A calculator chooses addition, subtraction, multiplication, or division. ________________________
    A program keeps asking for password until it is correct. ________________________

    Sample Answers

    Scenario Control Flow Type
    A program asks for name and then displays a greeting. Sequence
    A program checks whether marks are pass or fail. Selection
    A program prints numbers from 1 to 10. Iteration
    A calculator chooses addition, subtraction, multiplication, or division. Selection
    A program keeps asking for password until it is correct. Iteration with selection

    Mini Quiz

    1

    What is control flow?

    Control flow is the order in which statements are executed in a program.

    2

    What is sequence?

    Sequence means executing statements one after another in order.

    3

    What is selection?

    Selection means choosing which block of code to execute based on a condition.

    4

    What is iteration?

    Iteration means repeating a block of code multiple times.

    5

    Why is control flow important?

    Control flow allows programs to make decisions, repeat tasks, and respond to different situations.

    Interview Questions on Control Flow

    1

    Define control flow in programming.

    Control flow is the mechanism that determines the order and path of execution of statements in a program.

    2

    Name the three basic control flow structures.

    The three basic control flow structures are sequence, selection, and iteration.

    3

    How does selection control flow work?

    Selection checks a condition and executes one block of code if the condition is true and another block if it is false.

    4

    How does iteration control flow work?

    Iteration repeats a block of code while a condition is true or for a fixed number of times.

    5

    Give a real-world programming example of control flow.

    A login program uses control flow to check username and password, display success if correct, or show an error if incorrect.

    Quick Summary

    Concept Meaning
    Control Flow Order and path of program execution.
    Sequence Statements execute one after another.
    Selection Program chooses a path based on condition.
    Iteration Program repeats instructions.
    Condition An expression that evaluates to true or false.
    Loop A structure used for repetition.
    Best Practice Use clear logic, simple conditions, and test all possible paths.

    Final Takeaway

    Control flow is one of the most important foundations of programming. It decides how a program moves from one instruction to another. With control flow, programs can run steps in order, make decisions, and repeat tasks. In the Programming Mastery Course, students should understand control flow through three beginner-friendly ideas: sequence, selection, and iteration.