Skip to main content

Command Palette

Search for a command to run...

if–else, else if, Logical Operators & User Input

Published
5 min read
D

Heya! 👋 I love helping people, and one of the best ways I do this is by sharing my knowledge and experiences. My journey reflects the power of growth and transformation, and I’m here to document and share it with you.

I started as a pharmacist, practicing at a tertiary hospital in the Northern Region of Ghana. There, I saw firsthand the challenges in healthcare delivery and became fascinated by how technology could offer solutions. This sparked my interest in digital health, a field I believe holds the key to revolutionizing healthcare.

Determined to contribute, I taught myself programming, mastering tools like HTML, CSS, JavaScript, React, PHP, and more. But I craved deeper knowledge and practical experience. That’s when I joined the ALX Software Engineering program, which became a turning point. Spending over 70 hours a week learning, coding, and collaborating, I transitioned fully into tech.

Today, I am a Software Engineer and Digital Health Solutions Architect, building and contributing to innovative digital health solutions. I combine my healthcare expertise with technical skills to create impactful tools that solve real-world problems in health delivery.

Imposter syndrome has been part of my journey, but I’ve learned to embrace it as a sign of growth. Livestreaming my learning process, receiving feedback, and building in public have been crucial in overcoming self-doubt. Each experience has strengthened my belief in showing up, staying consistent, and growing through challenges.

Through this platform, I document my lessons, challenges, and successes to inspire and guide others—whether you’re transitioning careers, exploring digital health, or diving into software development.

I believe in accountability and the value of shared growth. Your feedback keeps me grounded and motivated to continue this journey. Let’s connect, learn, and grow together! 🚀

💡
I recently started teaching a course on C programming and as part of my preparation for class, I usually write out lesson notes that I will be teaching from. I have therefore decided to make these into blog posts so anyone else learning C can also benefit from them. Therefore, this lesson is part of the series I am calling: C Programming for Absolute Beginners. I hope you enjoy it. Ask any questions or leave comments if you need further clarification or want to make a suggestion.

In the last lesson, your program learned how to make a simple decision using if.

But what if you want the program to do something when the condition is false?

Or what if you want it to check multiple conditions?

That’s what we’ll learn today.

By the end of this lesson, you’ll know how to:

  • Use if–else

  • Use else if for multiple decisions

  • Combine conditions using logical operators

  • Take user input using scanf

Let’s build on what you already know.

1. The if–else Statement

An if statement only runs code when the condition is true.

But sometimes you want two possible outcomes:

  • One if true

  • One if false

That’s where else comes in.

Syntax

if (condition) {
    // runs if true
} else {
    // runs if false
}

2. Example: Pass or Fail

#include <stdio.h>

int main() {
    int score = 45;

    if (score >= 50) {
        printf("You passed!\n");
    } else {
        printf("You failed.\n");
    }

    return 0;
}

Explanation:

  • If score is 50 or more → “You passed!”

  • Otherwise → “You failed.”

Now the program always prints something.

3. The else if Statement

What if you want more than two outcomes?

For example, grading:

  • 80 and above → Grade A

  • 60–79 → Grade B

  • 50–59 → Grade C

  • Below 50 → Fail

You can use else if.

Syntax

if (condition1) {
    // runs if condition1 is true
} else if (condition2) {
    // runs if condition2 is true
} else {
    // runs if none are true
}

4. Example: Grading System

#include <stdio.h>

int main() {
    int score = 75;

    if (score >= 80) {
        printf("Grade A\n");
    } else if (score >= 60) {
        printf("Grade B\n");
    } else if (score >= 50) {
        printf("Grade C\n");
    } else {
        printf("Fail\n");
    }

    return 0;
}

Important:

C checks conditions from top to bottom.

As soon as one condition is true:

  • It runs that block

  • It skips the rest

So order matters.

5. Logical Operators

Sometimes one condition is not enough.

You may want:

  • Age between 18 and 30

  • Score greater than 50 OR equal to 0

Logical operators help combine conditions.

AND – &&

Both conditions must be true.

Example:

if (age >= 18 && age <= 30) {
    printf("Young adult\n");
}

This only runs if age is between 18 and 30.

OR – ||

Only one condition must be true.

Example:

if (score >= 90 || score == 0) {
    printf("Special case\n");
}

If either part is true, the block runs.

NOT – !

Reverses a condition.

Example:

if (!(age >= 18)) {
    printf("Not an adult\n");
}

This means: if age is NOT 18 or more.

6. Taking User Input with scanf

So far, we have hardcoded values.

But real programs ask users for input.

We use scanf to read input.

Basic Example

#include <stdio.h>

int main() {
    int age;

    printf("Enter your age: ");
    scanf("%d", &age);

    if (age >= 18) {
        printf("You are an adult.\n");
    } else {
        printf("You are not an adult.\n");
    }

    return 0;
}

Important:

  • %d is for integers.

  • &age gives the memory address.

  • C needs the address to store the input.

Do not forget the &.

7. Mini Project: Age Category Program

Let’s combine everything.

#include <stdio.h>

int main() {
    int age;

    printf("Enter your age: ");
    scanf("%d", &age);

    if (age < 13) {
        printf("Child\n");
    } else if (age <= 19) {
        printf("Teen\n");
    } else {
        printf("Adult\n");
    }

    return 0;
}

This program:

  • Takes input

  • Checks multiple conditions

  • Prints the correct category

You are now writing real logic.

8. Common Beginner Mistakes

  1. Putting conditions in the wrong order

  2. Forgetting curly braces

  3. Forgetting & in scanf

  4. Writing overlapping conditions

Example of wrong order:

if (score >= 50)

placed before

else if (score >= 80)

This would prevent Grade A from ever happening.

Always go from highest condition to lowest.

9. Practice Exercises

  1. Write a program that:

    • Asks for temperature

    • Prints “Cold” if below 15

    • Prints “Warm” if 15–30

    • Prints “Hot” if above 30

  2. Write a program that:

    • Asks for a number

    • Prints “Even” if divisible by 2

    • Prints “Odd” otherwise

  3. Write a grading program using user input.

Final Thoughts

You now know how to:

  • Use if

  • Use if–else

  • Use else if

  • Combine conditions

  • Take user input

At this point, your programs can:

  • Store data

  • Make decisions

  • Respond to users

That’s a big step for a beginner.

Next, we’ll move into loops and that’s when your programs start repeating actions automatically.

More from this blog

D

Dr. Ehoneah Obed

78 posts

Software engineer writing about systems: in code, in learning, in life. I reverse-engineer complex problems into frameworks. Pharmacist → SWE → Founder.