Structured programming notes
| Site: | Education is the key to success |
| Course: | Education is the key to success |
| Book: | Structured programming notes |
| Printed by: | |
| Date: | Saturday, 20 June 2026, 12:53 AM |
1. Introduction to structured programming
Structured programming is a way of writing programs that emphasizes clarity, logic, and maintainability. Instead of jumping around unpredictably in code, programs are built using a small set of well-defined control structures.
The main idea:
A program should be easy to read, understand, test, and modify.
Why Structured Programming Exists
Early programs relied heavily on statements, which caused “spaghetti code” to follow, error-prone, and painful to debug.
Structured programming was introduced to:
-
Improve program reliability
-
Make code easier to understand
-
Reduce logical errors
-
Support team development
Core Principles
Structured programming is based on three fundamental control structures:
1. Sequence
Statements are executed one after another, in order.
Read input
Process data
Display output
2. Selection (Decision Making)
The program chooses between alternatives.
Examples:
-
if -
if–else -
switch -
if (score >= 50) { printf("Pass"); } else { printf("Fail"); }
3. Iteration (Repetition / Looping)
A block of code runs multiple times.
Examples:
-
for -
while -
do–while -
for (int i = 1; i <= 5; i++) { printf("%d\n", i);
2. Control Structures
Control structures are fundamental programming constructs that determine the order in which statements are executed in a program. Instead of executing statements strictly from top to bottom, control structures allow a program to make decisions, repeat actions, and control the flow of execution based on conditions.
They are essential in structured programming because they help break complex problems into logical, manageable blocks, making programs easier to understand, test, and maintain.
Types of Control Structures
-
Sequential Control Structure
-
Statements are executed one after another in the order they appear.
-
This is the simplest form of control flow and is used when no decision or repetition is required.
-
-
Selection (Decision-Making) Control Structure
-
Allows the program to choose between different paths of execution based on a condition.
-
Common statements include
if,if–else, andswitch. -
Used when different actions are needed for different conditions.
-
-
Iteration (Looping) Control Structure
-
Enables a set of statements to be executed repeatedly as long as a condition is true or until a condition is met.
-
Common loops include
while,do–while, andfor. -
Used when tasks need to be repeated, such as processing data collections.
-
Importance of Control Structures
-
Improve program clarity and readability
-
Reduce complexity in program logic
-
Support structured and modular programming
-
Help avoid unstructured jumps like excessive use of
goto
2.1. Sequential Control Structure
The Sequential Control Structure is the simplest and most basic form of control structure in programming. In this structure, program statements are executed one after another in the exact order in which they appear, from the first statement to the last, without any branching or repetition.
Each statement is executed only once, and the flow of control moves step by step in a straight line. This type of control structure is used when the program logic does not require decision-making or looping.
Key Characteristics
-
Execution follows a top-to-bottom order
-
No conditions or loops are involved
-
Easy to understand and implement
-
Each statement is executed exactly once
Example Use
Sequential control structures are commonly used for:
-
Reading input data
-
Performing calculations
-
Displaying results
Importance in Structured Programming
The sequential control structure forms the foundation of all programs. Even complex programs that include selection and iteration still rely on sequential execution within their blocks.
2.2. Selection Control Structure
The Selection Control Structure is a programming construct that allows a program to make decisions and choose one path of execution from multiple alternatives based on a given condition. Instead of executing all statements sequentially, the program evaluates a condition and executes only the block of code that satisfies that condition.
This control structure is essential in structured programming because it enables programs to respond differently to different inputs or situations.
How It Works
-
A condition or logical expression is evaluated.
-
If the condition is true, a specific block of statements is executed.
-
If the condition is false, an alternative block may be executed or the program skips that section.
Common Selection Statements
-
if statement – executes a block when a condition is true
-
if–else statement – chooses between two alternatives
-
nested if statement – checks multiple conditions
-
switch / case statement – selects one option from many based on a value
Key Characteristics
-
Enables decision-making in programs
-
Improves flexibility and logic control
-
Avoids unnecessary execution of statements
-
Makes programs dynamic and interactive
Importance
Selection control structures help programs handle real-world decision scenarios, such as grading systems, menu-driven programs, and validation checks.
2.3. Iteration Control Structure
The Iteration Control Structure, also known as a looping structure, allows a program to repeat a set of instructions multiple times as long as a specified condition is satisfied. It is used when the same task needs to be performed repeatedly, which helps reduce code duplication and makes programs more efficient.
In structured programming, iteration plays a key role in tasks such as counting, searching, data processing, and generating repeated output.
How Iteration Works
-
A condition is evaluated.
-
If the condition is true, the loop body is executed.
-
After execution, the condition is checked again.
-
The loop continues until the condition becomes false.
Types of Iteration Control Structures with Examples
1. while Loop (Entry-Controlled Loop)
The condition is checked before executing the loop body. If the condition is false initially, the loop may not execute at all.
Example: Print numbers from 1 to 5.
2. do–while Loop (Exit-Controlled Loop)
The loop body is executed at least once, because the condition is checked after the execution.
Example: Print numbers from 1 to 5.
3. for Loop
The for loop is used when the number of iterations is known. Initialization, condition, and update are written in a single line.
Example: Print numbers from 1 to 5.
Key Features of Iteration Control Structures
-
Automates repetitive tasks
-
Makes programs concise and readable
-
Supports nested loops for complex logic
-
Reduces errors caused by repeated code
Summary
The Iteration Control Structure enables a program to repeat a block of code efficiently until a condition is met. By using loops like while, do–while, and for, programmers can handle repetitive tasks effectively in structured programming.
3. Nested Control Structures
Nested Control Structures occur when one control structure is placed inside another. This allows programs to handle more complex logic by combining multiple decisions or loops. Nested structures are widely used in structured programming to perform tasks that require multi-level decision-making or repeated actions within repeated actions.
Types of Nested Control Structures
-
Nested Selection Statements
-
An
iforswitchstatement placed inside another selection statement. -
Useful for situations where a decision depends on the result of a previous decision.
Example (Nested if):
-
-
Nested Loops
-
A loop inside another loop.
-
Commonly used for multi-dimensional data, like matrices or tables.
Example (Nested for loop): Print a 3x3 multiplication table.
-
-
Combination of Selection and Iteration
-
Loops containing decision statements or decisions containing loops.
-
Allows for flexible, real-world logic in programs.
Example: Print even numbers from 1 to 10.
-
Key Points
-
Nested structures increase program flexibility and problem-solving capability.
-
Over-nesting can reduce readability, so maintain clarity.
-
Always ensure proper indentation and block structure to avoid logical errors.
Summary
Nested Control Structures allow combining loops and decision-making constructs to handle complex programming logic. They are essential for tasks like multi-level decision-making, table manipulation, and processing multi-dimensional data.
3.1. Nested Selection Statements
A nested selection statement is a decision-making structure where one selection statement is placed inside another. In simpler terms, it’s like asking a question inside another question. This is commonly used in programming when you need to make more complex decisions.
Structure:
The most common selection statements are if, if-else, and switch. In nested selection, these statements are placed inside the body of another selection statement.
Example in pseudocode:
Here:
-
The first
ifcheckscondition1. -
Inside it, another
ifcheckscondition2. -
This is a nested if.
Key Points:
-
Decision within a decision: The inner selection is only evaluated if the outer condition is true.
-
Multiple levels allowed: You can nest as many selection statements as needed, but readability may suffer.
-
Alternative structures: Nested
if-elsecan sometimes be replaced by logical operators (&&,||) orswitch-casestatements for clarity. -
Indentation matters: Proper indentation is crucial to avoid confusion in nested structures.
-
Outer
ifcheckAdvantages:
-
Allows handling complex decision-making.
-
Can check multiple conditions step by step.
Disadvantages:
-
Too many levels of nesting make code hard to read and maintain.
-
Can be simplified using logical operators or functions.
3.2. Nested loops
A nested loop is a loop inside another loop.
-
The outer loop controls the number of times the inner loop runs.
-
The inner loop executes completely for each iteration of the outer loop.
In simple terms:
“For every single pass of the outer loop, the inner loop runs all of its cycles.”
Structure
Here’s the general structure:
Pseudocode:
for (initialization1; condition1; increment1) { # Outer loop
for (initialization2; condition2; increment2) { # Inner loop
// Code block to execute
}
-
Outer loop: Controls the number of times the inner loop executes.
-
Inner loop: Runs completely for each iteration of the outer loop.
Flow of Execution
-
Outer loop starts its first iteration.
-
Inner loop executes all its iterations.
-
Outer loop moves to its next iteration.
-
Inner loop runs again completely.
-
Repeat until the outer loop finishes.
Notice that for each value of i, the inner loop completes all iterations of j.
Applications of Nested Loops
-
Printing patterns (triangles, pyramids, etc.)
-
Traversing 2D arrays or matrices
-
Performing repetitive calculations in multiple dimensions
-
Generating combinations or permutations
Points to Remember
-
Performance: Nested loops can increase time complexity. A loop inside a loop is roughly O(n × m) if outer loop runs
ntimes and inner runsmtimes. -
Readability: Avoid deep nesting (more than 2–3 levels) to keep code readable.
-
Flexibility: Inner and outer loops can be of different types (for, while, do-while).
3.3. Combining selection and iteration
1. Structured Programming Recap
Structured programming emphasizes three main constructs:
-
Sequence: Execute statements one after another.
-
Selection: Make decisions (
if,if-else,switch). -
Iteration: Repeat actions (
for,while,do-while).
Combining selection and iteration means using loops with conditional decisions inside them, while keeping the code clear, modular, and easy to follow.
2. Combining Selection and Iteration
Concept:
-
Iteration repeats a block of code.
-
Selection chooses which statements to execute inside that block.
-
Together, they let a program repeat actions conditionally.
Flow:
3. Example 1: Print only even numbers (structured style)
Problem: Print all even numbers from 1 to 10.
Pseudocode (Structured Programming Style):
Explanation:
-
Iteration:
FOR i = 1 TO 10repeats for each number. -
Selection:
IF i MOD 2 = 0checks if the number is even. -
Only even numbers are printed.
4. Example 2: Sum positive numbers from a list
Problem: Sum all positive numbers in an array.
Pseudocode:
Explanation:
-
Iteration goes through all numbers in the array.
-
Selection filters only positive numbers to add.
-
Combines repetition and decision-making clearly.
Key Points in Structured Programming
-
Keep loops and conditional blocks nested neatly.
-
Avoid goto statements; use structured loops and if-else.
-
Combine selection and iteration to filter, validate, or process data repeatedly.
3.4. Examples of flowchart
1. What is a Flowchart?
A flowchart is a diagrammatic representation of an algorithm or process.
-
It uses symbols to represent different steps.
-
Helps visualize the flow of control, especially in structured programming.
Common symbols:
| Symbol | Meaning |
|---|---|
| Oval / Ellipse | Start or End |
| Rectangle | Process / Action |
| Diamond | Decision / Selection (Yes/No) |
| Parallelogram | Input / Output |
| Arrows | Flow of control |
2. Examples of Flowchart Descriptions
Example 1: Find the largest of two numbers
Description: The program reads two numbers and prints the largest one.
Flowchart Steps:
-
Start (Oval)
-
Input number A and number B (Parallelogram)
-
Decision: Is A > B? (Diamond)
-
Yes: Print A (Rectangle)
-
No: Print B (Rectangle)
-
-
End (Oval)
Explanation:
-
The diamond represents selection, choosing which number to print.
-
The flowchart clearly shows the decision and the flow to the end.
Example 2: Print even numbers from 1 to 5
Description: The program prints only even numbers from 1 to 5 using a loop.
Flowchart Steps:
-
Start (Oval)
-
Set
i = 1(Rectangle) -
Decision: Is
i <= 5? (Diamond)-
No: Go to End
-
Yes: Continue
-
-
Decision: Is
i MOD 2 = 0? (Diamond)-
Yes: Print i (Parallelogram)
-
No: Skip printing
-
-
Increment
i = i + 1(Rectangle) -
Go back to Step 3 (loop)
-
End (Oval)
Explanation:
-
Combines iteration (loop from 1 to 5) and selection (print only if even).
-
Flowcharts make loops and decisions visually clear.
Example 3: Sum positive numbers until zero is entered
Description: Keep reading numbers, sum the positive ones, and stop when 0 is entered.
Flowchart Steps:
-
Start (Oval)
-
Set
sum = 0(Rectangle) -
Input number (Parallelogram)
-
Decision: Is number = 0? (Diamond)
-
Yes: Go to Step 6 (End)
-
No: Continue
-
-
Decision: Is number > 0? (Diamond)
-
Yes: Add number to sum (Rectangle)
-
No: Skip addition
-
-
Go back to Step 3 (loop)
-
Output sum (Parallelogram)
-
End (Oval)
Explanation:
-
Iteration: Loop continues until 0 is entered.
-
Selection: Only positive numbers are added.
-
Flowchart shows decision-making and repetition clearly.