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.
Arrays โ Storing Many Values Under One Name
#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; }
Student marks: Student 1: 78 Student 2: 92 Student 3: 65 Student 4: 88 Student 5: 74
#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; }
Highest mark: 92
Loops โ Repeating Work Without Repeating Code
for when you know how many times, while when you repeat until a condition changes.#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; }
6 x 1 = 6 6 x 2 = 12 ... 6 x 10 = 60
#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; }
Reversed: 4321
% 10 gets the last digit, / 10 removes it. This same pattern powers Armstrong/Strong number checks too.Functions โ Reusable, Named Blocks of Logic
#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; }
Sum: 42
#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; }
Average: 79.40
size is always passed alongside the array.Conditions โ Making Decisions in Code
#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; }
Marks: 84 -> Grade: B
#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; }
2024 is a leap year
&& and ||, not just one.Quick Quiz
In the max-mark example, why start with highest = marks[0] instead of 0?
In reverse_number.c, what does n % 10 do?
Why does average() take a size parameter separately from the array?
Why is 1900 not a leap year while 2000 is?
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