Control structures allow you to control the flow of execution in a C program. Instead of running code from top to bottom, you can make decisions, repeat blocks, and jump to specific parts based on logic.
- Categories of Control Structures in C:
- Conditional Statements (Decision making)
- Looping Statements (Repetition)
- Jumping Statements (Transfer of control)
Conditional Statements (Decision Making)
Conditional statements help the program decide which block of code to execute based on a condition.
- if Statement
Executes a block only if the condition is true.
int age = 20;
if (age >= 18) {
printf(“Eligible to vote”);
}
Output: Eligible to vote
- if-else Statement
Selects one of two blocks based on the condition.
int num = 10;
if (num % 2 == 0) {
printf(“Even”);
} else {
printf(“Odd”);
}
- else-if Ladder
Used when there are multiple conditions to check.
int marks = 75;
if (marks >= 90) {
printf(“Grade A”);
} else if (marks >= 75) {
printf(“Grade B”);
} else if (marks >= 60) {
printf(“Grade C”);
} else {
printf(“Grade D”);
}
- switch-case Statement
Efficient when checking a variable against many constant values.
int choice = 2;
switch(choice) {
case 1: printf(“One”); break;
case 2: printf(“Two”); break;
case 3: printf(“Three”); break;
default: printf(“Invalid”);
}
Note: Always use break; to prevent fall-through to the next case.