×

Advanced Coding & Software Engineering Program

Duration: 1 Year (12 Months)

Join our premium 1-year program to master cutting-edge technologies and become an industry-ready Software Engineer!

Course Coverage

  • Languages: C, C++, Java, JavaScript, Python
  • Web Technologies: HTML, CSS, Bootstrap 5, MERN Stack, Full Stack Development
  • Databases: MySQL, MongoDB
  • Data Science Libraries: Pandas, NumPy
  • Development Tools: Visual Studio Code, IntelliJ IDEA, PyCharm, Postman, Git, GitHub
  • Cloud Platforms: Vercel, MongoDB Atlas

Program Highlights

  • Live Classes: Interactive sessions with real-time doubt resolution
  • Hands-On Sessions: Practical coding exercises to build real-world skills
  • Industry Experts: Learn from professionals with years of experience
  • Live Project: Work on real-world projects to apply your skills
  • Get Certificate: Earn a professional certificate upon program completion

Course Fee: Only ₹1020 / month
Limited Period Offer!

Conditional Statements in C++ – Complete Tutorial



Last Updated on: 6th Dec 2025 16:52:50 PM

1. Introduction to Conditional Statements in C++

In real-life decision making, we always take actions based on conditions:

  • If it is raining → take an umbrella

  • If marks ≥ 40 → pass

  • If balance is sufficient → allow transaction

Similarly, in programming, conditional statements allow a C++ program to make decisions and execute different blocks of code based on conditions.

 

Definition

Conditional statements in C++ are control flow statements that execute specific code blocks when a given condition is true and skip or execute an alternative block when the condition is false.

 

They help in:

  • Decision making

  • Branching logic

  • Writing dynamic and intelligent programs

 

2. Why Conditional Statements Are Important

Without conditional statements:

  • Programs would execute line-by-line only

  • No decision making

  • No real-world logic implementation

 

Uses of Conditional Statements

  • Checking eligibility (age, marks, salary)

  • Login authentication

  • Menu-driven programs

  • Validations (input checking)

  • Business logic implementation

 

3. Types of Conditional Statements in C++

C++ provides the following conditional statements:

  1. if statement

  2. if–else statement

  3. else–if ladder

  4. Nested if

  5. switch statement

  6. Conditional (Ternary) Operator ?:

Let’s study each one in detail with syntax, explanation, and examples.

 

4.  if  Statement

The if statement executes a block of code only if the given condition is true. If the condition is false, the code inside if is skipped.

 

Syntax :

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

 

Working

  • Condition inside parentheses is evaluated

  • If result is true (non-zero) → block executes

  • If false (0) → block is skipped

 

Example

#include <iostream>
using namespace std;

int main() {
    int age = 20;

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

    return 0;
}

 

Output

You are eligible to vote

 

 

5.  if–else  Statement

The if–else statement executes one block when the condition is true and another block when the condition is false.

 

Syntax

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

 

Example

#include <iostream>
using namespace std;

int main() {
    int number;
    cout << "Enter a number: ";
    cin >> number;

    if (number % 2 == 0) {
        cout << "Even Number";
    } else {
        cout << "Odd Number";
    }

    return 0;
}

 

Output

Enter a number: 7
Odd Number

 

 

6.  else–if  Ladder

An else-if ladder is used when multiple conditions need to be checked. The program executes only the first true condition.

 

Syntax

if (condition1)
{
    // code
}
else if (condition2)
{
    // code
}
else if (condition3)
{
    // code
}
else
{
    // default code
}

 

Example: Grade Calculation

#include <iostream>
using namespace std;

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

    if (marks >= 90) {
        cout << "Grade A";
    }
    else if (marks >= 75) {
        cout << "Grade B";
    }
    else if (marks >= 60) {
        cout << "Grade C";
    }
    else if (marks >= 40) {
        cout << "Grade D";
    }
    else {
        cout << "Fail";
    }

    return 0;
}

 

 

7.  Nested if  Statement

A nested if means placing one if statement inside another if or else block. It is used when a condition depends on another condition.

 

Syntax

if (condition1)
{
    if (condition2)
    {
        // code
    }
}

 

Example: Login Validation

#include <iostream>
using namespace std;

int main() {
    int password = 1234;
    int input;

    cout << "Enter password: ";
    cin >> input;

    if (input == password) {
        cout << "Password correct\n";

        if (password == 1234) {
            cout << "Welcome Admin";
        }
    } else {
        cout << "Incorrect Password";
    }

    return 0;
}

 

 

8.  switch  Statement

The switch statement is used to execute one block of code from multiple choices, based on the value of a variable or expression.

 

Rules

  • Works with int, char, enum

  • Case values must be constant

  • break prevents fall-through

 

Syntax

switch(variable)
{
    case value1:
        // code
        break;
    case value2:
        // code
        break;
    default:
        // code
}

 

Example: Calculator

#include <iostream>
using namespace std;

int main() {
    char op;
    int a, b;

    cout << "Enter operator (+, -, *, /): ";
    cin >> op;

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

    switch (op) {
        case '+': cout << a + b;
                  break;
        case '-': cout << a - b;
                  break;
        case '*': cout << a * b;
                  break;
        case '/': cout << a / b;
                  break;
        default: cout << "Invalid Operator";
    }

    return 0;
}

 

 

9. Conditional (Ternary) Operator ?:

The ternary operator is a short form of if–else, used for simple conditions in a single line.

 

Syntax

condition ? expression1 : expression2;

 

Example

#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 20;

    (a > b) ? cout << "A is greater" : cout << "B is greater";

    return 0;
}

 

10. Comparison of Conditional Statements

Statement Best Use Case
if Single condition
if–else Two choices
else–if ladder Multiple conditions
Nested if Dependent conditions
switch Menu-driven programs
Ternary Short conditions

 

11. Common Mistakes

❌ Missing { }
❌ Using = instead of ==
❌ Forgetting break in switch
❌ Incorrect condition ordering

 

12. Real-World Examples

  • Online exam result checking

  • ATM withdrawal validation

  • College admission eligibility

  • E-commerce discount logic

 

13. Conclusion

Conditional statements are the foundation of logic building in C++. Mastering them helps you:

  • Write intelligent programs

  • Handle real-life conditions

  • Prepare for coding interviews

  • Understand advanced topics like loops and functions

 

Strong grip on conditional statements = Strong programming skills

 

Keep practicing — you're doing amazing!

Happy Coding!    yes

 


Online - Chat Now
Let’s Connect

Inquiry Sent!

Your message has been successfully sent. We'll get back to you soon!

iKeySkills Logo