Home > C++ > Conditional Statements in C++

A conditional statement or Conditional statements in C++ are used to make a decision based on specific condition and run the desired piece of code.

Conditional Statements in C++

There are different types of conditional statements available in c++. They include: If statements, Switch cases, C++ ternary operator or sometimes referred as the conditional operator in c++.

If: – 

If is used to test a condition i.e. if a particular condition is satisfied only when the particular code should be run.
In real life example, we can say that your mom wants you to bring food home and says if they are offering the discount of 50 % then you should bring food home.
The general syntax for if statement is:

if(condition)
{
     //code to be run when given condition is true
}

E.g.

//when condition is true
#include "stdafx.h"
#include "iostream"

using namespace std;

void main()
{
     int x = 11;
     if(x > 10)
     {
          cout << "condition given inside if statement is true!!!!!";
     }
     getchar();
}

if- a simple conditional statement

//when condition is false
#include "stdafx.h"
#include "iostream"

using namespace std;

void main()
{
     int x = 1;
     if(x > 10)
     {
          cout << "condition given inside if statement is true!!!!!";
     }
     getchar();
}

Output:
if condition false - Conditional statements in C++

If-else: –

Another conditional statement used very often is if-else.
It’s more like if statement. If we change our real-life example given in if statement as:
your mom wants you to bring food home and says if they are offering a discount of 50 % then you should bring Pizza otherwise bring Burger.
The basic syntax is:

if (condition)
{
      //to be run when given condition is true
}
else
{
     //code to be run when given condition is false
}

E.g.

//when condition is true
#include "stdafx.h"
#include "iostream"

using namespace std;

void main()
{
     int x = 11;
     if(x > 10)
     {
          cout << "condition given inside if statement is true!!!!!";
     }
     else
     {
          cout << "condition given inside if statement is false!!!!!";
     }     
     getchar();
}

if - Conditional statements in C++

//when condition is false
#include "stdafx.h"
#include "iostream"

using namespace std;

void main()
{
     int x = 1;
     if(x > 10)
     {
          cout << "condition given inside if statement is true!!!!!";
     }
     else
     {
          cout << "condition given inside if statement is false!!!!!";
     }
     getchar();
}

if-else false - Conditional statements in C++

if-else-if statements: –

We can check more than one conditions can by using if-else-if statement.

The basic syntax is:

if (condition1)
{
      //to be run when given condition is true
}
else if(condition2)
{
      //to be run when given condition is true
}
else
{
      //to be run when given condition is false
}


Note:- Below Programs Will show different Variations of if-else-if statements

E.g.

#include "stdafx.h"
#include "iostream"

using namespace std;

void main()
{	
     int x = 11;
     if(x > 0)
     {
          cout << "condition given inside if statement is true!!!!!";
          cout << "\nx is greater than zero!!!!!!!!!!!";
     }
     else if(x < 0)
     {
          cout << "condition given inside if statement is true!!!!!";
          cout << "\nx is less than zero!!!!!!!!!!!";
     }
     else
     {
          cout << "Both conditions are false!!!!!";
          cout << "\nx is zero!!!!!!!!!!!";
     }
     getchar();
}

if-else-if true - Conditional statements in C++

#include "stdafx.h"
#include "iostream"

using namespace std;

void main()
{
     int x = -11;
     if(x > 0)
     {
          cout << "condition given inside if statement is true!!!!!";
          cout << "\nx is greater than zero!!!!!!!!!!!";
     }
     else if(x < 0)
     {
          cout << "condition given inside if statement is true!!!!!";
          cout <<"\nx is less than zero!!!!!!!!!!!";
     }
     else
     {
          cout << "Both conditions are false!!!!!";
          cout << "\nx is zero!!!!!!!!!!!";
     }
     getchar();
}

if else if true -Conditional statements in C++

#include "stdafx.h"
#include "iostream"

using namespace std;

void main()
{
     int x = 0;
     if(x > 0)
     {
          cout << "condition given inside if statement is true!!!!!";
          cout << "\nx is greater than zero!!!!!!!!!!!";     
     }
     else if(x < 0)
     {
          cout << "condition given inside if statement is true!!!!!";
          cout << "\nx is less than zero!!!!!!!!!!!";
     }
     else
     {
          cout << "Both conditions are false!!!!!";
          cout < "\nx is zero!!!!!!!!!!!";
     }
     getchar();
}

if-else-if false - Conditional statements in C++

Nested if statements: –

One or more if statements can be used inside another if statement. Like

if(condition)
{
     if(condition)
     {
          if(condition)
          {
                //to be run when given condition is true
          }
     }
}

E.g.

#include "stdafx.h"
#include "iostream"

using namespace std;

void main()
{
     int x= 5;
     int y = 20;
     int z = 10;
     if (x > 0 )
     {
          cout << "1st If Condition is True;"<<endl;
          if (y > z)
          {
               cout << "2nd If Condition is True;" << endl;
               if (x > z)
               {
                    cout << "3rd If Condition is True;" << endl;
               }
          }
     }
     getchar();
}

nested ifs - Conditional statements in C++

Note: – All Relational and Logical operators are used in conditions.

Switch: –
Another conditional statement which is used is switch statement. It provides a mechanism to run a particular block of code based on some value in a convenient manner.
Basic Syntax is:

switch (choice)
{
     case 1:
     {
          //code to be run
          break;
     }
     case 2:
     {
          //code to be run
     break;
     }
     .
     .
     .
     default:
     {
          //code to be run
     }
}

The break is used in every case to get out of switch statement when a particular case is run. Otherwise, all below statements will also run.
he default when none of the cases is true.
E.g.

#include "stdafx.h"
#include "iostream"

using namespace std;

void main()
{
     int x;
     cout << "Please Enter Value : ";
     cin >> x;
     switch (x)
     {
          case 0:
          {
               cout << "Entered Number is zero"<<endl;
               break;
          }
          default:
          {
               cout << "Entered Number is non zero"<<endl;
               break;
          }
     }
     getchar();
}

case true - Conditional statements in C++

When no case satisfies the condition the compiler will run the default block.
default - Conditional statements in C++

Another example will show some other aspects of the switch statement.
E.g.

#include "stdafx.h"
#include "iostream"

using namespace std;

void main()
{
     char ch;
     cout << "Please Enter Value : ";
     cin >> ch;
     switch (ch)
     {
          case 'a':
          {
               cout << "Entered Character is vowel";
               break;
          }
          case 'e':
          {
               cout << "Entered Character is vowel";
               break;
          }
          case 'i':
          {
               cout << "Entered Character is vowel";
               break;
          }
          case 'o':
          {
               cout << "Entered Character is vowel";
               break;
          }
          case 'u':
          {
               cout << "Entered Character is vowel";
               break;
          }
          default:
          {
               cout << "Entered Character is not vowel";
               break;
          }
     }
     getchar();
}

 
switch example

When no case satisfies the condition the compiler will run the default block.
switch example -Conditional statements in C++

We can write the above code in a much efficient way as:
E.g.

#include "stdafx.h"
#include "iostream"

using namespace std;

void main()
{
     char ch;
     cout << "Please Enter Value : ";
     cin >> ch;
     switch (ch)
     {
          case 'a':
          case 'e':
          case 'i':
          case 'o':
          case 'u':
          {
               cout << "Entered Character is vowel";
               break;
          }
          default:
          {
               cout << "Entered Character is not vowel";
               break;
          }
     }
     getchar();
}

switch example - Conditional statements in C++

When no case satisfies the condition the compiler will run the default block.

switch example - Conditional statements in C++

C++ Ternary Operator / Conditional operator in C++: –  

Another operator which is referred as a ternary operator or conditional operator in C++ is used as a conditional statement. Ternary operator helps a convenient way to write an expression that yields two possible values depending on the condition.
The basic syntax is:

condition? expression1: expression2
The ternary operator will check the condition and if the condition is true it will use the first expression otherwise it will use the 2nd expression. Below example will help us understanding ternary operator in a better way.

#include"stdafx.h"
#include"iostream"

using namespace std;

int main()
{
     int no = 0, no1 = 0, no2 = 0;
     cout << "Please Enter No 1 : ";
     cin >> no1;
     cout << "Please Enter No 2 : ";
     cin >> no2;
     // using the conditional operator in c++
     no = (no1 > no2) ? no1 : no2;
     cout << "Maximum Number is " << no << endl;
}

conditional operator in c++ - Conditional statements in C++

We can use the ternary or conditional operator as:

#include"stdafx.h"
#include"iostream"

using namespace std;

int main()
{
     int no = 0, no1 = 0, no2 = 0;
     cout << "Please Enter No 1 : ";
     cin >> no1;
     cout << "Please Enter No 2 : ";
     cin >> no2;
     cout << "Please Enter No 3 : ";
     cin >> no3;
     // We are using the conditional operator in c++
     no = (no1 > no2) ? no1 : (no2 > no3) ? no2 :no3;
     cout << "Maximum Number is " << no << endl;
}

c++ ternary operator - Conditional statements in C++

2 thoughts on “Conditional Statements in C++

  1. Hey mikel!
    Nicely written up. As a new learner I must say your article will help a lot to understand the things quickly. Great article indeed.

Leave a Reply

Your email address will not be published. Required fields are marked *