The "if statement" is used to execute (or ignore) a set of statements after testing a condition. The "if statement" evaluates a condition. If the given condition is true, the statement ( or a set of statements) following the "if statement" is executed. If the given condition is false, the statement ( or set of statements) following the "if statement" condition is ignored and the control transfer to the next statement. The syntax of the "if statement" is: If 9condition) Statements-1; Statement-2; Condition: Specifies a condition or a relational expression. When this condition is true, the statement following the "if statement" is executed. If the given condition is false, the statement following the "if statement" is ignored and the control transfers to the next statement. In the above syntax, only statement –1 will be executed if the given condition is true otherwise the control shifts to Statement-2 that comes after the Statement-1. To execute a set of statements following the "if statements", the set of statements is enclosed in curly braces, i.e. Within {}. The statements in braces are also known as compound statements. If (condition) { Statement-1 Statement-2 Statement-3 Statement-m } Statement-n
If statement is used for a decision on the given expression, if the given expression is true then the statement in "if" statement body will execute otherwise not. If statement is often used with else, and called "if else". If the expression is true then the body of "if" will execute other wise the body of "else" will execute.
The example of "if" statement
if(a == 10)
{
cout<<"Hello";
}
And the structure of if-else statement
if(a == 10)
{
cout<<"Hello! How are you?";
}
else
{
cout<<"Hi! I am fine.";
}
The example of "if" statement
if(a == 10)
{
cout<<"Hello";
}
And the structure of if-else statement
if(a == 10)
{
cout<<"Hello! How are you?";
}
else
{
cout<<"Hi! I am fine.";
}