Computer Science: Class 11 — Chapter 6: Flow of Control

Introduction to Flow of Control

In computer programming, the order in which statements are executed is critical. By default, Python executes statements sequentially, one after another, from the beginning to the end of the program. This is known as the concept of sequence . However, real-world problem-solving requires the ability to alter this default execution path based on conditions or to repeat operations. The order of execution of statements in a program is known as the flow of control , which is implemented using control structures.

Python supports two primary types of control structures to manage the flow of execution: selection (decision making) and repetition (looping).

Core Computer Science Concept Technical Definition / Operational State Structural Parameters & Specifications Computational Function & System Purpose
Sequence Linear execution of statements. Statements are executed in the exact order they are written. Performs straightforward, step-by-step calculations and operations.
Selection Conditional branching of execution. Evaluates boolean expressions to choose between alternative execution paths. Enables decision-making capabilities within programs based on runtime data.
Repetition Iterative execution of a block of code. Repeats a block of statements as long as a control condition remains true. Automates redundant tasks and processes collections of data efficiently.
Selection Control Structures

Selection structures allow a program to test conditions and execute different blocks of code depending on whether the condition evaluates to true or false. In Python, selection is implemented using if , if..else , and if..elif..else statements.

The Simple if Statement

The if statement is used to execute a block of code only if a specified condition is true. If the condition is false, the block is skipped.

Syntax of the if statement:

  
    if condition:
    statement(s)
  

Example of a simple if statement:

  
    age = int(input("Enter your age: "))
    if age >= 18:
    print("Eligible to vote")
  
The if..else Statement

The if..else statement provides two alternative paths of execution. If the condition evaluates to true, the block under the if statement is executed; otherwise, the block under the else statement is executed.

Syntax of the if..else statement:

  
    if condition:
    statement(s)
    else:
    statement(s)
  

Example of an if..else statement:

  
    age = int(input("Enter your age: "))
    if age >= 18:
    print("Eligible to vote")
    else:
    print("Not eligible to vote")
  
The if..elif..else Statement

When multiple conditions need to be checked, Python provides the if..elif..else structure (where elif stands for else-if). This creates a chain of conditions that are evaluated sequentially.

Syntax of the if..elif..else statement:

  
    if condition_1:
    statement(s)
    elif condition_2:
    statement(s)
    elif condition_3:
    statement(s)
    else:
    statement(s)
  

Example of checking if a number is positive, negative, or zero:

  
    number = int(input("Enter a number: "))
    if number > 0:
    print("Number is positive")
    elif number < 0:
    print("Number is negative")
    else:
    print("Number is zero")
  

Technical Profile: Python Selection Statements

  • Architecture Layer / Tier: Core Language Control Flow
  • Operational Mechanics & Syntax: Evaluates expressions to boolean values ( True or False ). Uses colon ( : ) to denote the start of a block and relies on indentation to define block boundaries.
  • System Trade-offs & Failures: Highly readable, but deeply nested selection structures can lead to complex, hard-to-maintain code (often referred to as "spaghetti code").
Indentation in Python

Unlike many programming languages that use curly braces {} to define blocks of code, Python uses indentation . Leading whitespace (spaces or tabs) at the beginning of a line is used to determine the grouping of statements.

Example of correct block indentation:

  
    if num1 > num2:
    # Block 1 starts
    print("first number is larger")
    print("Bye")
    # Block 1 ends
    else:
    # Block 2 starts
    print("second number is larger")
    print("Bye Bye")
    # Block 2 ends
  
Repetition (Looping Constructs)

Repetition or iteration constructs allow a set of statements to be executed repeatedly based on a loop control condition. Python provides two looping constructs: the for loop and the while loop .

The for Loop

The for loop is used to iterate over a range of values or a sequence (such as a string, list, or tuple). It is typically used when the number of iterations is known in advance.

Syntax of the for loop :

  
    for control_variable in sequence:
    statements inside body of the loop
  

Example of iterating over a string:

  
    for letter in 'PYTHON':
    print(letter)
  
The range() Function

The built-in range() function is commonly used with the for loop to generate a sequence of integers.

Syntax of the range() function:

  
  range([start], stop[, step])

  • start (optional): The starting value of the sequence (default is 0).
  • stop (required): The upper limit of the sequence (excluded from the generated sequence).
  • step (optional): The increment/decrement value between consecutive numbers (default is 1).

Interactive terminal examples of range() :

  
    >>> list(range(10))
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> list(range(2, 10))
    [2, 3, 4, 5, 6, 7, 8, 9]
    >>> list(range(0, 30, 5))
    [0, 5, 10, 15, 20, 25]
    >>> list(range(0, -9, -1))
    [0, -1, -2, -3, -4, -5, -6, -7, -8]
  
The while Loop

The while loop executes a block of code repeatedly as long as a specified control condition remains true. It is an entry-controlled loop, meaning the condition is evaluated before executing the loop body.

Syntax of the while loop :

  
    while test_condition:
    body of while
  

Example of a while loop :

  
    count = 1
    while count <= 5:
    print(count)
    count += 1
  

Technical Profile: Python Looping Constructs

  • Architecture Layer / Tier: Iterative Control Flow
  • Operational Mechanics & Syntax: for loops iterate over iterable sequences. while loops evaluate a boolean expression on each iteration.
  • System Trade-offs & Failures: If the loop control variable in a while loop is not updated correctly to eventually make the condition False , it results in an infinite loop , causing the program to hang or crash due to resource exhaustion.
Jump Statements: Break and Continue

Jump statements are used to alter the normal flow of a loop by prematurely exiting the loop or skipping specific iterations.

The break Statement

The break statement immediately terminates the execution of the loop in which it is placed. The program control resumes at the statement immediately following the loop.

Example of using break in a loop:

  
    for num in range(10):
    num = num + 1
    if num == 8:
    break
    print('Num has value ' + str(num))
    print('Out of loop')
  
The continue Statement

The continue statement skips the remaining statements in the current iteration of the loop and jumps directly to the next iteration, re-evaluating the loop's control condition.

Example of using continue in a loop:

  
    num = 0
    for num in range(6):
    num = num + 1
    if num == 3:
    continue
    print('Num has value ' + str(num))
    print('End of loop')
  
Nested Loops

A loop can contain another loop inside its body. This structure is called a nested loop . Python allows any type of loop (for or while) to be nested inside any other loop.

Example of nested for loops:

  
    for var1 in range(3):
    print("Iteration " + str(var1 + 1) + " of outer loop")
    for var2 in range(2):
    print(var2 + 1)
    print("Out of inner loop")
    print("Out of outer loop")
  

Example of generating a pattern using nested loops:

  
    num = int(input("Enter a number to generate its pattern = "))
    for i in range(1, num + 1):
    for j in range(1, i + 1):
    print(j, end=" ")
    print()
  
Case Studies & Programming Exercises

Case Study: Student Marks Information System (SMIS) Grading Logic

Develop a menu-driven program for SMIS that accepts student marks in five major subjects, calculates the total sum, computes the percentage, and assigns a letter grade based on the following criteria:

  • Percentage > 85: Grade A
  • Percentage < 85 and >= 75: Grade B
  • Percentage < 75 and >= 50: Grade C
  • Percentage > 30 and <= 50: Grade D
  • Percentage < 30: Reappear

Socio-Technical Analysis & Core Systemic Impact: This system automates academic evaluation using sequential input, arithmetic processing, and multi-way selection structures. The implementation of logical operators (such as and ) ensures mutually exclusive grading boundaries. Proper validation is required to handle edge cases, such as preventing division by zero if the number of subjects is dynamically modified, and ensuring input values fall within valid ranges (0 to 100).