C++ if-else Statement with examples

- February 26, 2018
The "if-else" structure is another form of if statement that is specially used for making two way decision. In its structure, one test condition and two blocks of statements are used to make decision. When the given condition is true, the first block is executed whereas other block is ignored. Similarly, second block is executed if the given condition becomes false.

Declaration

In C++, the general syntax of 'if-else' is as follows:
if (test_condition)
      first_statement;
else
      second_statement;
Where:
test_conditionIt indicates the test conditions that can be logical expression or relational expression.
first_statementIt will be executed if the given condition is true and second_statement will be ignored.
second_statementIf the given condition is false, this statement will be executed and first_statement will be ignored.
In the case of compound statement, the if-else syntax will be as follows:
if (condition or set of conditions)
   {
      group_of_statement_1
   }
else
   {
      group_of_statement_2
   }
 
In the above syntax, " group_of_statement_1 and group_of_statement_2 " indicates set of statement that will be executed or ignored according to the test condition.

Flowchart

The flowchart of C++ if else structure is as follows:
flow chart for simple if else statement

Example Programs

Example 1: The following source code of the program inputs two values from the user and finds the large value from them.
#include<iostream>
using namespace std;
main()
 {
      int num1, num2, large;
      cout<<"Enter First Number:"<<endl;
      cin>>num1;
      cout<<"Enter Second Number:"<<endl;
      cin>>num2;
      if
            (num1 > num2)
            large = num1;
      else
            large = num2;
  cout<<"The Large Number is<<largest;
return 0;
} // end main function
In the above program, num1, num2 and large are tree variables of integer data type. When the user enter two values, then if condition (num1 > num 2) is evaluate, suppose, we entered 5 and 3 respectively, then the condition looks like "5 > 4" which is absolutely true, in this case, "num1" value assigned to the variable "large" and its result will be display as large value.

Example 2: The following program inputs a value and finds whether the entered value is even or odd by using if else.
#include<iostream>
using namespace std;
main()
 {
      int num;
      clrscr ();
      cout<<"Enter Value:"<<endl;
      cin>>num;
      if (num%2==0)
            cout<<num<<" is even number"<<endl;
     else
            cout<<num<<" is odd number"<<endl;
      cout<<"Done";
      return 0;
 }
Output of the Program:
(suppose we entered 20)

Enter value: 20
20 is even number
Done

Example 3: The following program inputs a year and finds whether it is leap year or not.
#include<iostream>
using namespace std;
main()
 {
      int year;
      clrscr ();
      cout<<"Enter a Year:"<<endl;
      cin>>num;
      if (year%4==0) //leap year divisible by 4
            cout<<year<<" is a leap year."<<endl;
     else
            cout<<year<<" is not a leap year."<<endl;
      return 0;
 }
Output of the Program:
(suppose we entered 2020)

Enter a year: 2020
2020 is a leap yaer.