Chapter 6: Decision Making (Control Flow)
1. if Statement (Simple Decision)
Syntax:
|
0 1 2 3 4 5 6 7 8 |
if (condition) { // code to run if condition is true } |
Condition must be something that is true (non-zero) or false (zero).
Example:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <stdio.h> int main() { int age; printf("Enter your age: "); scanf("%d", &age); if (age >= 18) { printf("You are eligible to vote! 🎉\n"); } printf("Thank you for checking.\n"); // This runs always return 0; } |
Sample Output:
- If you enter 20 → “You are eligible to vote!”
- If you enter 15 → Nothing special, just “Thank you…”
2. if…else Statement (Two Choices)
Syntax:
|
0 1 2 3 4 5 6 7 8 9 10 |
if (condition) { // true part } else { // false part } |
Example:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <stdio.h> int main() { int marks; printf("Enter your marks: "); scanf("%d", &marks); if (marks >= 40) { printf("Congratulations! You passed.\n"); } else { printf("Sorry, you failed. Better luck next time.\n"); } return 0; } |
3. Nested if…else (if inside if)
When you need more than two choices or conditions depend on each other.
Example:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <stdio.h> int main() { int age; char has_license; printf("Enter your age: "); scanf("%d", &age); if (age >= 18) { printf("You are adult. Do you have driving license? (Y/N): "); scanf(" %c", &has_license); if (has_license == 'Y' || has_license == 'y') { printf("Great! You can drive legally.\n"); } else { printf("You are adult but you need a license to drive.\n"); } } else { printf("You are still a minor. Wait a few more years!\n"); } return 0; } |
4. else if Ladder (Multiple Choices)
Used when you have many conditions to check one by one.
Syntax:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
if (condition1) { // code } else if (condition2) { // code } else if (condition3) { // code } else { // none of the above } |
Example – Grading System
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <stdio.h> int main() { int marks; printf("Enter your marks (0-100): "); scanf("%d", &marks); if (marks >= 90) { printf("Grade: A+ - Excellent!\n"); } else if (marks >= 80) { printf("Grade: A - Very Good!\n"); } else if (marks >= 70) { printf("Grade: B - Good!\n"); } else if (marks >= 60) { printf("Grade: C - Average\n"); } else if (marks >= 40) { printf("Grade: D - Pass\n"); } else { printf("Grade: F - Fail\n"); } return 0; } |
5. switch…case Statement (Best for Multiple Fixed Choices)
Used when you have one variable and many exact values to compare.
Syntax:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
switch (expression) { case value1: // code break; case value2: // code break; default: // code if no match } |
Important:
- Always use break; otherwise next cases will also run (called fall-through)
- default is optional – runs if no case matches
Example – Day of the Week
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include <stdio.h> int main() { int day; printf("Enter day number (1-7): "); scanf("%d", &day); switch (day) { case 1: printf("Monday\n"); break; case 2: printf("Tuesday\n"); break; case 3: printf("Wednesday\n"); break; case 4: printf("Thursday\n"); break; case 5: printf("Friday\n"); break; case 6: printf("Saturday\n"); break; case 7: printf("Sunday - Holiday!\n"); break; default: printf("Invalid day number!\n"); } return 0; } |
6. Ternary Operator (?:) – Short if…else
Syntax:
|
0 1 2 3 4 5 6 |
variable = (condition) ? value_if_true : value_if_false; |
Example:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <stdio.h> int main() { int age; char *status; printf("Enter your age: "); scanf("%d", &age); status = (age >= 18) ? "Adult" : "Minor"; printf("You are a/an %s.\n", status); return 0; } |
Very useful for short decisions.
7. goto Statement (and Why You Should Avoid It)
goto jumps to a labeled part of the code.
Syntax:
|
0 1 2 3 4 5 6 7 8 9 10 11 |
goto label; ... label: // code here |
Example (just for understanding – NOT recommended):
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <stdio.h> int main() { int num; printf("Enter a positive number: "); scanf("%d", &num); if (num < 0) { goto error; // Jump to error label } printf("Thank you! Number is %d\n", num); goto end; error: printf("Error: Negative number not allowed!\n"); end: printf("Program ends.\n"); return 0; } |
Why Avoid goto?
- Makes code very hard to read and understand (spaghetti code)
- Can create bugs that are difficult to find
- Almost every program can be written better using if, else, loops, functions
- Famous programmers (like Dijkstra) said: “goto is considered harmful”
Rule: Never use goto in normal programs. Use it only in very special cases (like breaking out of nested loops – but even then, better ways exist).
Today’s Homework
- Write a program using if…else to check if a number is positive, negative, or zero.
- Create a program using else if ladder to check temperature:
-
35 → Very Hot
- 25–35 → Hot
- 15–25 → Pleasant
- < 15 → Cold
-
- Write a switch program that takes a character (A, B, C, D, F) and prints the meaning (Excellent, Very Good, Good, Average, Fail).
- Use ternary operator to find the maximum of two numbers entered by the user.
