Unit 3: Decision Control and Loops

This unit covers the core logic of C programming: how to make decisions and how to repeat actions.

Table of Contents

Decision Control Structures

if Statement

The if statement executes a block of code *only if* a given condition is true (non-zero).

Syntax:

if (condition) {
    // Code to execute if condition is true
}

If there is only one statement, the braces { } are optional, but it is good practice to always use them.

if-else Statement

The if-else statement provides an alternative block of code to execute if the condition is false (zero).

Syntax:

if (condition) {
    // Code to execute if condition is true
} else {
    // Code to execute if condition is false
}

This can be extended into an if-else-if ladder to check multiple conditions:

if (condition1) {
    // ...
} else if (condition2) {
    // ...
} else {
    // ... if all else fails
}

switch Statement

The switch statement is an alternative to a long if-else-if ladder. It checks a single integer or character variable against a list of constant values (cases).

Syntax:

switch (variable) {
    case value1:
        // Code for value1
        break; // IMPORTANT!

    case value2:
        // Code for value2
        break;

    default:
        // Code if no other case matches
        break;
}
"Fall-through" and `break`: If you forget the break statement, the code will "fall through" and execute the code for the *next* case as well. This is a common bug.

Loop Structures

Loops are used to repeat a block of code multiple times.

for Loop

The for loop is ideal when you know exactly how many times you want to loop (e.g., 10 times, or 'n' times).

Syntax:

//   (1. Initialization; 2. Condition; 4. Update)
for (int i = 0;         i < 10;      i++) {
    // 3. Loop Body (executes if condition is true)
}

Execution Order: 1 → 2 → 3 → 4 → 2 → 3 → 4 ... until 2 is false.

while Loop

The while loop is an entry-controlled loop. It is ideal when you don't know how many times you need to loop, but you know the condition to *stop*.

The condition is checked *before* the loop body executes.

Syntax:

int i = 0; // 1. Initialization
while (i < 10) { // 2. Condition
    // 3. Loop Body
    i++; // 4. Update
}

If the condition is false the first time, the loop body never runs.

do-while Loop

The do-while loop is an exit-controlled loop. It is the only loop in C that is guaranteed to execute at least once.

The condition is checked *after* the loop body executes.

Syntax:

int i = 0; // 1. Initialization
do {
    // 2. Loop Body
    i++; // 3. Update
} while (i < 10); // 4. Condition (note the semicolon!)

Loop and Switch Control

break Statement

The break statement has two uses:

  1. Inside a switch block, it stops execution and exits the block.
  2. Inside a loop (for, while, do-while), it immediately terminates the loop and execution continues at the next statement *after* the loop.

continue Statement

The continue statement is used only inside loops.

It immediately stops the *current iteration* and sends execution back to the top of the loop to start the next iteration.

Example: Print only odd numbers from 1 to 10.

for (int i = 1; i <= 10; i++) {
    if (i % 2 == 0) {
        continue; // Skip this iteration if 'i' is even
    }
    printf("%d ", i); // This line is skipped for even numbers
}
// Output: 1 3 5 7 9

goto Statement

The goto statement provides an unconditional jump to a labeled statement elsewhere in the *same function*.

Syntax:

if (x < 0) {
    goto error_handler;
}
// ... more code ...

error_handler:
    printf("An error occurred.\n");
Warning: The goto statement is almost never necessary. It makes code very difficult to read, debug, and maintain (this is called "spaghetti code"). It is considered bad programming practice and should be avoided. Any goto can be rewritten with structured loops (for, while) and if statements.

Code Examples (from Practicals)

Practical #2: Leap Year (if-else)

#include <stdio.h>
int main() {
    int year;
    printf("Enter a year: ");
    scanf("%d", &year);

    // A year is a leap year if it's divisible by 4,
    // UNLESS it's divisible by 100 but NOT by 400.
    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
        printf("%d is a leap year.\n", year);
    } else {
        printf("%d is not a leap year.\n", year);
    }
    return 0;
}

Practical #4: Prime Number (for loop)

#include <stdio.h>
int main() {
    int n, i, is_prime = 1; // 1 means true

    printf("Enter a positive integer: ");
    scanf("%d", &n);

    if (n <= 1) {
        is_prime = 0; // 0 and 1 are not prime
    }

    // Check for factors from 2 up to n/2
    for (i = 2; i <= n / 2; i++) {
        if (n % i == 0) {
            is_prime = 0; // Found a factor, not prime
            break;       // Exit the loop early
        }
    }

    if (is_prime == 1) {
        printf("%d is a prime number.\n", n);
    } else {
        printf("%d is not a prime number.\n", n);
    }
    return 0;
}

Practical #5: Factorial (while loop)

#include <stdio.h>
int main() {
    int n;
    long long factorial = 1; // Use long long for larger numbers
    int i;

    printf("Enter a positive integer: ");
    scanf("%d", &n);

    i = n; // Initialization
    while (i > 1) { // Condition
        factorial = factorial * i;
        i--; // Update
    }

    printf("Factorial of %d = %lld\n", n, factorial);
    return 0;
}

Unit 3: Exam Quick Tips