Lesson Progress
0%
Lesson  ยท  Arrays, Loops, Functions & Conditions

The Four Building Blocks of Every C Program

Arrays store data, loops repeat work, functions organize logic, and conditions make decisions. Almost every program you'll ever write is just these four ideas combined in different ways โ€” this lesson covers each one with two hands-on examples.

Arrays
Loops
Functions
Conditions
๐Ÿ“–

How These Four Ideas Fit Together

Think of a program like running a small shop: you need shelves to hold items (arrays), a way to check each shelf one by one (loops), a set of standard tasks like "ring up a sale" you can reuse (functions), and rules like "if stock is low, reorder" (conditions). Every C program โ€” no matter how complex โ€” is built from these same four pieces.

topic 1 โ€” arrays
1

Arrays โ€” Storing Many Values Under One Name

An array is a fixed-size, ordered collection of values of the same type, all sharing one name and accessed by index (starting at 0).
Example 1 ยท Store & Display Student Marks
student_marks.c
student_marks.c
C
#include <stdio.h>

int main() {
    int marks[5] = {78, 92, 65, 88, 74};

    printf("Student marks:\n");
    for (int i = 0; i < 5; i++)
        printf("Student %d: %d\n", i + 1, marks[i]);

    return 0;
}
terminal
output
Student marks:
Student 1: 78
Student 2: 92
Student 3: 65
Student 4: 88
Student 5: 74
Example 2 ยท Find the Maximum Mark (Arrays + Loop + Condition Together)
find_max_mark.c
find_max_mark.c
C
#include <stdio.h>

int main() {
    int marks[5] = {78, 92, 65, 88, 74};
    int highest = marks[0];   // assume the first is highest, then prove it wrong

    for (int i = 1; i < 5; i++) {
        if (marks[i] > highest)   // condition inside a loop over the array
            highest = marks[i];
    }

    printf("Highest mark: %d\n", highest);
    return 0;
}
terminal
output
Highest mark: 92
๐Ÿ’ก This is the "running maximum" pattern โ€” start with a guess, then let the loop correct it. It's used in almost every "find the biggest/smallest" problem you'll ever write.
topic 2 โ€” loops
2

Loops โ€” Repeating Work Without Repeating Code

A loop repeats a block of code โ€” for when you know how many times, while when you repeat until a condition changes.
Example 1 ยท Multiplication Table (for loop)
times_table.c
times_table.c
C
#include <stdio.h>

int main() {
    int n = 6;
    for (int i = 1; i <= 10; i++)
        printf("%d x %d = %d\n", n, i, n * i);
    return 0;
}
terminal โ€” shortened
output
6 x 1 = 6
6 x 2 = 12
...
6 x 10 = 60
Example 2 ยท Reverse a Number (while loop, unique digit-extraction pattern)
reverse_number.c
reverse_number.c
C
#include <stdio.h>

int main() {
    int n = 1234, reversed = 0, digit;

    while (n != 0) {
        digit = n % 10;              // peel off the last digit
        reversed = reversed * 10 + digit;   // build the reversed number
        n = n / 10;                // drop the digit we just used
    }

    printf("Reversed: %d\n", reversed);
    return 0;
}
terminal
output
Reversed: 4321
๐Ÿ’ก %10 and /10 are the classic digit-extraction pair: % 10 gets the last digit, / 10 removes it. This same pattern powers Armstrong/Strong number checks too.
topic 3 โ€” functions
3

Functions โ€” Reusable, Named Blocks of Logic

A function packages a task under one name, so you can call it whenever needed instead of retyping the logic.
Example 1 ยท A Function That Returns a Value
add_numbers.c
add_numbers.c
C
#include <stdio.h>

int add(int a, int b) {
    return a + b;
}

int main() {
    int result = add(15, 27);   // call the function, store what it returns
    printf("Sum: %d\n", result);
    return 0;
}
terminal
output
Sum: 42
Example 2 ยท A Function That Takes an Array Parameter
array_average.c
array_average.c
C
#include <stdio.h>

float average(int arr[], int size) {
    int sum = 0;
    for (int i = 0; i < size; i++)
        sum += arr[i];
    return (float)sum / size;   // cast so division isn't truncated
}

int main() {
    int marks[5] = {78, 92, 65, 88, 74};
    printf("Average: %.2f\n", average(marks, 5));
    return 0;
}
terminal
output
Average: 79.40
๐Ÿ’ก Notice size is passed separately. Arrays decay to pointers when passed to a function, so the function alone can't know how many elements it received โ€” that's why size is always passed alongside the array.
topic 4 โ€” conditions
4

Conditions โ€” Making Decisions in Code

Conditions let a program choose between different paths based on whether an expression is true or false.
Example 1 ยท Grade Calculator (if-else ladder)
grade_calculator.c
grade_calculator.c
C
#include <stdio.h>

int main() {
    int marks = 84;
    char grade;

    if (marks >= 90)      grade = 'A';
    else if (marks >= 75) grade = 'B';
    else if (marks >= 60) grade = 'C';
    else                     grade = 'F';

    printf("Marks: %d -> Grade: %c\n", marks, grade);
    return 0;
}
terminal
output
Marks: 84 -> Grade: B
Example 2 ยท Leap Year Checker (nested + logical conditions)
leap_year.c
leap_year.c
C
#include <stdio.h>

int main() {
    int year = 2024;
    int isLeap;

    // divisible by 4 AND (not divisible by 100 OR divisible by 400)
    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
        isLeap = 1;
    else
        isLeap = 0;

    printf(isLeap ? "%d is a leap year\n" : "%d is NOT a leap year\n", year);
    return 0;
}
terminal
output
2024 is a leap year
โš ๏ธ Century years are the tricky part. 2000 is a leap year, but 1900 is not โ€” because 1900 is divisible by 100 but not 400. This is exactly why the rule needs three checks combined with && and ||, not just one.
quiz
Q

Quick Quiz

Question 1 of 5

In the max-mark example, why start with highest = marks[0] instead of 0?

Question 2 of 5

In reverse_number.c, what does n % 10 do?

Question 3 of 5

Why does average() take a size parameter separately from the array?

Question 4 of 5

Why is 1900 not a leap year while 2000 is?

Question 5 of 5

Which best matches "loops" in the shop analogy from this lesson?

โœ“

Lesson Checklist

  • I can store and display values using an array
  • I can find the max/min value in an array using a loop + condition
  • I can write a for loop and a while loop for different situations
  • I can extract digits from a number using % and /
  • I can write a function that returns a value
  • I can write a function that takes an array + size as parameters
  • I can write an if-else ladder for grading
  • I can combine && and || for a multi-part condition like leap year
  • I completed the quiz