C++ if Statement
An if statement consists of a boolean expression followed by one or more statements.
Syntax
The syntax for an if statement in C++ is:
if(boolean_expression)
{
// Statements to be executed if the boolean expression is true
}
If the boolean expression evaluates to true, the block of code inside the if statement is executed. If the boolean expression evaluates to false, the first set of code after the closing bracket of the if statement is executed.
In C, any non-zero and non-null values are assumed to be true, and zero or null is assumed to be false.
Flowchart
Example
#include <iostream>
using namespace std;
int main ()
{
// Local variable declaration
int a = 10;
// Using if statement to check the boolean condition
if( a < 20 )
{
// Statement to be printed if the condition is true
cout << "a is less than 20" << endl;
}
cout << "The value of a is " << a << endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
a is less than 20
The value of a is 10