Conditional Statements in C++

 C++ provides us the opportunity to control the execution of our program through conditional statements. These statements are used to execute certain block of codes based on some conditions defined by the programmer. Primarily there are four types of conditional statements each with their own specifications and conditions. By understanding their syntax and best practices, developers can write more efficient and maintainable code.

 Types of Conditional Statements

 1. if Statement

The `if` statement is the most basic form of a conditional statement in C++. It allows us to execute a block of code only if a specified condition evaluates to true.

Syntax:

if (condition) {

    // Write your Code to execute if condition is true

}

Example:

#include <iostream>

int main() {

    int num = 7;

    // Check if num is greater than 0

    if (num > 0) {

        std::cout << "The number is positive." << std::endl;

    }

    return 0;

}

Output:

 2. if-else Statement

The `if-else` statement extends the functionality of the `if` statement by providing an alternative block of code to execute when the condition evaluates to false.

Syntax:

if (condition) {

    //  execute if condition is true

} else {

    // execute if condition is false

}

Example:

#include <iostream>

int main() {
    int num = -3;

    // Check if num is greater than 0
    if (num > 0) {
        std::cout << "The number is positive." << std::endl;
    }
// Check if num is smaller than 0
else
{
        std::cout << "The number is negative." << std::endl;
    }
    return 0;
}
Output:

 3. Nested if-else Statement

Nested `if-else` statements allow for multiple levels of conditions, providing greater control over the program's flow.

Syntax

if (condition1) {

    // execute if condition1 is true

    if (condition2) {

        //  execute if both condition1 and condition2 are true

    } else {

        // execute if condition1 is true but condition2 is false

    }

} else {

    //  execute if condition1 is false

}

Example:

#include <iostream>

int main() {
    int num = 10;

    if (num > 0) {
        std::cout << "The number is positive." << std::endl;

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

    return 0;
}
Output:

In the example above , firstly we will check if num is greater than 0. If it is, we print "The number is positive." Then, within this if block, we have another if-else statement to check whether the number is even or odd. If num % 2 == 0 evaluates to true, the number is even; otherwise, it's odd.

If num is not greater than 0, we move to the else if part and check if num is equal to 0. If it is, we print "The number is zero." Finally, if none of the above conditions are true, we print "The number is negative."

 4. Switch Statement

The `switch` statement provides an alternative way to handle multiple conditional branches based on the value of an expression.

Syntax:

switch (expression) {

    case value1:

        //  execute if expression equals value1

        break;

    case value2:

        //  execute if expression equals value2

        break;

    default:

        // execute if expression does not match any case

        break;

}

Example:

#include <iostream>

int main() {
    char grade = 'B';

    switch (grade) {
        case 'A':
            std::cout << "Excellent!" << std::endl;
            break;
        case 'B':
            std::cout << "Well done!" << std::endl;
            break;
        case 'C':
            std::cout << "Good job!" << std::endl;
            break;
        case 'D':
            std::cout << "Needs improvement." << std::endl;
            break;
        case 'F':
            std::cout << "Failed." << std::endl;
            break;
        default:
            std::cout << "Invalid grade." << std::endl;
    }

    return 0;
}

Output:

 Best Practices for Using Conditional Statements

  • Write clear and concise conditions to improve code readability so that everyone will be able to understand your code.
  • Excessive nesting can make code difficult to understand and maintain so try to keep it as simple as possible.
  •  Prefer `switch` statements for scenarios with multiple mutually exclusive options. It is better than if statement if you want to avoid chaos.
  •  Utilize conditional statements for error checking and handling to ensure robustness.

 Whether it's simple if-else constructs or intricate nested conditions, mastering conditional statements empowers developers to create sophisticated applications with ease.

Post a Comment

0 Comments