if-else statement in C++

In a program, the control statement is used for the following reasons:

Download this as PDF format  if-else statement in C++

  • To execute the program according to a specific condition,
  • To exclude a specific statement in the program or To execute the same statement multiple times,
  • We use a control statement in C ++ to perform several tasks, such as to terminate a program under a particular type of operation.

In C++ or in any other programming language, control statement has an important role. Control statement is the basic concept of a language. where we describe the problem in Programming world and catch the problem solution.

Type of Control Statement in C++

The control statement in C++ is divided into three parts which are as follows-

such as,

control statement types in C++

Conditional statement in C++

The selection statement contains one or more set of statements that are declared in the parentheses bracket ({}) and execute only when each block expression value is true. This type of statement is also called Decision-Making statement because it executes the statement when the expression value is true.

Type of conditional statement in C++

  • if statement
  • if-else statement
  • switch statement

Here is another one,

nested if-else

if statement in C++

If the expression value is true then body of if will be executed and if expression value becomes false then body of if will be skipped and execution will go to the next statement,

we can understand it with the help of the program given below

syntax

if(expression/condition)
  {
    body of if;
  }
if-statement flow diagram in C++

an if-statement flow diagram will make as follows,

if-statement flow diagram in cpp

 

 

Example of if statement in C++

In this program, we have been given the condition that if the user-input is less than 5, then the body of if will be executed.

#include<iostream>
using namespace std;
int main()
 {
  
  int x; // variable declaration

  cout<<"Enter number: ";
  cin>>x; 

   if(x<5) // expression
    {
      cout<<"Given input is smaller than 5: "<<x; // Body of if
    }
return 0;
 }

OUTPUT

Given input is smaller than 5: 4

Explanation

  • In the program, input given by the user will be stored in variable x,
  • And then expression value checks in the if statement. which is becoming true (4<5), so body of if will execute.

implementation of the nested if statement will as follows,

if-statement-example-in-cpp

 

if-else statement in C++

If the expression value is true so the body of if will be executed, and if it becomes false then the body of else (which can also be other if-statement) will be executed.

SYNTAX:

if(expression/condition)
  {
     body of if;
  }
else
  {
     body of else;
  }
if-else flow diagram in C++

Here is the Flow-diagram of the if-else statement,

if-else-statement-cpp

 

Example of an if-else statement in C++

In this program,  there we have gives the condition that if the given value by the user is less than 5 then the body of if will be executed and if it becomes false, then the body of else will be executed.

#include<iostream>
using namespace std;

int main()
 {
  int x; // variable declaration

  cout<<"Enter number: ";
  cin>>x; 
 
   if(x<5) // expression
   {
     cout<<"number smaller then 5"; // body of if
   }
  else {
         cout<<"number greater then 5"; //body of else
      }

return 0;
}

OUTPUT

Enter number: 4 
number smaller then 5

such as,

if-else-example-in-cpp

 

Let’s try with another example,

creating a calculator using if-else in C++
#include<iostream>
using namespace std;
 
int main()
 {
   
   int num1,num2; // variable declaration
 
   cout<<"Enter Two number: ";
   cin>>num1>>num2; 
  
   char select;
   cout<<"select operation(+,-,*,/): ";
   cout<<select;

     if(select=='+')   
            cout<<num1+num2;
         
     else if(select=='-') 
            cout<<num1-num2;
         
     else if(select=='*') 
            cout<<num1*num2;
         
     else if(select=='/') 
            cout<<num1/num2;
       
  return 0; 
 }

OUTPUT

Enter two number: 4 8

select operation(+,-,*,/): 12

above Program also made with switch statement- creating a calculator using switch statement in C++

nested if-else statement in C++

when an if statement is the body of another if statement in the program then it’s called a nested if-else.

if(expression)
  {
//outer if body
      if(expression) 
         {
           body of if;  // inner if body
         }
      else
         {
            body of else  // inner else body
         }
   } // close outer if body
else
   {
       body of else // outer else body 
   }

you can see above syntax of nested if-else in below flow daigram.

nested if-else flow diagram in C++

nested-if-else-in-flow-diagram-cpp

Explanation

In the nested if-statement, first if-statement (outer-if statement) body will be a second if-statement when in the outer if-statement expression becomes true then execution will go to the inner if-statement and if it becomes false then execution will go to outer else body.

inner if-statement will execute as normal if-statement. if the execution goes there.

Example of a nested if-else statement in C++

You can understand it below with the help of the program given below.

#include<iostream>
using namespace std;

 void main()
{

 int x; 

  cout<<"Enter number: ";
  cin>>x;

  if(x<5) // outer if statement
  { 
       // outer if body
     if(x<3) //inner if statement
      {
         cout<<"number greater than 3"; // inner if body

      }else {
              cout<<"number smaller than 3";//inner else body
            }
  }
 else{
      cout<<"outer else body execute"; // body of outer else
   }

  return 0;
}

OUTPUT

Enter number: 2
number greater than 3

Enter number: 5
Outer else body execute

Related Exercise


Previous- typedef in C++ with examples
Next- switch statement in C++


so you really understand what is conditional statement in C++, Try yourself

  • write a program to find out three digits of number in C++
  • write a program to find out one digit, two digit and three digit of number in C++ which type of operator you will use there?
  • write a program to find out even or odd number in C++
  • write a program to find out largest number among three number in C++
  • write a program to find out middle number among three number in C++
  • write a program where a program only accept even number in C++
  • write a program where a program only accept odd number in C++
  • write a program where a program only accept three digits of number in C++
  • write a program to find out negative, positive number in C++
  • write a program where program only accept positive number in C++(check modifier in C++)?
  • write a program where program find out mobile number is valid or not?
  • write a program to find out where user input is number or decimal type in C++.
  • write a program to find out where user input is similar to the user input or not in C++.
  • write a program where user check password is true or false where password will be initialized in the program?

now think about it, where you can use C++ conditional statement to solve real world task ?

Try yourself First. comments us we will discuss them.

“No one can teach you if you are not desperate, so desperate yourself first.”

Like it?

Leave a Reply

Your email address will not be published.