C++ If…Else Examples

Welcome to The Coding College! In this tutorial, we’ll explore a variety of examples to demonstrate the practical use of if…else statements in C++. These examples will help you understand how conditional statements can make your programs dynamic and interactive.

Example 1: Checking If a Number is Positive or Negative

#include <iostream>
using namespace std;

int main() {
    int number;

    cout << "Enter a number: ";
    cin >> number;

    if (number >= 0) {
        cout << "The number is positive." << endl;
    } else {
        cout << "The number is negative." << endl;
    }

    return 0;
}

Output:

Enter a number: -5  
The number is negative.

Example 2: Even or Odd

#include <iostream>
using namespace std;

int main() {
    int number;

    cout << "Enter a number: ";
    cin >> number;

    if (number % 2 == 0) {
        cout << "The number is even." << endl;
    } else {
        cout << "The number is odd." << endl;
    }

    return 0;
}

Output:

Enter a number: 8  
The number is even.

Example 3: Voting Eligibility

#include <iostream>
using namespace std;

int main() {
    int age;

    cout << "Enter your age: ";
    cin >> age;

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

    return 0;
}

Output:

Enter your age: 16  
You are not eligible to vote.

Example 4: Grading System

#include <iostream>
using namespace std;

int main() {
    int marks;

    cout << "Enter your marks: ";
    cin >> marks;

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

    return 0;
}

Output:

Enter your marks: 75  
Grade: C

Example 5: Finding the Largest Number

#include <iostream>
using namespace std;

int main() {
    int a, b;

    cout << "Enter two numbers: ";
    cin >> a >> b;

    if (a > b) {
        cout << "The first number is larger." << endl;
    } else if (b > a) {
        cout << "The second number is larger." << endl;
    } else {
        cout << "Both numbers are equal." << endl;
    }

    return 0;
}

Output:

Enter two numbers: 15 20  
The second number is larger.

Example 6: Temperature Check

#include <iostream>
using namespace std;

int main() {
    int temperature;

    cout << "Enter the temperature: ";
    cin >> temperature;

    if (temperature > 30) {
        cout << "It's hot outside!" << endl;
    } else if (temperature >= 15) {
        cout << "The weather is pleasant." << endl;
    } else {
        cout << "It's cold outside." << endl;
    }

    return 0;
}

Output:

Enter the temperature: 10  
It's cold outside.

Example 7: Divisibility Check

#include <iostream>
using namespace std;

int main() {
    int number;

    cout << "Enter a number: ";
    cin >> number;

    if (number % 3 == 0 && number % 5 == 0) {
        cout << "The number is divisible by both 3 and 5." << endl;
    } else if (number % 3 == 0) {
        cout << "The number is divisible by 3." << endl;
    } else if (number % 5 == 0) {
        cout << "The number is divisible by 5." << endl;
    } else {
        cout << "The number is not divisible by 3 or 5." << endl;
    }

    return 0;
}

Output:

Enter a number: 15  
The number is divisible by both 3 and 5.

Example 8: Leap Year Check

#include <iostream>
using namespace std;

int main() {
    int year;

    cout << "Enter a year: ";
    cin >> year;

    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
        cout << "The year is a leap year." << endl;
    } else {
        cout << "The year is not a leap year." << endl;
    }

    return 0;
}

Output:

Enter a year: 2024  
The year is a leap year.

Summary

The if…else statement is a fundamental tool for decision-making in C++. It allows your programs to react dynamically to different inputs and conditions.

Explore More at The Coding College

Dive into more C++ tutorials and examples at The Coding College. Learn to code with hands-on lessons tailored for beginners and advanced programmers alike!

What’s Next?

  • Learn about loops like for, while, and do...while for repetitive tasks.
  • Explore switch statements for handling multiple discrete cases.
  • Master functions to create reusable blocks of code.

Leave a Comment