Skip to main content

C Programming Day 8: Making Decisions - If, Else-If, and Else

(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

  1. The if Statement — Executing code only when a condition is true.
  2. The else Statement — What to do when the condition is false.
  3. The else if Ladder — Handling multiple options.
  4. Nested Decisions — Decisions inside decisions.
  5. 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 score is 85, 85 > 50 is True, so it prints "You passed!".
  • If score was 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!

CProgramming #CodingForBeginners #LearnToCode #Programming #Developer

Comments

Popular posts from this blog

Unlock C Programming in 30 Days

Your 30‑Day C Programming Series Click any day below to jump straight to that lesson (opens in a new tab): Day 0: Unlock C Programming in 30 Days Your ultimate self‑study guide introduction—your roadmap for the entire 30‑day journey. Read Day 0 Day 1: What is Programming & Setting Up Your C Lab Understand programming basics and install GCC + VS Code so you’re ready to code. Read Day 1 Day 2: Write Your First “Hello, World!” Learn the anatomy of a C program and compile & run your very first C code . Read Day 2 Day 3: Variables & Basic Data Types Discover how to store and print integers, floats, and characters in C . Read Day 3 Day 4: Interactive Input with scanf Make your programs listen—read numbers, floats, and chars from the user. Read Day 4

C Programming Day 3: Variables, Data Types & Constants - The "Boxes" of Programming

(Learn C Programming for Beginners — Day 3) Welcome back to your 30-Day C Programming Adventure ! 🚀 Yesterday, you wrote your very first "Hello, World!" program. You felt the power of making the computer speak. Today, we’re going to give your programs a memory. Imagine trying to cook a complex meal without any bowls or containers. You’d have ingredients everywhere! In programming, variables are those containers. They hold your data so you can use, change, and display it. Today, we unlock the power of Variables , Data Types , and Constants . By the end of this post, you'll be able to write programs that can store and manipulate information, not just print static text. 🎯 Goals for Today Understand Variables — What they are and how to create them. Master Data Types — Integers, floats, characters, and more. Learn Constants — Values that never change. Write Code — Create a program that stores and prints different types of data. 📦 What is a Varia...

C Programming Day 4: Input/Output - Talking to Your Computer

(Learn C Programming for Beginners — Day 4) Welcome to Day 4 of your 30-Day C Programming Adventure ! 🚀 So far, our programs have been a bit... quiet. We've told the computer what to store (variables) and what to show us ( printf ), but we haven't let the user say anything back. It's been a one-way conversation. Today, that changes. We are going to make your programs interactive . By the end of this post, you'll be able to write programs that ask questions, listen for answers, and respond accordingly. We're diving deep into printf (Output) and its best friend, scanf (Input). 🎯 Goals for Today Master printf — Learn how to format output beautifully. Unlock scanf — Learn how to take input from the user. Handle Common Pitfalls — Avoid the dreaded "skipped input" bug. Build an Interactive Program — Create a " Age Calculator " that talks to you. 📢 The Art of Speaking: printf Revisited You've used printf to print...