C++ if Keyword
The if
keyword in C++ is used to implement conditional statements, allowing the program to execute specific blocks of code based on whether a condition evaluates to true
.
By using the if
keyword, you can make decisions in your code and branch execution paths depending on conditions. Additional constructs like else
and else if
can be combined with if
to handle multiple conditions.
Syntax
</>
Copy
if (condition) {
// Code to execute if condition is true
}
- if
- The keyword used to define a conditional statement.
- condition
- A boolean expression that is evaluated to determine whether the block of code should execute.
- Code block
- The block of code enclosed in curly braces that executes if the condition evaluates to
true
.
Examples
Example 1: Simple if
Statement
In this example, we will learn how to use a basic if
statement to check if a number is positive.
</>
Copy
#include <iostream>
using namespace std;
int main() {
int number = 10;
if (number > 0) {
cout << "The number is positive." << endl;
}
return 0;
}
Output:
The number is positive.
Explanation:
- The variable
number
is initialized with the value10
. - The
if
statement checks ifnumber > 0
. Since the condition is true, the code block executes. - The message “The number is positive.” is printed to the console.
Example 2: if-else
Statement
In this example, we will learn how to use an if-else
statement to check whether a number is even or odd.
</>
Copy
#include <iostream>
using namespace std;
int main() {
int number = 7;
if (number % 2 == 0) {
cout << "The number is even." << endl;
} else {
cout << "The number is odd." << endl;
}
return 0;
}
Output:
The number is odd.
Explanation:
- The variable
number
is initialized with the value7
. - The
if
statement checks ifnumber % 2 == 0
. Since the condition is false, theelse
block executes. - The message “The number is odd.” is printed to the console.
Example 3: if-else if-else
Statement
In this example, we will learn how to use an if-else if-else
statement to classify a number as positive, negative, or zero.
</>
Copy
#include <iostream>
using namespace std;
int main() {
int number = 0;
if (number > 0) {
cout << "The number is positive." << endl;
} else if (number < 0) {
cout << "The number is negative." << endl;
} else {
cout << "The number is zero." << endl;
}
return 0;
}
Output:
The number is zero.
Explanation:
- The variable
number
is initialized with the value0
. - The first
if
condition checks ifnumber > 0
, which is false. - The
else if
condition checks ifnumber < 0
, which is also false. - The
else
block executes, printing “The number is zero.”
Key Points about if
Keyword
- The
if
keyword allows conditional execution of code blocks based on boolean expressions. - It can be combined with
else
andelse if
to handle multiple conditions. - Curly braces
{}
are recommended even for single-line statements to enhance code readability and avoid errors. - The condition in the
if
statement must evaluate totrue
orfalse
. - Nested
if
statements allow for more complex decision-making.