Day 2 Progress
0%
Day 2  ·  1 Hour

Control Flow, Loops & Functions

Make decisions, repeat actions, and organize code — the three pillars of every real program.

0–15 min · if / else
15–30 min · Loops
30–45 min · Functions
45–60 min · Arrays & Quiz
1

Conditionals — if, else if, else

0 – 15 min

Conditionals let your program make decisions. Based on whether a condition is true or false, C executes different blocks of code. The basic structure is:

  • if — runs the block when the condition is true
  • else if — checks another condition if the first was false
  • else — catches everything that didn't match above
grade.c
C
#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 >= 75) {
        printf("Grade: B — Good!\n");
    } else if (marks >= 50) {
        printf("Grade: C — Average\n");
    } else {
        printf("Grade: F — Please study harder!\n");
    }

    return 0;
}
  • marks >= 90 — relational operator, checks if marks is greater than or equal to 90
  • C evaluates each condition top to bottom — the first one that is true runs, the rest are skipped
  • else has no condition — it runs only when all above are false
OperatorMeaningExampleResult
==Equal toa == btrue if equal
!=Not equala != btrue if different
>Greater thana > btrue if a bigger
<Less thana < btrue if a smaller
>=Greater or equala >= btrue if a ≥ b
<=Less or equala <= btrue if a ≤ b
⚠️ Common mistake: = vs ==
if (x = 5) sets x to 5 (always true!). if (x == 5) checks if x equals 5. Using = inside an if condition is one of the most dangerous C bugs.
2

Switch Statement

13 – 18 min

When you have one variable that can take several exact values, switch is cleaner than a long chain of else if. Each case matches one value. Always end each case with break — otherwise execution "falls through" to the next case.

dayname.c
C
#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\n");    break;
        default: printf("Invalid day!\n");
    }

    return 0;
}
💡 default is like else — it runs when no case matches. Always include it to handle unexpected input gracefully.
mid-lesson checkpoint
3

Loops — for, while, do-while

18 – 35 min

Loops let you repeat a block of code without writing it again and again. C has three types — each suited for a different situation:

  • for — when you know exactly how many times to repeat
  • while — when you repeat as long as a condition is true (check first)
  • do-while — same as while, but runs at least once (check after)
forloop.c
C
#include <stdio.h>

int main() {
    // Print multiplication table of 5
    for (int i = 1; i <= 10; i++) {
        printf("5 x %d = %d\n", i, 5 * i);
    }
    return 0;
}

The for loop has three parts inside the parentheses, separated by semicolons:

  • Init: int i = 1 — runs once at the start, sets up the counter
  • Condition: i <= 10 — checked before every iteration; loop stops when false
  • Update: i++ — runs after every iteration; i++ means add 1 to i
while.c
C
#include <stdio.h>

int main() {
    int n, sum = 0, i = 1;
    printf("Enter a number: ");
    scanf("%d", &n);

    // while loop: sum of 1 to n
    while (i <= n) {
        sum += i;   // same as: sum = sum + i
        i++;
    }

    printf("Sum from 1 to %d = %d\n", n, sum);
    return 0;
}
dowhile.c
C
#include <stdio.h>

int main() {
    int choice;

    do {
        printf("\n--- MENU ---\n");
        printf("1. Say Hello\n");
        printf("2. Exit\n");
        printf("Enter choice: ");
        scanf("%d", &choice);

        if (choice == 1)
            printf("Hello!\n");

    } while (choice != 2); // keeps looping until user picks 2

    printf("Goodbye!\n");
    return 0;
}
LoopWhen to UseChecks ConditionMin Runs
forKnown count of iterationsBefore each run0 times
whileUnknown count, condition-drivenBefore each run0 times
do-whileMenu loops, must run onceAfter each run1 time always
💡 break & continuebreak exits the loop immediately. continue skips the rest of the current iteration and jumps to the next one. These work inside all three loop types.
4

Functions — Write Once, Use Anywhere

35 – 48 min

A function is a named, reusable block of code. Instead of copy-pasting the same logic in 10 places, you write it once as a function and call it whenever needed. Functions make code readable, organized, and maintainable.

A function has four parts:

  • Return type — what kind of value the function gives back (int, float, void for nothing)
  • Name — what you call it (e.g. add, greet, factorial)
  • Parameters — inputs the function receives (can be empty)
  • Body — the code that runs, wrapped in { }
functions.c
C
#include <stdio.h>

// Function that returns the sum of two integers
int add(int a, int b) {
    return a + b;
}

// Function that returns nothing (void)
void greet(char name[]) {
    printf("Hello, %s!\n", name);
}

// Function to check if a number is even
int isEven(int n) {
    return (n % 2 == 0);  // returns 1 (true) or 0 (false)
}

int main() {
    int result = add(8, 5);
    printf("8 + 5 = %d\n", result);  // 13

    greet("Ananta");

    if (isEven(42))
        printf("42 is even\n");

    return 0;
}
terminal
output
8 + 5 = 13
Hello, Ananta!
42 is even
💡 Function Prototype — If you define a function after main(), you must declare a prototype at the top so C knows about it. Example: int add(int a, int b); placed before main(). It's like a promise to the compiler that this function exists.
⚠️ void means no return value. If your function's job is to just print something or do an action (not calculate a value), use void as the return type and skip the return statement.
5

Arrays — Store Multiple Values

48 – 58 min

An array stores a collection of values of the same type under one name. Instead of writing int score1, score2, score3..., you write int scores[5] — and access each value by its index (position).

  • Array index starts at 0, not 1 — so a 5-element array uses indices 0 to 4
  • All elements must be the same data type
  • Size is fixed when declared — you cannot grow an array later (for that you use dynamic memory)
arrays.c
C
#include <stdio.h>

int main() {
    // Declare and initialize an array of 5 integers
    int scores[5] = {85, 92, 78, 96, 88};

    // Access individual elements
    printf("First score:  %d\n", scores[0]);  // 85
    printf("Third score:  %d\n", scores[2]);  // 78

    // Loop through the whole array
    int sum = 0;
    for (int i = 0; i < 5; i++) {
        sum += scores[i];
        printf("scores[%d] = %d\n", i, scores[i]);
    }

    printf("\nTotal: %d\n", sum);
    printf("Average: %.1f\n", (float)sum / 5);

    return 0;
}
terminal
output
First score:  85
Third score:  78
scores[0] = 85
scores[1] = 92
scores[2] = 78
scores[3] = 96
scores[4] = 88

Total: 439
Average: 87.8
💡 Arrays + Loops = Power Combo. The real value of arrays comes when paired with loops. You can process scores[i] for any i from 0 to n-1 — whether there are 5 elements or 5000, the code looks exactly the same.
⚠️ Array bounds — C doesn't check them! If you have int arr[5] and access arr[7], C won't give an error — it will silently read or write random memory. This causes bugs that are very hard to find. Always ensure your index is between 0 and size - 1.
practice & quiz
Q

Quick Quiz — Test Yourself

58 – 60 min
Question 1 of 5

What is the output of: for(int i=0; i<3; i++) printf("%d ", i); ?

Question 2 of 5

What happens if you forget break in a switch case?

Question 3 of 5

A function with return type void means:

Question 4 of 5

Given int arr[4] = {10, 20, 30, 40}; — what is arr[2]?

Question 5 of 5

Which loop is guaranteed to execute its body at least once?

Lesson Checklist

  • I can write if / else if / else chains
  • I understand all 6 relational operators
  • I know when to use switch vs if-else
  • I understand how a for loop works (init, condition, update)
  • I know the difference between while and do-while
  • I can write and call a function with parameters and return value
  • I understand void functions
  • I can declare, initialize and loop through an array
  • I know that array index starts at 0
  • I completed the quiz

Day 3 Preview

Coming up next
  • 🧵 Strings — char arrays, strlen, strcpy, strcat Day 3
  • 📌 Pointers — memory addresses, & and * operators Day 3
  • 🔁 Recursion — functions that call themselves Day 3
  • 🗂️ 2D Arrays — matrices and grids Day 3