(Learn C Programming for Beginners — Day 8)
Welcome to Week 2 of your 30-Day C Programming Adventure! 🚀
In Week 1, we built the foundation. We learned how to store data, talk to the user, and do basic math. But our programs were linear—they just ran from top to bottom, doing the same thing every time.
Today, we give your code a brain.
We are going to learn about Control Flow, specifically Conditional Statements. This allows your program to ask questions like "Is the user over 18?" or "Did they pass the exam?" and act differently based on the answer.
By the end of this post, you'll be able to write programs that can make smart decisions.
🎯 Goals for Today
- The
ifStatement — Executing code only when a condition is true. - The
elseStatement — What to do when the condition is false. - The
else ifLadder — Handling multiple options. - Nested Decisions — Decisions inside decisions.
- Build a "Ticket Booker" — A program that checks age and eligibility.
🤔 The if Statement: The Gatekeeper
The if statement is the simplest form of decision-making. It says: "If this condition is true, do this task. Otherwise, skip it."
Syntax:
if (condition) {
// Code to execute if condition is true (1)
}
Example:
int score = 85;
if (score > 50) {
printf("You passed!\n");
}
- If
scoreis 85,85 > 50is True, so it prints "You passed!". - If
scorewas 40, it would print nothing.
↔️ The else Statement: Plan B
What if you want to do something specific when the condition is false? That's where else comes in.
Syntax:
if (condition) {
// Code if true
} else {
// Code if false
}
Example:
int age = 16;
if (age >= 18) {
printf("You can vote!\n");
} else {
printf("You are too young to vote.\n");
}
🪜 The else if Ladder: Multiple Choices
Life isn't always just Yes or No. Sometimes you have many options.
Syntax:
if (condition1) {
// Do this if condition1 is true
} else if (condition2) {
// Do this if condition2 is true
} else {
// Do this if NONE of the above are true
}
Example: Grading System
int marks = 75;
if (marks >= 90) {
printf("Grade: A\n");
} else if (marks >= 80) {
printf("Grade: B\n");
} else if (marks >= 70) {
printf("Grade: C\n");
} else {
printf("Grade: F\n");
}
💻 Let's Code: The "Movie Ticket Booker"
Let's build a program that determines ticket prices based on age.
* Under 12: Child Price ($5)
* 12 to 60: Adult Price ($10)
* Over 60: Senior Price ($7)
Step 1: Create day8_decisions.c.
Step 2: Type this code:
/*
Project: Movie Ticket Booker
Description: Calculates ticket price based on age using if-else logic.
*/
#include <stdio.h>
int main() {
int age;
float price;
// 1. Get Input
printf("Welcome to the Cinema!\n");
printf("Please enter your age: ");
scanf("%d", &age);
// 2. Logic: Determine Price
if (age < 0) {
printf("Invalid age! Are you a time traveler?\n");
return 1; // Exit the program with an error code
} else if (age < 12) {
price = 5.00;
printf("Ticket Type: Child\n");
} else if (age <= 60) {
price = 10.00;
printf("Ticket Type: Adult\n");
} else {
price = 7.00;
printf("Ticket Type: Senior\n");
}
// 3. Output
printf("Please pay: $%.2f\n", price);
return 0;
}
Step 3: Compile and Run!
* gcc day8_decisions.c -o day8_decisions
* ./day8_decisions
🐢 Nested Ifs: Decisions Inside Decisions
You can put an if statement inside another if statement. This is called nesting.
if (hasTicket) {
if (hasPopcorn) {
printf("Enjoy the movie and snack!");
} else {
printf("Enjoy the movie!");
}
} else {
printf("Please buy a ticket first.");
}
Pro Tip: Avoid nesting too deep (like 5 levels down). It makes code hard to read. Use else if or logical operators (&&) where possible.
📝 Day 8 Summary
if: Checks a condition.else: The fallback plan.else if: Checks multiple conditions in sequence.- Nesting: Putting decisions inside decisions.
- Logic: Remember your operators (
>,<,==,&&,||) from Day 5!
🚀 Challenge for Tomorrow
Create a "Login System":
1. Define a correct PIN (e.g., const int CORRECT_PIN = 1234;).
2. Ask the user to enter a PIN.
3. If it matches, print "Access Granted".
4. If it doesn't match, print "Access Denied".
Next Up: Day 9: The Switch Statement — A cleaner way to handle complex menus and multiple choices!
Found this helpful? Clap 👏 and follow to keep up with the adventure!
Comments
Post a Comment