Breaking News

Understand the concept of IF Statements in C++

First of all we have to understand what is an If Statement?
An if Statement is a programming conditional statement that provide two results, true or false. If it proved true, it performs the execution of statements that depends on it and if it proved false it skip the execution of statements that depends on it.

This Conditional Statement is composed by Relational Operators (Comparison Operators). This Operators create an expression called Boolean Expression. We call it Boolean Expression because it returns the value that is a Bool Data Type (true or false). There are six Relational Operators, these are:
  1. Greater than operator ( > ). This is used to compare if the operand (value) of the left side is greater than the operand of the right side.
  2. Less than operator ( < ). This is used to compare if the operand (value) of the left side is less than the operand of the right side.
  3. Greater than or equal operator ( >= ). This is used to compare if the operand (value) of the left side is greater than or equal (same) to the operand of the right side.
  4. Less than or equal operator ( <= ). This is used to compare if the operand (value) of the left side is less than or equal the operand of the right side.
  5. Equal operator ( == ). This is used to compare if the operand (value) of the left side is equal to the operand of the right side.
  6. Not Equal operator ( != ). This is used to compare if the operand (value) of the left side is NOT equal to the operand of the right side.
The Structure of the conditional statement (Boolean Expression) can be expressed like:

 ( Left operand Operator Right operand )

After understand the operators that is used to create Conditional Statement, now let us see how can we create an If Statement.
We have three types of If Statements.
  • if.... This is used to create a conditional statement that has a one alternative that depends on it. This alternative will be executed if this conditional statement has provide true result, otherwise it wont execute anything. Example: If you feel hungry, take a food. This mean that, the condition to take a food, you must feel hungry, otherwise you wont take a food. Its Syntax is look like. Note: In this Syntax alternatives represented by the word statement(s)
          if ( condition )
          {
              statement(s).......;
          } 

          Example:

          if ( feel hungry )
          {
              take a food;
          } 
  • if....else.... This is used to create a conditional statement that has two alternatives that depends on it. One among these alternatives must be executed, because one depends if statement is true and another depends if the statement is false and because the condition has two result (true or false), one alternative must be executed. Example: If you have finish the work, get the rest otherwise go to work. This mean that, the condition to get the rest you must finish the work otherwise you wont rest. Its Syntax is look like.       
          if ( condition )
          {
              statement(s).......;
          }
          else 
          {
              statement(s).......;
          }

          Example:

          if ( you have finish the work)
          {
              get the rest;
          }
          else 
          {
              go to work;
          }
  • if....else if....else.... This is used to create more than one conditional statement that has more than two alternatives. This will gives you many alternatives but only one should be selected. Example: If you feel thirsty, take some water, if you feel hungry, take a food, if you feel sleep, get to the bed, if you feel nothing go out. Here we have three conditions that has four alternatives (take some water, take a food, get to the bed and go out). Its Syntax is look like.
          if ( condition )
          {
              statement(s).......;
          }
          else if ( condition )
          {
              statement(s).......;
          }
          else
          {
              statement(s).......;
          }

          Example:

          if ( feel thirsty )
          {
              take some water;
          }
          else if ( feel hungry )
          {
              take a food;
          }
          else if ( feel sleep )
          {
              get to the bed;
          }
          else
          {
              go out;
          }

Note: When you have more than one condition like in the above example, the program will execute only one condition that has true result and the rest will be ignored. This mean that, if some one feel hungry the program will produce take a food and it will finish, it wont reach to the line of feel sleep.

Now let us do it practically.
Assume we want to write a program for a simple calculator that will give a person an option to select operator for calculation of two integers entered by user. A person can enter two numbers and select one operator, either he/she needs to get the summation ( + ), difference ( - ), product ( * ) or division ( / ).

Implementation of the above scenario, write the following code.

#include <iostream>
using namespace std;
int main()
{
     double num1, num2;
     char opertr;
     cout<<"Enter the first number: ";
     cin >> num1;
     cout<<"Enter the second number: ";
     cin >> num2;
     cout<<"Enter operator (+, -, *, /): ";
     cin >> opertr;
 
     if (opertr == '+')
     {
         cout << "Result is: " << num1 + num2;
     }
     else if (opertr == '-')
     {
         cout << "Result is: " << num1 - num2;
     }
     else if (opertr == '*')
     {
         cout << "Result is: " << num1 * num2;
     }
     else if (opertr == '/')
     {
         cout << "Result is: " << num1 / num2;
     }
     else
     {
         cout << "Operator not found...!!";
     }
}

Finally, save it and gives a name with extension of .cpp.


My input was 20 for num1 and 4 for num2 and I selected * operator it produced the following output (see picture below).


We are done...!!!

I hope now you understand how to use an If statement in C++. Feel free to ask any question if you didn't understand then I can reply as faster as I can.

Please comment and share with your friends. Don't miss the following post where we will continue to learn different things and application of new concepts.

Thank you for reading, be blessed....!!!


No comments