C Programming

Control Flow — Conditional Statements; Control Flow — Loops

C-CAT

Control Flow — Conditional Statements

if Statement

#include <stdio.h>

int main() {
    int score = 75;

    // Simple if
    if (score >= 60) {
        printf("Passed!\n");
    }

    // if-else
    if (score >= 60) {
        printf("Passed!\n");
    } else {
        printf("Failed!\n");
    }

    // if-else if-else (grading system)
    char grade;
    if (score >= 90) {
        grade = 'A';
    } else if (score >= 80) {
        grade = 'B';
    } else if (score >= 70) {
        grade = 'C';
    } else if (score >= 60) {
        grade = 'D';
    } else {
        grade = 'F';
    }
    printf("Grade: %c\n", grade);    // C (for score=75)

    return 0;
}

Nested if

int num = 15;

if (num > 0) {
    printf("Positive\n");
    if (num % 2 == 0) {
        printf("and Even\n");
    } else {
        printf("and Odd\n");         // This will print
    }
} else {
    printf("Non-positive\n");
}

switch Statement

int day = 3;

switch (day) {
    case 1:
        printf("Monday\n");
        break;
    case 2:
        printf("Tuesday\n");
        break;
    case 3:
        printf("Wednesday\n");   // This executes
        break;
    case 4:
        printf("Thursday\n");
        break;
    case 5:
        printf("Friday\n");
        break;
    case 6:
    case 7:
        printf("Weekend\n");     // Both 6 and 7 fall through to this
        break;
    default:
        printf("Invalid day\n");
}

Important: break exits the switch; without it, execution "falls through" to next case.

Control Flow — Loops

for Loop

// for (init; condition; update) { body }
for (int i = 0; i < 5; i++) {
    printf("i = %d\n", i);
}
// Output: i=0, i=1, i=2, i=3, i=4

// Countdown
for (int i = 10; i > 0; i--) {
    printf("%d ", i);
}
printf("\n");

// Fibonacci
int a = 0, b = 1;
for (int i = 0; i < 10; i++) {
    printf("%d ", a);
    int temp = a + b;
    a = b;
    b = temp;
}
// 0 1 1 2 3 5 8 13 21 34

while Loop

// while (condition) { body }
int i = 0;
while (i < 5) {
    printf("i = %d\n", i);
    i++;
}

// Read until sentinel value
int num;
int sum = 0;
printf("Enter numbers (0 to stop): ");
while (scanf("%d", &num) == 1 && num != 0) {
    sum += num;
}
printf("Sum: %d\n", sum);

do-while Loop

// do { body } while (condition);
// Executes body at LEAST ONCE before checking condition

int choice;
do {
    printf("\n1. Add\n2. Subtract\n3. Exit\nChoice: ");
    scanf("%d", &choice);

    switch (choice) {
        case 1: printf("Adding...\n"); break;
        case 2: printf("Subtracting...\n"); break;
        case 3: printf("Goodbye!\n"); break;
        default: printf("Invalid choice!\n");
    }
} while (choice != 3);   // keep looping until user chooses exit

break and continue

// break: exits the innermost loop immediately
for (int i = 0; i < 10; i++) {
    if (i == 5) break;        // stop when i reaches 5
    printf("%d ", i);          // 0 1 2 3 4
}

// continue: skips rest of current iteration, goes to next
for (int i = 0; i < 10; i++) {
    if (i % 2 == 0) continue; // skip even numbers
    printf("%d ", i);          // 1 3 5 7 9
}

Nested Loops

// Multiplication table
for (int i = 1; i <= 5; i++) {
    for (int j = 1; j <= 5; j++) {
        printf("%4d", i * j);
    }
    printf("\n");
}

// Printing stars pattern
for (int i = 1; i <= 5; i++) {
    for (int j = 1; j <= i; j++) {
        printf("* ");
    }
    printf("\n");
}
// Output:
// *
// * *
// * * *
// * * * *
// * * * * *

Continue learning

Related notes

Put this topic into timed practice

Open mock tests when you want full-exam pacing, or keep drilling in practice mode.