Unit 4: Decision Making and Looping
Table of Contents
1. Decision Making and Branching
Decision making structures allow your program to execute certain blocks of code only if a specific condition is true.
Simple IF Statement
Executes a block of code only if the condition is true. If the condition is false, the block is skipped.
int age = 20; if (age >= 18) { printf("You are eligible to vote.\n"); // This line WILL run } if (age < 18) { printf("You are not eligible.\n"); // This line will be SKIPPED } IF-ELSE Statement
Provides a two-way branch. One block runs if the condition is true, and a different block runs if it is false.
int num = 7; if (num % 2 == 0) { printf("The number is even.\n"); } else { printf("The number is odd.\n"); // This line WILL run } Nested of IF-ELSE Statements
You can place an if-else statement inside another if or else block to check for multiple conditions.
int a = 10, b = 20, c = 5; if (a > b) { if (a > c) { printf("a is the largest.\n"); } else { printf("c is the largest.\n"); } } else { if (b > c) { printf("b is the largest.\n"); // This line WILL run } else { printf("c is the largest.\n"); } } ELSE IF Ladder
This is a cleaner way to write multiple nested if-else statements. It is used for multi-way decisions where only one condition can be true.
int marks = 75; if (marks >= 90) { printf("Grade A\n"); } else if (marks >= 80) { printf("Grade B\n"); } else if (marks >= 70) { printf("Grade C\n"); // This block runs } else { printf("Grade F\n"); } switch Statement
The switch statement is an efficient alternative to an else if ladder when you are checking a single variable against a list of constant integer or character values.
int day = 3; switch (day) { case 1: printf("Monday\n"); break; case 2: printf("Tuesday\n"); break; case 3: printf("Wednesday\n"); // This block runs break; default: printf("Invalid day\n"); } break; statement. If you forget break in case 1, the program will execute the code for case 1 AND case 2 (and so on) until it hits a break. This is called "fall-through." 2. Looping (Iteration)
Loops are used to execute a block of code repeatedly as long as a certain condition is true.
while Statement
This is an entry-controlled loop. The condition is checked *before* the loop body is executed. If the condition is false the first time, the loop body will never run.
int i = 1; while (i <= 5) { printf("%d ", i); // Prints 1 2 3 4 5 i++; } do-while Statement
This is an exit-controlled loop. The loop body is executed *first*, and then the condition is checked. This guarantees that the loop body will run at least once.
int i = 100; do { printf("%d ", i); // Prints 100 i++; } while (i <= 5); // Condition is false, but loop ran once for Statement
The for loop is ideal for situations where you know how many times you want to loop. It combines initialization, condition, and increment/decrement into one line.
for (initialization; condition; update) { ... } // This loop runs 5 times for (int i = 1; i <= 5; i++) { printf("%d ", i); // Prints 1 2 3 4 5 } - Use
forwhen you know the number of iterations (e.g., 10 times, or for every element in an array). - Use
whilewhen you don't know the number of iterations (e.g., "keep running until user types 'q'"). - Use
do-whilewhen you need the loop to run at least one time (e.g., a menu that redisplays).