Conditional Statements in cpp

Conditional Statements in cpp

Conditional statements in C++ allow a program to make decisions based on whether certain conditions are true or false. The most common conditional statements are if, else if, and else.

1) if statement

Here is the syntax for the if statement:

if (condition) {
    // code to be executed if condition is true
}

The condition is a Boolean expression that evaluates to either true or false. If the condition is true, the code within the curly braces will be executed. If the condition is false, the code will be skipped.

Here is an example of an if statement in C++:

#include <iostream>
using namespace std;

int main() {
    int x = 5;

    if (x < 10) {
        cout << "x is less than 10" << endl;
    }

    return 0;
}

In this example, the condition is "x < 10", which is true because x is 5. Therefore, the code within the curly braces will be executed, and the output will be "x is less than 10".

2) else if statement

Here is the syntax for the else if statement:

if (condition1) {
    // code to be executed if condition1 is true
} else if (condition2) {
    // code to be executed if condition2 is true
} else {
    // code to be executed if neither condition1 nor condition2 is true
}

In this case, if condition1 is true, the code within the first set of curly braces will be executed. If condition1 is false and condition2 is true, the code within the second set of curly braces will be executed. If both condition1 and condition2 are false, the code within the third set of curly braces will be executed.

Here is an example of an else if statement in C++:

#include <iostream>
using namespace std;

int main() {
    int x = 15;

    if (x < 10) {
        cout << "x is less than 10" << endl;
    } else if (x < 20) {
        cout << "x is between 10 and 19" << endl;
    } else {
        cout << "x is greater than or equal to 20" << endl;
    }

    return 0;
}

In this example, x is greater than or equal to 10 but less than 20, so the condition "x < 20" is true. Therefore, the code within the second set of curly braces will be executed, and the output will be "x is between 10 and 19".

3) else statement

Finally, here is the syntax for the else statement:

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

In this case, if the condition is true, the code within the first set of curly braces will be executed. If the condition is false, the code within the second set of curly braces will be executed.

Here is an example of an else statement in C++:

#include <iostream>
using namespace std;

int main() {
    int x = 15;

    if (x < 10) {
        cout << "x is less than 10" << endl;
    } else {
        cout << "x is greater than or equal to 10" << endl;
    }

    return 0;
}

In this example, x is greater than or equal to 10, so the condition "x < 10" is false. Therefore, the code within the second set of curly braces will be executed, and the output will be "x is greater than or equal to

4)Switch statement

The switch statement in C++ is used to execute different sections of code based on the value of an expression. It is commonly used as an alternative to a long chain of if-else statements.

Here is the syntax for a switch statement in C++:

switch (expression) {
    case value1:
        // code to be executed if expression is equal to value1
        break;
    case value2:
        // code to be executed if expression is equal to value2
        break;
    .
    .
    .
    case valueN:
        // code to be executed if expression is equal to valueN
        break;
    default:
        // code to be executed if expression is not equal to any of the values
        break;
}

The expression is evaluated and compared to each of the values specified in the case statements. If the expression matches one of the values, the code following that case statement will be executed. The break statement is used to terminate the switch statement.

If none of the case statements match the expression, the code following the default statement will be executed.

Here is an example of a switch statement in C++:

#include <iostream>
using namespace std;

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

    switch (grade) {
        case 'A':
            cout << "Excellent!" << endl;
            break;
        case 'B':
            cout << "Well done!" << endl;
            break;
        case 'C':
            cout << "Good job!" << endl;
            break;
        case 'D':
            cout << "You passed." << endl;
            break;
        case 'F':
            cout << "Better try again." << endl;
            break;
        default:
            cout << "Invalid grade." << endl;
            break;
    }

    return 0;
}

In this example, the value of the variable "grade" is 'B'. The switch statement compares this value to each of the case statements. Since the value matches the second case, the code within the second set of curly braces will be executed. The output will be "Well done!".