UNIT 2
STRUCTURED PROGRAMMING CONCEPTS
1. Introduction to Structured Programming
Structured programming is a programming paradigm that improves clarity, quality, and
development time by using clear, logical structures for code execution. It emphasizes dividing
a program into smaller blocks known as modules or functions. These blocks are connected
using control structures such as sequence, selection, and repetition.
Key Points: - Encourages top-down program design. - Avoids the
got statements. -
use of debugging and testing simpler. - Enhances readability and o Makes
maintainability.
2. Sequence Structure (Input/Output/Assignment)
A sequence structure executes statements one after another in a linear
order.
Example:
int a = 10, b = 20,
sum; sum = a + b;
printf("Sum = %d", sum);
Explanation: Each statement executes sequentially — first variable declaration, then calculation,
and finally output.
Flowchart:
[Start]
|
[Read a, b]
|
[sum = a + b]
|
[Display
sum]
|
[En
d]
3. Selection Structure (Decision Making)
Used to execute different statements based on a condition.
1
Example (if):
if (x > 0)
printf("Positive");
Example (if-else):
if (x % 2 == 0)
printf("Even");
else
printf("Odd");
Explanation: The program checks the condition and executes the block that meets the requirement.
Flowchart:
[Start] → [Input x]
↓
[x % 2 == 0?]
/
Yes No
| |
[Print Even][Print Odd]
\ /
[End]
4. Repetition Structures (Looping Statements)
Repetition structures allow executing a set of statements multiple times.
a) For Loop (Entry Controlled)
Used when the number of iterations is known.
for (int i = 1; i <= 5; i+
+) { printf("%d ", i);
}
Explanation: The loop
i , checks the condition, executes, and increments until the
initializes fails. condition
2
b) While Loop (Entry Controlled)
Executes while the condition is true.
int i = 1;
while (i <= 5) {
printf("%d ",
i); i++;
}
c) Do-While Loop (Exit Controlled)
Executes at least once, even if the condition is false.
int i =
1; do {
printf("%d ",
i); i++;
} while (i <= 5);
5. Control Structure Stacking and Nesting
Stacking: Sequential use of multiple structures. Nesting: One control structure inside another.
Example (Nested Loop):
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= i;
j++) { printf("*");
}
printf("\n");
}
Output:
*
**
**
*
Explanation: The outer loop controls rows, and the inner loop prints stars in each row.
3
6. Different Kinds of Repetitions
•Entry Controlled: Condition tested first for
( , while )
•Exit Controlled: Condition tested after execution ( do-while )
•Counter Controlled: Runs a fixed number of times.
•Definite Loops: Known limit of iterations.
•Indefinite Loops: Number of iterations unknown.
•Sentinel Controlled: Stops when a specific value (sentinel) is input.
Example (Sentinel Controlled):
int n, sum = 0;
while (1) {
scanf("%d", &n);
if (n == -1)
break; sum +=
n;
}
printf("Sum = %d", sum);
7. Procedure and Flowcharts
A procedure defines a set of steps or instructions to solve a problem.
Flowchart Symbols: | Symbol | Meaning | |---------|--------| | Oval | Start/Stop | | Rectangle |
Process | |
Parallelogram | Input/Output | | Diamond | Decision |
Example Flowchart (Sum of Two Numbers):
[Start] → [Input A, B] → [Sum = A + B] → [Print Sum] → [End]
8. Algorithms
An algorithm is a finite set of well-defined instructions to perform a task or solve a problem.
Characteristics: - Input and output clearly defined - Finiteness and effectiveness - Unambiguous
steps - Generality and correctness
Standard Algorithm Format: 1. Start 2. Input values 3. Process (computation) 4. Output results 5.
Stop
Example (Finding Factorial):
4
Start
Read n
fact =
1
for i = 1 to n
do fact =
fact * i
Print fact
Stop
9. Problems Involving Iteration and Nesting
•Simple Loops: Printing numbers, squares, or sums.
•Nested Loops: Patterns and tables.
Example – Print Multiplication Table:
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 10;
j++) { printf("%d\
t", i * j);
}
printf("\n");
}
10.Displaying Patterns and Shapes
Example – Triangle Pattern:
for (int i = 1; i <= 4;
i++) { for (int j =
1; j <= i; j++)
printf("* ");
printf("\n");
}
Output:
*
* *
* * *
* * * *
Explanation: Nested loops are used to print stars in an increasing pattern.
5
11.Arithmetic and Geometric Progression
AP Example:
int a = 2, d = 3;
for (int i = 0; i < 5; i++)
printf("%d ", a + i * d);
GP Example:
int a = 3, r = 2;
for (int i = 0; i < 5; i+
+) printf("%d ", a *
pow(r, i));
12.Fibonacci and Other Sequences
Example:
int n1 = 0, n2 = 1, n3;
printf("%d %d ", n1, n2);
for (int i = 2; i < 8;
i++) { n3 = n1 +
n2;
printf("%d ",
n3); n1 = n2;
n2 = n3;
}
Output: 0 1 1 2 3 5 8 13
13.Approximation Problems (π, sin(x), cos(x))
Used in scientific calculations using series expansions.
Example (Approximation of π):
double pi =
0; int sign =
1;
for (int i = 1; i <= 1000; i
+= 2) { pi += sign *
(4.0 / i);
6
sign = -sign;
}
printf("Approx value of pi = %lf", pi);
14.Representation of Data in Computer Memory
Data is represented using binary digits (0 and 1). Each type of data (integer, real number,
character) has a specific format.
a) Representation of Integers
•Signed Magnitude: MSB represents sign (0 = +, 1 = –)
•1’s Complement: Invert all bits.
•2’s Complement: Invert bits and add 1.
b) Representation of Real Numbers (IEEE 754)
Represented as: - Sign bit (1 bit) - Exponent (8 bits) - Mantissa (23 bits)
Example:
(+1.101)₂ × 2³ = 13.0
c) Character Representation
•ASCII: 7-bit (A = 65, a = 97)
•UNICODE: 16-bit supports multiple languages.