Conditional Statements (if-else, switch-case)

In any programming language, the ability to perform different actions based on different conditions is crucial. Conditional statements allow us to control the flow of our program and make it more dynamic and intelligent. In C++, we have two main types of conditional statements: if-else and switch-case.

If-Else Statements

The if-else statement is used when we want to execute a block of code if a condition is true, and another block of code if the condition is false. The basic syntax of the if-else statement is:

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

For example, let's say we want to write a program that checks whether a given number is positive or negative. We can use an if-else statement to achieve this:

int number = -7;

if (number >= 0) {
    cout << "The number is positive." << endl;
} else {
    cout << "The number is negative." << endl;
}

In this case, since number is less than zero, the output will be "The number is negative."

We can also use multiple if-else statements to handle different conditions. Here's an example:

int age = 25;

if (age < 12) {
    cout << "You are a child." << endl;
} else if (age < 18) {
    cout << "You are a teenager." << endl;
} else if (age < 30) {
    cout << "You are a young adult." << endl;
} else {
    cout << "You are an adult." << endl;
}

In this case, the output will be "You are a young adult," since the age falls within the range of 18 to 29.

Switch-Case Statements

The switch-case statement provides an alternative way to handle multiple conditions. It allows us to specify different actions for different values of a variable. The basic syntax of the switch-case statement is:

switch (variable) {
    case value1:
        // Code to be executed if variable equals value1
        break;
    case value2:
        // Code to be executed if variable equals value2
        break;
    // ... more cases
    default:
        // Code to be executed if variable doesn't match any case
}

Here's an example that demonstrates the usage of the switch-case statement:

char grade = 'B';

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

In this case, since grade is 'B', the output will be "Good job!"

Note that the break statement is important to prevent the execution from falling through to the next case. Without it, all the subsequent cases would be executed until a break statement is encountered or until the end of the switch block is reached.

Conclusion

Conditional statements are essential for controlling the flow of a C++ program based on different conditions. The if-else statement allows us to execute different blocks of code based on a condition, while the switch-case statement provides a way to handle multiple cases efficiently. By mastering these conditional statements, you can create more intelligent and flexible programs in C++.


noob to master © copyleft