Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy

Conditional Statements

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

In any programming language, we need to make certain decisions and based on these decisions, we need to perform different tasks just like we do in real life. Conditional statements help us to make decisions based on certain conditions.

Also, see Literals in C.

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;
    }
}


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;
    }
}


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;
        }
    }
}


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;
    }
}


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;
    }
}


Output

Number is 15
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

  1. 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.
     
  2. 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.
     
  3. 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.

Key Takeaways 

In this blog, we have discussed the following topics:

  • First, we discussed what conditional statements are and why we need them.
  • Then we discussed the types of conditional statements and their uses.

Decision-making is very important in any programming language, it helps to increase code quality. To learn more about decision making refer to this.

Just like conditional statements, there are control statements as well you can learn more about it from this.

Don't forget to check out our YouTube Channel for free and complete series on Data Structures and Algorithms.

Topics covered
1.
Introduction
2.
Types of conditional statements
2.1.
If-else Conditional Statements
2.1.1.
If Statements
2.1.2.
If-else Statements
2.1.3.
Nested-if Statements
2.1.4.
if-else-if ladder
2.2.
Switch Statements
3.
Frequently Asked Questions
4.
Key Takeaways