Control statements in C allow you to manage the flow of execution of your program. They help you decide which instructions to run, how many times to run them, or when to stop running them.
C provides three main categories of control statements:
- Decision-making statements
- Looping (Iterative) statements
- Jump statements
1. Decision-Making Statements
These statements allow your program to take different paths based on conditions.
a) if Statement
Executes a block only if the condition is true.
if (a > 0) {
printf("Positive number");
}
b) if-else Statement
Executes one block when the condition is true, otherwise the other block.
if (age >= 18) {
printf("Eligible to vote");
} else {
printf("Not eligible");
}
c) else-if Ladder
Used to check multiple conditions sequentially.
if (marks >= 90) {
printf("Grade A");
} else if (marks >= 75) {
printf("Grade B");
} else {
printf("Grade C");
}
d) Nested if
Using an if statement inside another if.
if (a > 0) {
if (a % 2 == 0)
printf("Positive Even");
}
e) Switch Statement
Used when you need to compare the same expression with multiple values.
switch(day) {
case 1: printf("Monday"); break;
case 2: printf("Tuesday"); break;
default: printf("Invalid");
}
2. Looping (Iterative) Statements
Used to execute a block of code multiple times.
a) for Loop
Runs loop for a fixed number of iterations.
for (int i = 1; i <= 5; i++) {
printf("%d ", i);
}
b) while Loop
Runs as long as the condition remains true.
int i = 1;
while (i <= 5) {
printf("%d ", i);
i++;
}
c) do-while Loop
Runs at least once because the condition is checked after execution.
int i = 1;
do {
printf("%d ", i);
i++;
} while (i <= 5);
3. Jump Statements
These change the normal execution flow abruptly.
a) break Statement
Terminates a loop or switch.
for (int i = 1; i <= 10; i++) {
if (i == 5)
break;
}
b) continue Statement
Skips the current iteration and moves to the next.
for (int i = 1; i <= 5; i++) {
if (i == 3)
continue;
printf("%d ", i);
}
c) goto Statement (NOT recommended)
Transfers control to a labeled statement.
goto label;
label:
printf("Jumped!");
d) return Statement
Ends the execution of a function.
return 0;
4. Summary Table of Control Statements
| Category | Statements | Purpose |
|---|---|---|
| Decision-making | if, if-else, else-if, switch | Choose a path |
| Looping | for, while, do-while | Repeat statements |
| Jump | break, continue, goto, return | Change normal flow |
5. Example Program Using All Control Types
#include <stdio.h>
int main() {
int n = 5;
// Decision
if (n > 0)
printf("Positive number\n");
// Loop
for (int i = 1; i <= n; i++) {
if (i == 3)
continue; // Skips 3
printf("%d ", i);
}
printf("\n");
// Switch
switch (n) {
case 5: printf("Number is five\n"); break;
default: printf("Other number\n");
}
return 0;
}
Citations
🔗 View other articles about C Programming:
https://savanka.com/category/learn/c-programming/
🔗 External C Documentation:
https://www.w3schools.com/c/