C++ If Statement (with example programs)

- February 27, 2018
The "if" statement is used to execute or to ignore a statement after the testing condition. This 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' structure 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.

Declaration

In C++, the general syntax of 'if' statement is as follows:
if (test_condition)
            statement-1;
statement-2;

Where:
test_conditionIt indicates the test condition that can be a relational expression or logical expression.
statement-1It 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-2This statement will be executed after the 'if' condition, this statement comes after the 'if' structure.
A group of statements following the 'if' statement can be given. In this case, These statements are written within curly braces { }. Then this group of statements becomes a compound statement.
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

The flowchart of simple 'if' structure is as follows:
If statement process of execution flowchart

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.

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<iostream>
using namespace std;
main () //start main function
  {
      int num;
      clrscr(); //clear the display screen
      cout<<"Enter any Number:";
      cin>>num;
      if (num > 50)
            cout<<"The Number is Greater Than 50"<<endl;
      cout<<"Done";
      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<<"The Number is Greater Than 50 "<<endl;" 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<iostream>
using namespace std;
main()
 {
      int num;
      cout<<"Enter an integer number:";
      cin>>num;
      if (num%2==0) cout<<"This is Even Value"<<endl; // this is the condition for even
      if (num%2!=0) cout<<"This is Odd Value"; //this is the condition for odd
      return 0;
 }

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.