allizwell.jobsdoor360.in

Conditional Statements

CS & IT / Data Structures and Algorithms / Conditional Statements

Conditional Statements in C++

In programming, we often need to make decisions based on certain conditions. For example, a program may check whether a number is positive or negative, whether a student has passed an examination, or whether a person is eligible to vote.

Conditional statements allow a program to make decisions and execute different blocks of code depending on whether a condition is true or false.

1. What Are Conditional Statements?

Conditional statements are instructions that control the flow of a program by checking whether a particular condition is satisfied.

If the condition is true, one block of code executes. If it is false, another block may execute.

For example, consider a program that checks whether a person is eligible to vote.

int age = 20;

if (age >= 18) {
    cout << "Eligible to vote";
}

Since the age is 20, the condition age >= 18 is true, so the message is displayed.

2. Types of Conditional Statements

C++ provides the following commonly used conditional statements:

  • if statement
  • if-else statement
  • if-else-if ladder
  • Nested if statement
  • switch statement

3. The if Statement

The if statement executes a block of code only when the given condition is true.

Syntax

if (condition) {
    // Code executes if condition is true
}

Example

#include <iostream>
using namespace std;

int main() {
    int age = 20;

    if (age >= 18) {
        cout << "You can vote";
    }

    return 0;
}

Output:

You can vote

The condition is true because 20 is greater than or equal to 18. Therefore, the statement inside the if block executes.

4. The if-else Statement

The if-else statement is used when we want to execute one block of code if a condition is true and another block if it is false.

Syntax

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

Example: Check Even or Odd

#include <iostream>
using namespace std;

int main() {
    int number = 7;

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

    return 0;
}

Output:

Odd number

The modulus operator (%) gives the remainder after division. If a number is divisible by 2, it is even. Otherwise, it is odd.

5. The if-else-if Ladder

An if-else-if ladder is used when we need to check multiple conditions.

The conditions are checked from top to bottom. As soon as a true condition is found, its block executes and the remaining conditions are skipped.

Syntax

if (condition1) {
    // Code for condition1
}
else if (condition2) {
    // Code for condition2
}
else if (condition3) {
    // Code for condition3
}
else {
    // Code if all conditions are false
}

Example: Grading System

#include <iostream>
using namespace std;

int main() {
    int marks = 85;

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

    return 0;
}

Output:

Grade B

Since 85 is greater than or equal to 80, the second condition is true. Therefore, Grade B is displayed.

6. Nested if Statement

A nested if statement means placing one if statement inside another if statement.

It is useful when a decision depends on more than one condition, and the second condition should only be checked after the first one is true.

Example: Check Eligibility

#include <iostream>
using namespace std;

int main() {
    int age = 22;
    bool hasLicense = true;

    if (age >= 18) {
        if (hasLicense) {
            cout << "You can drive";
        }
        else {
            cout << "You need a driving license";
        }
    }
    else {
        cout << "You are not eligible to drive";
    }

    return 0;
}

Output:

You can drive

7. The switch Statement

The switch statement is used when we want to select one option from multiple possible values of an expression.

It is commonly used in menu-driven programs, calculators and programs involving fixed choices.

Syntax

switch (expression) {
    case value1:
        // Code
        break;

    case value2:
        // Code
        break;

    default:
        // Code if no case matches
}

Example: Day of the Week

#include <iostream>
using namespace std;

int main() {
    int day = 3;

    switch (day) {
        case 1:
            cout << "Monday";
            break;

        case 2:
            cout << "Tuesday";
            break;

        case 3:
            cout << "Wednesday";
            break;

        case 4:
            cout << "Thursday";
            break;

        case 5:
            cout << "Friday";
            break;

        default:
            cout << "Invalid day";
    }

    return 0;
}

Output:

Wednesday

Understanding break and default

  • break: Stops the execution of the switch statement after a matching case is executed.
  • default: Executes when none of the case values match. It is optional.

If break is omitted, execution continues into the next case. This behaviour is called fall-through.

8. Difference Between if-else and switch

if-else switch
Can check complex conditions. Checks an expression against fixed case values.
Supports relational and logical conditions. Uses matching case values.
Useful for ranges and multiple conditions. Useful for menus and fixed choices.
Does not require break statements. Usually uses break to prevent fall-through.

9. Common Mistakes

  • Using = instead of == when comparing two values.
  • Forgetting curly braces when writing multiple statements in a block.
  • Writing conditions in the wrong order in an if-else-if ladder.
  • Forgetting the break statement in a switch case.
  • Using a semicolon immediately after an if condition.

10. Practice Questions

  1. Write a program to check whether a number is positive, negative or zero.
  2. Write a program to find the largest of two numbers.
  3. Write a program to find the largest of three numbers.
  4. Write a program to check whether a year is a leap year.
  5. Write a program to check whether a person is eligible to vote.
  6. Write a program to calculate grades based on marks.
  7. Write a simple calculator using the switch statement.

11. Key Takeaways

  • Conditional statements help programs make decisions.
  • The if statement executes code when a condition is true.
  • The if-else statement provides two possible execution paths.
  • The if-else-if ladder checks multiple conditions.
  • Nested if statements allow one condition to be checked inside another.
  • The switch statement selects an option from multiple fixed choices.
  • break prevents unwanted fall-through in switch statements.
Scroll to Top