UNIT II: STRUCTURED PROGRAMMING
CONCEPTS AND C LANGUAGE
Introduction to Structured Programming
Structured programming represents a fundamental paradigm in computer science that
revolutionized how programmers design and implement software solutions. This
methodology, first formally proposed in the late 1960s by computer scientist Edsger
Dijkstra, emphasizes the use of well-defined control structures to create clear,
maintainable, and error-free code. The structured programming theorem demonstrates
that any algorithm can be constructed using just three basic building blocks: sequence,
selection, and iteration.
The beauty of structured programming lies in its simplicity and universality. Just as DNA
uses only three simple component groups (phosphate, sugar, and nitrogen) to describe
all life, structured programming uses three fundamental control structures to solve all
computational problems. This approach eliminates the need for unstructured branching
statements (like the infamous "goto" statement) that made programs difficult to
understand and maintain. By the end of the 20th century, nearly all computer scientists
were convinced that learning and applying structured programming concepts is essential
for creating reliable software.
Fundamental Control Structures
Sequence Structure
The sequence structure is the most basic and intuitive control structure in
programming. It represents the straightforward execution of statements in a specific,
predetermined order. In this structure, each statement is executed one after another,
from top to bottom, without any branching or repetition. The sequence is so fundamental
that it almost "goes without saying" – yet it forms the foundation upon which all other
control structures are built.
In C programming, sequence structure is implemented through consecutive statements
that execute linearly. Unless new instructions or control structures are encountered,
statements execute in the exact order they appear in the source code.
Example 1: Simple Sequence in C
#include <stdio.h>
int main() {
int num1, num2, sum;
// Step 1: Input first number
printf("Enter first number: ");
scanf("%d", &num1);
// Step 2: Input second number
printf("Enter second number: ");
scanf("%d", &num2);
// Step 3: Calculate sum
sum = num1 + num2;
// Step 4: Display result
printf("Sum = %d\n", sum);
return 0;
}
Output:
Enter first number: 15
Enter second number: 28
Sum = 43
Selection Structure (Decision Making)
The selection structure allows programs to make decisions and execute different
blocks of code based on specific conditions. This structure provides the ability to choose
between alternative paths of execution, making programs dynamic and responsive to
different situations. Selection is implemented using conditional statements that evaluate
Boolean expressions (conditions that are either true or false).
If Statement
The if statement is the most fundamental decision-making structure. It executes a block
of code only when a specified condition evaluates to true.
Syntax:
if (condition) {
// Statements to execute if condition is true
}
Characteristics:
The condition is evaluated first before any execution
If the condition is true, the statements inside the braces are executed
If the condition is false, the statements are skipped entirely
Conditions use relational operators: ==, !=, <, >, <=, >=
Logical operators can combine conditions: && (AND), || (OR), ! (NOT)
Example 2: If Statement
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18) {
printf("You are eligible to vote.\n");
printf("Please proceed to register.\n");
}
printf("Thank you!\n");
return 0;
}
If-Else Statement
The if-else statement extends the basic if statement by providing an alternative path of
execution when the condition is false. This creates a two-way branching structure.
Syntax:
if (condition) {
// Statements to execute if condition is true
} else {
// Statements to execute if condition is false
}
Example 3: If-Else Statement
#include <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number % 2 == 0) {
printf("%d is an EVEN number.\n", number);
} else {
printf("%d is an ODD number.\n", number);
}
return 0;
}
Nested If Statements
Nested if statements occur when an if statement is placed inside another if statement,
allowing for complex multi-level decision making. This structure enables programs to
evaluate multiple conditions in a hierarchical manner.
Example 4: Nested If Statements
#include <stdio.h>
int main() {
int age;
char student;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18) {
printf("You are an adult.\n");
printf("Are you a student? (Y/N): ");
scanf(" %c", &student);
if (student == 'Y' || student == 'y') {
printf("You qualify for student discount!\n");
} else {
printf("Regular pricing applies.\n");
}
} else {
printf("You are a minor.\n");
printf("Parental consent required.\n");
}
return 0;
}