An if statement in C++ is a conditional statement that allows you to execute a block of code only if a specific condition is true. It helps control the flow of the program based on whether a condition evaluates to true or false.
Syntax:
if (condition) {
// Code to execute if condition is true
}
Example:
#include <iostream>
using namespace std;
int main() {
int age = 18;
// Checking if the person is eligible to vote
if (age >= 18)
{
cout << "You are eligible to vote."<< endl;
}
return 0;
}
In this example, if age>=18 then it will show message You are eligible to vote.
Output:
You are eligible to vote.