Conditional Statements

Conditional statements in C++ are programming constructs that allow the execution of different blocks of code based on the evaluation of conditions. They help control the flow of execution in a program by determining whether certain blocks of code should be executed or skipped.

There are primarily two types of conditional statements in C++:

  1. if-else Statement
  2. Switch Statement

If-Else Statements

The if-else statement in C++ allows you to execute a block of code based on the evaluation of a condition. If the condition is true, the code inside the if block is executed; otherwise, the code inside the else block is executed.

Here's the general syntax:

if (condition) {
    // Code to execute if the condition is true
} else {
    // Code to execute if the condition is false
}

If the condition specified inside the if parentheses evaluates to true, the code block inside the if statement is executed. If the condition evaluates to false, the code block inside the else statement (if present) is executed.

Example:-

#include<iostream>
using namespace std;
 
int main(){
    int x = 10;
    if (x > 5) {
        cout << "x is greater than 5" << endl;
    } else {
        cout << "x is not greater than 5" << endl;
    }
    return 0;
}

Output:-

x is greater than 5

Else-If

The else if statement is an extension of the if-else statement in C++. It allows you to specify additional conditions to be evaluated if the preceding if condition (or any previous else if condition) evaluates to false. This allows for more complex decision-making within your code.

Here's the general syntax of the else if statement:

if (condition1) {
    // Code to execute if condition1 is true
} else if (condition2) {
    // Code to execute if condition1 is false and condition2 is true
} else {
    // Code to execute if both condition1 and condition2 are false
}

You can have multiple else if blocks after an initial if statement. Each else if block is only evaluated if all preceding conditions (including the if condition and any previous else if conditions) are false.

If-Else Statement

Code Example:

#include <iostream>
using namespace std;
 
int main() {
    int num = 10;
 
    if (num > 10) {
        cout << "Number is greater than 10" << endl;
    } else if (num == 10) {
        cout << "Number is equal to 10" << endl;
    } else {
        cout << "Number is less than 10" << endl;
    }
 
    return 0;
}

Output:

Number is equal to 10

Nested If-Else

Topic asked in July 2022 (CBCS) and December 2022 (CBCS) question paper.

Nested if-else statements are used when you need to check multiple conditions sequentially, where one condition depends on the outcome of another condition. In C++, you can nest if statements within other if or else blocks to create more complex conditions. This nesting can occur within both if and else portions of the main if statement.

Here's the general structure of nested if statements:

if(condition1)
{
    // Code to be executed 
    if(condition2)
    {
        // Code to be executed
    }
    else
    {
         // Code to be executed
    }
}
else
{
    // code to be executed
}
Nested If-Else Statement

Code Example:- Check the greatest of three numbers

#include <iostream> 
using namespace std; 
  
int main() 
{ 
    // Declaring three numbers 
    int x = 9; 
    int y = 7; 
    int z = 5; 
  
    // Outermost if else 
    if (x < y) { 
        // Nested if else 
        if (z < y) { 
            cout << y << " is the greatest" << endl;
        } 
        else { 
            cout << z << " is the greatest" << endl;
        } 
    } 
    else { 
        // Nested if else 
        if (z < x) { 
            cout << x << " is the greatest" << endl;
        } 
        else { 
            cout << z << " is the greatest" << endl;
        } 
    } 
  
    return 0; 
}

Output:-

9 is the greatest

Explanation:-

  • Three integers x, y, and z are declared with values 9, 7, and 5 respectively.
  • The outermost if-else statement checks if x is less than y.
  • Since x (9) is greater than y (7), the else block is executed.
  • Inside the else block, another if-else statement checks if z is less than x.
  • Since z (5) is less than x (9), the if block is executed, and it prints "9 is the greatest".

Switch Statements

Topic asked in February 2022 (CBCS) and July 2022 (CBCS) question paper.

In C++, a switch statement is a control flow statement used to select one of many code blocks to be executed based on the value of an expression. It's particularly useful when you have multiple cases to handle and want a more elegant alternative to using nested if statements.

Here's the basic syntax of a switch statement in C++:

switch (expression) {
    case value1:
        // code to be executed if expression equals value1
        break;
    case value2:
        // code to be executed if expression equals value2
        break;
    // more cases can be added as needed
    default:
        // code to be executed if expression doesn't match any case
}

Here's a breakdown of each component:

  • expression: This is typically a variable or an expression whose value will be compared against the values specified in the case labels.

  • case value1: This is a label that specifies one of the possible values of the expression. If the value of the expression matches value1, the code block following this label will be executed.

  • break: This statement is used to exit the switch block. If a break statement is not included, execution will "fall through" to the next case, executing its code block as well.

  • default: This is an optional label that provides a default case in case none of the specified cases match the value of the expression. It is executed when none of the case labels match the expression.

Switch Statement

Here's a simple example in C++ to illustrate how a switch statement works:

#include <iostream>
using namespace std;
 
int main() {
    char grade;
    cout << "Enter your grade (A, B, C, D, or F): ";
    cin >> grade;
 
    switch (grade) {
        case 'A':
            cout << "Excellent! You scored very well!\n";
            break;
        case 'B':
            cout << "Good job! You performed above average.\n";
            break;
        case 'C':
            cout << "You passed. Keep up the good work.\n";
            break;
        case 'D':
            cout << "You barely passed. Consider studying more.\n";
            break;
        case 'F':
            cout << "You failed. You need to improve.\n";
            break;
        default:
            cout << "Invalid grade entered.\n";
    }
 
    return 0;
}

Output:-

Enter your grade (A, B, C, D, or F): A
Excellent! You scored very well!

Nested Switch Case

Nested switch cases in C++ refer to the situation where a switch statement is nested within another switch statement. This is useful when you need to make a decision based on multiple criteria, with each criterion having multiple options.

Here's an example of nested switch cases:

#include <iostream>
using namespace std;
 
int main() {
    int option1, option2;
 
    cout << "Enter option 1: ";
    cin >> option1;
    cout << "Enter option 2: ";
    cin >> option2;
 
    switch (option1) {
        case 1:
            switch (option2) {
                case 1:
                    cout << "Option 1 and Option 2 both are 1\n";
                    break;
                case 2:
                    cout << "Option 1 is 1 and Option 2 is 2\n";
                    break;
                default:
                    cout << "Option 1 is 1 and Option 2 is neither 1 nor 2\n";
            }
            break;
        case 2:
            switch (option2) {
                case 1:
                    cout << "Option 1 is 2 and Option 2 is 1\n";
                    break;
                case 2:
                    cout << "Option 1 and Option 2 both are 2\n";
                    break;
                default:
                    cout << "Option 1 is 2 and Option 2 is neither 1 nor 2\n";
            }
            break;
        default:
            cout << "Option 1 is neither 1 nor 2\n";
    }
 
    return 0;
}

Output:-

Enter option 1: 1 
Enter option 2: 2
Option 1 is 1 and Option 2 is 2

Let's break down how this nested switch case works:

  1. The program prompts the user to enter two options.
  2. The first switch statement checks the value of option1.
  3. Inside each case of the outer switch statement, there's another switch statement nested, which checks the value of option2.
  4. Depending on the combination of option1 and option2, the program prints a corresponding message.
How's article quality?

Last updated on -

Page Contents