C Control Statements
Defination
In C, control structures guide the progression and behavior of your code. These statements steer program execution based on defined pathways, decisions, and repetition, enabling dynamic outcomes instead of static line-by-line execution.
Main Types of Control Flow Statements
1. Conditional Directives
These allow your logic to evaluate a condition and choose between multiple directions.
if
Performs a logical evaluation, and if the condition is met, executes a specified sequence of actions.
if (temperature > 30) {
printf("Too hot!");
} if-else
Offers an alternative course of action when the initial condition fails.
if (marks >= 50) {
printf("Pass");
} else {
printf("Fail");
} else if
Allows for multiple condition branches in succession.
if (score >= 90) {
printf("A");
} else if (score >= 80) {
printf("B");
} else {
printf("C or below");
} switch
Enables multi-way selection based on a variable’s value. It’s ideal when you have many possible fixed options.
switch (day) {
case 1: printf("Monday"); break;
case 2: printf("Tuesday"); break;
default: printf("Unknown Day");
} 2. Repetitive Constructs
Looping statements repeat blocks of code while conditions remain valid.
while
Repeat the set until the check fails.
int i = 0;
while (i < 5) {
printf("%d\n", i);
i++;
} do-while
Performs the loop body at least once, regardless of the condition, then checks.
int x = 1;
do {
printf("%d\n", x);
x++;
} while (x <= 3); for
Common for running loops a known number of times, with all control logic in a single line.
for (int i = 0; i < 5; i++) {
printf("%d\n", i);
} 3. Jumping Techniques
Used to redirect execution flow instantly from one point to another.
break
Halts execution of the current loop or switch block immediately.
for (int i = 0; i < 10; i++) {
if (i == 5) break;
printf("%d ", i);
} continue
Skips the remaining steps in the current loop iteration and goes to the next cycle.
for (int j = 1; j <= 5; j++) {
if (j == 3) continue;
printf("%d ", j);
} goto
Jumps to a labeled part of the program. Though rare and discouraged, it exists for specific cases.
int num = 1;
begin:
printf("%d\n", num);
num++;
If (num <= 3) goto begin; Key Points to Remember
- Sequence, decision-making, and repetition form the base of control flow.
- Nesting control statements can enable multi-layered logic.
- Excessive use of goto can lead to unreadable "spaghetti code."
- Prefer switch-case for multiple condition matches on a single variable.
- Always use break with switch unless fall-through is intentional.
Summary Table
| Category | Statement | Action |
|---|---|---|
| Decision | if, else, else if | Pick paths based on checks |
| Branching | switch | Match against multiple constants |
| Looping | for, while, do-while | Repeat tasks |
| Redirection | break, continue, goto | Alter or skip flow mid-way |
Prefer Learning by Watching?
Watch these YouTube tutorials to understand C Tutorial visually:
What You'll Learn:
- 📌 C_27 If Statement in C | C Programming Tutorials
- 📌 Control Statements In C Explained | Types Of Control Statements In C | C Programming | Simplilearn