Table of contents
1.
Introduction
2.
What are Conditional statements in C++?
3.
Types of conditional statements
3.1.
If-else Conditional Statements
3.1.1.
If Statements
3.1.2.
If-else Statements
3.1.3.
Nested-if Statements
3.1.4.
if-else-if ladder
3.2.
Switch Statements
4.
Frequently Asked Questions
4.1.
What is the use of the default case in the Switch case?
4.2.
Can we use a switch case inside another switch case?
4.3.
If in an if-else-if ladder more than one if conditions are true, then which block of statements will be executed?
5.
Conclusion
Last Updated: Jan 10, 2025
Easy

Conditional Statements

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Conditional statements in C++ help the program decide what to do based on certain conditions. They let you run different parts of the code depending on whether something is true or false, making your program smarter and more flexible.

Conditional Statements

Also, see Literals in C.

What are Conditional statements in C++?

Conditional statements in C++ allow the program to make decisions based on specific conditions. Common statements include if, if-else, and switch, which execute code blocks depending on whether a condition evaluates to true or false, enabling dynamic and logical flow in programs.

Types of conditional statements

In C++, there are two types of conditional statements:

If-else Conditional Statements

If Statements

  • If conditional statements are the most simplest decision-making statements. In this, a given statement or a block of statements will be executed or not depending upon the IF statement, that is, whether it is true or false.
  • The Condition will evaluate to be either true or false. If the condition evaluates as true, then the block of statements will be executed, and if the condition is evaluated as false, then the block of statements won't be executed.
    If we do not provide the curly braces after the if(condition) then by default, only the first statement right after the if(condition) will be considered to be inside its block.
     

Syntax:

if(condition)
{
  // statements to be executed
  if condition is true.
}

 

Example:

#include<iostream>
using namespace std;

int main()
{
    int x=5;
    if(x<32){
        cout<<"Inside the if block"<<endl;
    }
}
You can also try this code with Online C++ Compiler
Run Code

 

Output:

Inside the if block

If-else Statements

  • The if statement alone only tells us what to do if the condition is true, but if the condition is false then it doesn't tell us what to do. If we want to do something when the condition is false, we use if-else conditional statements.
  • In this, if the condition is true, then we will execute the block of statements just after the condition, but if it is false, then we will execute the block of statements which is after the keyword else.

Syntax

if(condition)
{
	// execute this block if the condition is true
}
else
{
	// execute this block if the condition is false
}

 

Example

#include<iostream>
using namespace std;

int main()
{
    int x=50;
    if(x<32){
        cout<<"Inside the if block"<<endl;
    }else{
        cout<<"Inside the else block"<<endl;
    }
}
You can also try this code with Online C++ Compiler
Run Code


Output

Inside the else block

You can try and compile with the help of online c++ compiler.

Nested-if Statements

  • Nested-if statements are if-statements inside another if statement. In other words, an if statement which is a target of another if statement. Usually used when we must test a combination of conditions before deciding on the proper action.

Syntax

if(condition-1)
{
	// execute this when condition-1 is true
	if(condition-2)
	{
		//execute this when condition-2 is true
	}
}

 

Example 

#include<iostream>
using namespace std;

int main()
{
    int x=20;
    if(x>5){
        cout<<"Number is greater than 5"<<endl;
        if(x>15){
            cout<<"Number is greater than 15"<<endl;
        }else{
            cout<<"Number is smaller than 15"<<endl;
        }
    }
}
You can also try this code with Online C++ Compiler
Run Code


Output:

Number is greater than 5
Number is greater than 15

if-else-if ladder

In C++, if statements are executed from top to bottom. Multiple if-else-if statements can be grouped together, as soon as one of the conditions controlling the if is true, then the statement associated with that if will be executed, and the rest of the else-if ladder is bypassed. In case none of the conditions are true, then the final else statement is executed.

Syntax

if(condition)
   statements
else if(condition)
   statements
.
.
.
else
   statements

 

Example

#include<iostream>
using namespace std;

int main()
{
    int x=2;
    if(x==1){
        cout<<"Number is 1"<<endl;
    }else if(x==2){
        cout<<"Number is 2"<<endl;
    }else if(x==3){
        cout<<"Number is 3"<<endl;
    }else{
        cout<<"Number is greater than 3"<<endl;
    }
}
You can also try this code with Online C++ Compiler
Run Code


Output 

Number is 2

Switch Statements

  • Switch case statements are used when we have multiple conditions, and we need to execute a specific block of statements corresponding to a condition when it is satisfied. It is an alternative to the use of lengthy if..else-if statements, which is complex and not well structured, in comparison, switch statements are cleaner, efficient, and easier to read when dealing with multiple conditions. It is a multiway branch statement that provides an easy way to dispatch execution to different parts of code based on the value of the expression.
  • Break: if the break is omitted, execution will continue onto the next case and won't stop even when a case is valid. It is used to stop the execution inside a switch block and helps to terminate the switch block and break out of it.
  • Default: it is also referred to as a general case, this is executed if no case is executed.
     

Syntax

Switch(x)
{
    Case 1 : // this will be executed if x=1
		break;
    Case 2 : // this will be executed if x=2
		break;
    Case 3 : // this will be executed if x=3
		break;	
    Default: // this is executed if x doesn't match any given cases.
}

 

Example

#include<iostream>
using namespace std;

int main()
{
    int x=15;
    switch (x)
    {
    case 5: cout<<"Number is 5"<<endl;
        break;
    case 10: cout<<"Number is 10"<<endl;
        break;    
    case 15: cout<<"Number is 15"<<endl;
        break;
    default: cout<<"Number is greater than 15"<<endl;
        break;
    }
}
You can also try this code with Online C++ Compiler
Run Code


Output

Number is 15

Frequently Asked Questions

What is the use of the default case in the Switch case?

We use the default case in a switch case so that at times when there is no case match, then this default will be executed.

Can we use a switch case inside another switch case?

Yes, we can use a switch case inside another switch case it is known as Nested Switch Statements.

If in an if-else-if ladder more than one if conditions are true, then which block of statements will be executed?

If in an if-else-if ladder more than one if conditions are true, then the block of statements for the first if condition which evaluates to true will be executed, this happens because in C++ statements are executed from top to bottom.

Conclusion

Conditional statements are essential in C++ programming as they allow your code to make decisions and respond to different scenarios. Mastering them helps create flexible, efficient, and user-friendly programs, making them a vital skill for any developer to learn and apply effectively.

Live masterclass