Day 4 How to Use If Else in C++ (Beginner Tutorial)

If you’ve been following along with our beginner series, welcome to Day 4! In Day 3, you learned how to work with variables, data types, How to Use Variables in C++: A Beginner’s Guide to Data Types and cin and take input using cin. Now, it’s time to give your code some decision-making power.

This tutorial will show you how to use if else in C++ one of the most important concepts in programming. These conditional statements allow your program to make decisions and respond differently based on input or logic.

In this post, you’ll:

Learn how conditional logic works

Understand if, else if, and else

Practice writing code examples

See line-by-line breakdowns so you don’t miss a thing

Let’s get started!

What Are Conditional Statements?

Conditional statements help your program make decisions. In simple terms:

“If something is true, do this. Otherwise, do that.”

You use conditions every day without realizing it:

If you’re hungry, you eat.

Else, you keep working.

If it’s raining, take an umbrella.

Else, don’t bother.

Similarly, in C++, we use the if, else if, and else keywords to implement this logic.

Basic Syntax of if Statement

Here’s what a simple if block looks like:

if (condition) {
// code to run if condition is true
}

Example: Checking Age

Let’s write a program that checks if someone is old enough to vote.

#include <iostream>
using namespace std;

int main() {
    int age;
    cout << "Enter your age: ";
    cin >> age;

    if (age >= 18) {
        cout << "You are eligible to vote.";
    }

    return 0;
}

Line-by-Line Breakdown:

include <iostream> → Lets you use cout and cin

int age; → Declares a variable to store user’s age

cin >> age; → Takes input from user

if (age >= 18) → Checks if user is 18 or older

If the condition is true, it prints the message

If not, the program does nothing (yet!)

Adding else: Provide an Alternative Path

What if we want to display a message even when the user is not eligible?

We use else:

if (age >= 18) {
cout << "You are eligible to vote.";
} else {
cout << "Sorry, you are not eligible.";
}

Now your code gives feedback in both situations.

Add More Logic with else if

Sometimes, you want to check multiple conditions. That’s where else if comes in.

if (age >= 18) {
cout << "You are eligible to vote."; } else if (age >= 13) {
cout << "You’re a teenager, but not eligible yet.";
} else {
cout << "You’re just a kid!";
}

Let’s Try Another Example: Grade Checker

Let’s write a program that prints a grade based on marks entered by the user.

#include <iostream>
using namespace std;

int main() {
    int marks;
    cout << "Enter your marks: ";
    cin >> marks;

    if (marks >= 90) {
        cout << "Grade: A";
    } else if (marks >= 80) {
        cout << "Grade: B";
    } else if (marks >= 70) {
        cout << "Grade: C";
    } else if (marks >= 60) {
        cout << "Grade: D";
    } else {
        cout << "Grade: F";
    }

    return 0;
}

Explanation:

This checks multiple conditions in order.

If one condition is true, it executes that block and skips the rest.

If none match, the final else runs.

This is exactly how online forms or tests assign grades behind the scenes.

Tips for Writing if else in C++

1. Use parentheses around your conditions

2. Always use { } even for one line — safer and cleaner

3. Use == to check equality, not =

4. Keep logic simple for readability

5. Don’t over-nest too many else if use switch when needed

Using Boolean with If Else

You can also use boolean variables inside if:

bool isRainy = true;

if (isRainy) {
cout << "Take an umbrella!";
} else {
cout << "Enjoy the sun!";
}

Practice Challenge for You:

Try writing a program that:

Asks user for a number

Prints:

“Even” if it’s divisible by 2

“Odd” otherwise

Hint: Use % (modulo operator) and if else.

Summary: What You Learned

• How to use if else in C++

• Syntax for if, else, and else if

• When and how to use comparison operators

• Avoiding common mistakes

Conditional logic is one of the core powers of any programming language and you just unlocked it!

What’s Next?

In Day 5, you’ll learn about Loops where you repeat tasks without writing the same code again and again. This is how calculators, counters, and games work.

Stay tuned — it’s about to get fun!

Leave a Comment