Conditionals — if, else if, else
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
#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
elsehas no condition — it runs only when all above are false
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | Equal to | a == b | true if equal |
| != | Not equal | a != b | true if different |
| > | Greater than | a > b | true if a bigger |
| < | Less than | a < b | true if a smaller |
| >= | Greater or equal | a >= b | true if a ≥ b |
| <= | Less or equal | a <= b | true if a ≤ b |
= 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.
Switch Statement
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.
#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; }
else — it runs when no case matches. Always include it to handle unexpected input gracefully.
Loops — for, while, do-while
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)
#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
#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; }
#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; }
| Loop | When to Use | Checks Condition | Min Runs |
|---|---|---|---|
| for | Known count of iterations | Before each run | 0 times |
| while | Unknown count, condition-driven | Before each run | 0 times |
| do-while | Menu loops, must run once | After each run | 1 time always |
break 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.
Functions — Write Once, Use Anywhere
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,voidfor 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
{ }
#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; }
8 + 5 = 13 Hello, Ananta! 42 is even
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 as the return type and skip the return statement.
Arrays — Store Multiple Values
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
0to4 - 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)
#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; }
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
scores[i] for any i from 0 to n-1 — whether there are 5 elements or 5000, the code looks exactly the same.
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.
Quick Quiz — Test Yourself
What is the output of: for(int i=0; i<3; i++) printf("%d ", i); ?
What happens if you forget break in a switch case?
A function with return type void means:
Given int arr[4] = {10, 20, 30, 40}; — what is arr[2]?
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
- 🧵 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