if-else in C++

The if-else decision-making statement allows us to make a decision based on the evaluation of a given condition. If the given condition evaluates to true then the code inside the ‘if’ block is executed and in case the condition is false, the code inside the ‘else’ block is executed.

Syntax of if-else in C++

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

Flowchart of if-else in C++

Flow diagram of if else

Example of if-else in C

The below example demonstrates the use of an if-else statement to find if the given number is positive or nonpositive.

C++




// C++ program to find if the given number is positive or
// non positive
  
#include <iostream>
using namespace std;
int main()
{
    int num = 5;
  
    // Using if-else to determine if the number is positive
    // or non positive
    if (num > 0) {
        cout << "number is positive." << endl;
    }
    else {
        cout << "number is non-positive." << endl;
    }
    return 0;
}


Output

number is positive.

Decision Making in C++

Decision-making in C++ involves the usage of conditional statements (also called decision control statements) to execute specific blocks of code primarily based on given situations and their results.

So basically, in decision-making, we evaluate the conditions and make a decision about which part of the code should be executed or not. It allows selective code execution which is crucial for controlling the flow of a program and making it more dynamic.

 

Similar Reads

Types of Decision-Making Statements in C++

In C++, the following decision-making statements are available:...

1. if in C++

In C++, the if statement is the simplest decision-making statement. It allows the execution of a block of code if the given condition is true. The body of the ‘if’ statement is executed only if the given condition is true....

2. if-else in C++

...

3. if-else if Ladder in C++

The if-else decision-making statement allows us to make a decision based on the evaluation of a given condition. If the given condition evaluates to true then the code inside the ‘if’ block is executed and in case the condition is false, the code inside the ‘else’ block is executed....

4. Nested if-else in C++

...

5. Switch Statement in C++

The if-else-if statements allow us to include additional situations after the preliminary if condition. The ‘else if’ condition is checked only if the above condition is not true. , and the `else` is the statement that will be executed if none of the above conditions is true. If some condition is true, then no only the associated block is executed....

6. Ternary Operator ( ? : ) in C++

...

7. Jump Statements in C++

The Nested if-else statement contains an ‘if’ statement inside another ‘if’ statement. This structure lets in more complex selection-making by way of comparing multiple conditions. In this type of statement, multiple conditions are checked, and then the body of the last if statement is executed....

Conclusion

...