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