Structured programming notes

3. Nested Control Structures

3.3. Combining selection and iteration

1. Structured Programming Recap

Structured programming emphasizes three main constructs:

  1. Sequence: Execute statements one after another.

  2. Selection: Make decisions (if, if-else, switch).

  3. 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:

 
Start | v [Loop Start] | v [Check Condition inside loop] | \ |Yes No v v [Do Action] [Skip / Do Another Action] | v [Loop End / Repeat] | v End

3. Example 1: Print only even numbers (structured style)

Problem: Print all even numbers from 1 to 10.

Pseudocode (Structured Programming Style):

 
START FOR i = 1 TO 10 DO IF i MOD 2 = 0 THEN PRINT i ENDIF ENDFOR END

Explanation:

  • Iteration: FOR i = 1 TO 10 repeats for each number.

  • Selection: IF i MOD 2 = 0 checks 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:

 
START sum = 0 FOR each number in array DO IF number > 0 THEN sum = sum + number ENDIF ENDFOR PRINT sum END

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.