The "if" Statement is used to execute or to ignore a statement after the testing condition. The if statement is a decision making and the simplest form of selection structure. The "if" statement evaluates the condition. If the given condition is true, then statement or group of statements following the 'if' statement is executed. If the given condition is false, the statement following the 'if' statement is ignored and control transfers to the statement that comes after the 'if' structure.
In C++, the general syntax of 'if' statement is as follows:
if (test_condition)
statement-1;
statement-2;
In the above syntax:
test_condition | It indicates the test condition that can be a relational expression or logical expression. |
statement-1 | It is a single statement which will be executed if the given condition is true. If the condition is false, then statement-1 will be ignore. |
statement-2 | This statement will be executed after the 'if' condition, this statement comes after the 'if' structure. |
For compound statements, the 'if' statement syntax will be as follows:
if (test_condition)
{
statement-1;
statement-2;
statement-3;
-----------
statement-m;
}
statement-n;
In the above syntax, the set of statements (from statement-1 to statement-m) represents compound statement. Where's the statement-n represents the statement that comes after 'if' structure.
Flowchart of 'if' Structure
The flowchart of simple 'if' structure is as follows:In the above diagram, first 'if' condition is evaluate, if it is true then group of statements under if condition is executed. Similarly if condition is false, then group of statements will be ignored and "Next-Statement" will be executed.
The 'if' Statement Example Programs
The following source code of the program inputs a number from the user and displays a message if the number is greater than 50.
#include
#include
main () //start main function
{
int num;
clrscr(); //clear the display screen
cout
cin>>num;
if (num > 50)
cout
cout
getch();
} // end main function
In the above source code, if the entered value of variable "num" is greater than 50, then given 'if' condition becomes true. In this case, the statement "cout" is executed. This statement comes under 'if' statement. If the entered value is less than 50, then this statement will be ignored. The statement that is after the 'if' condition, it will be executed in both circumstances, either the 'if' condition is true or false. If you execute the above program, it will produce the following output: (suppose we entered 55)
Enter any Number: 55
The Number is Greater Than 50.
Done
Example 2: The following program inputs a number from the user and tests whether a given number is odd or even if selection structure.
#include
#include
main()
{
int num;
clrscr();
cout
cin>>num;
if (num%2==0) cout
if (num%2!=0) cout
getch();
}
In the above syntax relational operator (==) is used within if statement, if entered number is divisible by 2 (its mean even) similarly, if number is not divisible by 2 (it mean odd). In the second 'if' statement, the NOT (!) logical operator is used.