Control structures (also called decision control statements) let a C program choose what to do next based on conditions, and repeat actions using loops. They are essential for building real programs that respond to user input, data, and changing situations.
C control flow mainly includes:
- Decision making:
if,if-else, nestedif,else-ifladder,switch, ternary?: - Loops:
for,while,do-while - Jump statements:
break,continue,goto,return
1) Why Do We Need Conditional Statements?
In programming, you often need logic like:
- If marks ≥ 40 → pass, else fail
- If age ≥ 18 → eligible, else not
- If choice is 1/2/3 → do different actions
Conditional statements help you implement that decision-making cleanly.
2) Decision Making in C
2.1 if Statement
Executes a block only if the condition is true.
Syntax
if (condition) {
// executes if condition is true
}
Example
#include <stdio.h>
int main() {
int x = 10;
if (x > 5) {
printf("x is greater than 5\n");
}
printf("Program continues...\n");
return 0;
}
2.2 if-else Statement
Provides two paths:
- if condition is true → run
ifblock - else → run
elseblock
Syntax
if (condition) {
// true block
} else {
// false block
}
Example (Even/Odd)
#include <stdio.h>
int main() {
int num = 4956;
if (num % 2 == 0) {
printf("%d is Even\n", num);
} else {
printf("%d is Odd\n", num);
}
return 0;
}
2.3 Nested if / Nested if-else
An if (or if-else) inside another if block for more complex decisions.
Syntax
if (condition1) {
if (condition2) {
// both true
} else {
// condition1 true, condition2 false
}
} else {
// condition1 false
}
Example
#include <stdio.h>
int main() {
int x = 15;
if (x > 10) {
if (x < 20) {
printf("x is between 10 and 20\n");
} else {
printf("x is 20 or more\n");
}
} else {
printf("x is 10 or less\n");
}
return 0;
}
2.4 if - else if - else Ladder
Used when you have multiple conditions to test in order.
The first true condition runs, and the rest are skipped.
Syntax
if (condition1) {
// ...
} else if (condition2) {
// ...
} else if (condition3) {
// ...
} else {
// none matched
}
Example (Positive/Negative/Zero)
#include <stdio.h>
int main() {
int number = 0;
if (number > 0) {
printf("The number is Positive\n");
} else if (number < 0) {
printf("The number is Negative\n");
} else {
printf("The number is Zero\n");
}
return 0;
}
✅ Tip: Ladder is different from nested if because it checks conditions sequentially at the same level.
2.5 switch Statement
Best when you compare one expression against many constant values.
Syntax
switch (expression) {
case value1:
// ...
break;
case value2:
// ...
break;
default:
// ...
}
Example
#include <stdio.h>
int main() {
int day = 3;
switch (day) {
case 1: printf("Sunday"); break;
case 2: printf("Monday"); break;
case 3: printf("Tuesday"); break;
default: printf("Invalid day");
}
return 0;
}
Important: If you forget break, C will fall through into the next case.
2.6 Ternary Operator ?:
Short form of if-else when assigning/returning a value.
Syntax
condition ? expr_if_true : expr_if_false;
Example
#include <stdio.h>
int main() {
int x = 5;
int result = (x > 0) ? 1 : 0;
printf("Result: %d\n", result);
return 0;
}
3) Looping Control Structures in C
Loops help repeat tasks efficiently.
3.1 for Loop
Best when number of iterations is known.
for (int i = 1; i <= 5; i++) {
printf("HelloWorld\n");
}
3.2 while Loop
Runs while condition remains true (entry-controlled loop).
int i = 0;
while (i < 5) {
printf("Hello, World\n");
i++;
}
3.3 do-while Loop
Runs at least once because the condition is checked after the body.
#include <stdbool.h>
#include <stdio.h>
int main() {
bool condition = false;
do {
printf("This is the loop body.\n");
} while (condition);
return 0;
}
4) Jump Statements (Flow Control Helpers)
break→ exits loop/switch immediatelycontinue→ skips to next loop iterationreturn→ exits the functiongoto→ jumps to a labeled statement (rarely recommended)
Example (break)
for (int i = 1; i <= 10; i++) {
if (i == 6) break;
printf("%d ", i);
}
Output: 1 2 3 4 5
Summary
- Use
iffor simple decisions - Use
if-elsewhen there are two choices - Use nested if for multi-level checks
- Use else-if ladder for multiple conditions in order
- Use switch for clean multi-option branching
- Use loops (for/while/do-while) for repetition
- Use jump statements (break/continue/return) to control execution flow
Leave a Reply