What is if-else Statement in C++?

An if-else statement allows us to handle both possibilities:

  • If the condition is true, run the if block
  • Else, run the else block

It is used when we want to choose between two paths. It helps the program decide what to do in different situations.

Syntax of if-else Statement:

if (condition) {
    // code if condition is true
}
else {
    // code if condition is false
}

How if-else Statement Works

  • First, the condition is evaluated inside the if statement.
  • If it is true, the if block executes.
  • If it is false, the else block executes.

Example

#include <iostream>
using namespace std;

int main() {
    int number = -5;

    if (number >= 0) {
        cout << "Positive number";
    } else {
        cout << "Negative number";
    }

    return 0;
}

Output:

Negative number

Explanation:

  • The condition number >= 0 is checked.
  • Since -5 is not greater than or equal to 0, the condition is false.
  • So, the else block executes.

Real-Life Example

  • If marks are greater than or equal to 40 → Pass
  • Else → Fail

Summary:

  • if-else is used for two-way decision making.
  • Only one block executes at a time.
  • It executes one block when condition is true and another when false.
  • Curly braces { } define blocks of code.

Welcome to ShikshaSanchar!

ShikshaSanchar is a simple and helpful learning platform made for students who feel stressed by exams, assignments, or confusing topics. Here, you can study with clarity and confidence.

Here, learning is made simple. Notes are written in easy English, filled with clear theory, code examples, outputs, and real-life explanations — designed especially for students like you who want to understand, not just memorize.

Whether you’re from school, college, or someone learning out of curiosity — this site is for you. We’re here to help you in your exams, daily studies, and even to build a strong base for your future.

Each note on this platform is carefully prepared to suit all levels — beginner to advanced. You’ll find topics explained step by step, just like a good teacher would do in class. And the best part? You can study at your pace, anytime, anywhere.

Happy Learning! – Team ShikshaSanchar