C++ If… Else.. Statement

The if statement in C++ allows you to control the flow of your program based on certain conditions. The basic syntax of an if statement is as follows:

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

The condition in the parenthesis can be any expression that evaluates to a Boolean value (true or false). If the condition is true, the code inside the curly braces will be executed. If the condition is false, the code will be skipped.

You can also use the else statement to specify code that should be executed if the condition in the if statement is false. The syntax for an if...else statement is as follows:

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

It’s also possible to chain multiple if...else statements together using the else if statement. This allows you to check multiple conditions in a single block of code. The syntax for an if...else if...else statement is as follows:

if (condition1) {
   // code to be executed if condition1 is true
} else if (condition2) {
   // code to be executed if condition1 is false and condition2 is true
} else {
   // code to be executed if condition1 and condition2 are false
}

Here is an example that uses an if...else statement to check if a number is positive or negative:

#include <iostream>
using namespace std;

int main() {
   int num = 0;
   cout << "Enter a number: ";
   cin >> num;

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

   return 0;
}

The output of this program will be:

Enter a number: 5
The number is positive.

And,

Enter a number: -5
The number is negative.

In this example, the program prompts the user to enter a number and stores it in the variable num. The if statement then checks if the value of num is greater than 0. If it is, the program prints “The number is positive.” If it is not, the program prints “The number is negative.”

We can also use if...else if...else statement to check for multiple conditions. Here is an example:

#include <iostream>
using namespace std;

int main() {
    int num;
    cout ​`oaicite:{"index":0,"invalid_reason":"Malformed citation << \"Enter a number: \";\n cin >>"}`​ num;

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

    return 0;
}

In this example, the program first checks if the value of num is greater than 0. If it is, it prints “The number is positive.” If it is not, it then checks if the value of num is equal to 0. If it is, it prints “The number is zero.” If it is not, it then goes to the else statement and prints “The number is negative.”

It is also important to note that the else if statement is only executed if the previous if statement is false. The program will not check the conditions in the else if statement unless the previous if statement is false. This is known as short-circuit evaluation.

In addition, we can also use switch statement as an alternative to if...else if...else statement, which can make the code more readable and easier to maintain. Here is an example:

#include <iostream>
using namespace std;

int main() {
    int num;
    cout ​`oaicite:{"index":1,"invalid_reason":"Malformed citation << \"Enter a number: \";\n cin >>"}`​ num;

    switch (num) {
        case 1:
            cout << "The number is 1." << endl;
            break;
        case 2:
            cout << "The number is 2." << endl;
            break;
        default:
            cout << "The number is not 1 or 2." << endl;
    }

    return 0;
}

In this example, the program uses a switch statement to check the value of num. If the value of num is 1, the program will execute the statement “The number is 1.” If the value of num is 2, the program will execute the statement “The number is 2.” If the value of num is neither 1 nor 2, the program will execute the statement “The number is not 1 or 2.” The break statement is used to exit the switch statement and prevent any further execution of the following cases.

In conclusion, if...else and if...else if...else statements are used to make decisions in C++ based on certain conditions. switch statement can also be used as an alternative to if...else if...else statement. It is important to understand the usage of each statement and choose the appropriate one for the specific task at hand. With the proper usage of these control flow statements, you can add logic and decision-making capabilities to your C++ programs, making them more powerful and efficient.

Example Output:

Enter a number: -5
The number is negative.
Enter a number: 10
The number is positive.
Enter a number: 5
The number is 5.

In this example, we can see how the if...else if...else statement is used to check for multiple conditions, and the switch statement is used as an alternative. Both statements have their own specific use cases and it is important to choose the appropriate one for the task at hand. With the proper usage of these control flow statements, you can add logic and decision-making capabilities to your C++ programs, making them more powerful and efficient.

Leave a Reply

Your email address will not be published. Required fields are marked *