Last Updated : 12 Dec, 2024
The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false, it won’t. But what if we want to do something else if the condition is false. Here comes the C++ if else statement. We can use the else statement with if statement to execute a block of code when the condition is false.
Let's take a look at an example:
C++
#include <iostream>
using namespace std;
int main() {
int i = 10;
// If statement
if (i < 15) {
cout << "10 is less than 15";
}
// Else statement with the above if
else {
cout << "10 is not less than 15";
}
return 0;
}
10 is less than 15
Explanation: The condition in the if block checks if 10 is less than 15. Its true so the statement inside if block, “10 is less than 15” gets printed and else block is skipped. If the condition was false, all the statements inside the if block will be skipped and else block will be executed.
Syntaxif (condition) {
// Executes this block if
// condition is true
}
else {
// Executes this block if
// condition is false
}
The if statement condition can be anything that evaluates to a boolean value or a boolean converted value. We generally use relational and equality operator to specify the condition.
Working of if-else statementThe following are a few basic examples of the if-else statement that shows the use of the if-else statement in a C program.
Check for Odd and Even Number C++
#include <bits/stdc++.h>
using namespace std;
int main() {
int n = 7;
// if block executed when the number is even
if (n % 2 == 0)
cout << "Even";
// else block executed when the number is not even
else
cout << "Odd";
return 0;
}
You may have noticed that we skipped using the braces for the body of the if statement. If we do not provide the curly braces ‘{‘ and ‘}’ after if( condition ) then by default if statement will consider the immediate one statement to be inside its block.
Find Largest Among Three Numbers C++
#include <iostream>
using namespace std;
int main() {
int a = 1, b = 2, c = 11;
// Finding the largest by comparing using
// relational operators with if-else
if (a >= b) {
if (a >= c)
cout << a;
else
cout << c;
}
else {
if (b >= c)
cout << b;
else
cout << c;
}
return 0;
}
Explanation: Finding largest element requires users to compare multiple condition. We can compare multiple conditions by nesting one condition inside other. There is also another way of combining multiple conditions.
The above program can also be written by using compound expressions as:
C++
#include <iostream>
using namespace std;
int main() {
int a = 1, b = 2, c = 11;
// Finding largest using compound expressions
if (a >= b && a >= c)
cout << a;
else if (b >= a && b >= c)
cout << b;
else
cout << c;
return 0;
}
In this code, we are grouping multiple conditions in a single if statement using logical AND operator. (&&)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4