Control structures determine the flow of execution of a program. Instead of running sequentially line by line, they allow decisions, repetitions, and jumps to be made.
There are three main types: Branching, Looping, and Jumping.
1. Branching (Decision-Making)
(a) if Statement
Executes a block if the condition is true.
if (marks >= 40) {
printf(“Pass”);
}
(b) if…else
Executes one block if true, another if false.
if (marks >= 40)
printf(“Pass”);
else
printf(“Fail”);
(c) if…else if…else
Checks multiple conditions.
if (marks >= 80)
printf(“Distinction”);
else if (marks >= 60)
printf(“First Division”);
else
printf(“Pass”);
(d) switch…case
Used when multiple conditions depend on the value of one variable.
int day = 2;
switch(day) {
case 1: printf(“Sunday”); break;
case 2: printf(“Monday”); break;
default: printf(“Invalid”);
}
2. Looping (Repetition)
(a) while Loop
Condition checked first, executes repeatedly until false.
int i = 1;
while (i <= 5) {
printf(“%d “, i);
i++;
}
(b) do…while Loop
Executes at least once, then checks condition.
int i = 1;
do {
printf(“%d “, i);
i++;
} while (i <= 5);
(c) for Loop
Compact form, commonly used.
for (int i = 1; i <= 5; i++) {
printf(“%d “, i);
}
3. Jumping Statements
- goto → Jumps to a labeled statement. Rarely used (bad practice).
goto end;
printf(“Skipped”);
end:
printf(“Reached here”);
- break → Immediately exits a loop or switch.
for (int i = 1; i <= 10; i++) {
if (i == 5) break;
printf(“%d “, i); // Prints 1 2 3 4
}
- continue → Skips the current iteration, continues loop.
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
printf(“%d “, i); // Prints 1 2 4 5
}
4. Nested Control Structures
Control structures inside another control structure.
Example: Nested Loop
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
printf(“i=%d, j=%d\n”, i, j);
}
}
Key Takeyaways
- Control structures decide the execution flow of a program.
- Branching: if, if-else, else-if, switch.
- Looping: while, do-while, for.
- Jumping: goto, break, continue.
- Nested control structures are allowed and commonly used in loops.
- Loops are best for repetition, switch is best for multi-way selection.