If Else If ladder statement in C++ Programming Language

 


If Else If ladder statement in C++ Programming Language


Today in this blog post we are going to cover another very important conditional statement which is known as if else-if ladder statement. In C++ programming language we have studied about the if statement, if else statement, and today our focus is on if else if ladder statement in C++ programming language. The if else if ladder statement is a multi-way decision making structure with the help of which we can test multiple conditions in a sequential manner from top to bottom. And in a very simple way if any if any of the else if conditions become true it will execute that specific piece of code, and ignores the remaining else if conditions without even checking them.


In programming languages we have conditional statements or decision making statements that we can use in order to make decisions among multiple conditions. Today we are going to cover a simple conditional statement which is known as if-else-if ladder statement in C++ programming language. In C++ programming language the if-esle-if ladder statement is a multi-way decision-making construct, it allows us to test different  conditions in sequence, and executing the block of code associated with the first condition that evaluates to be true.



Syntax of if else-if ladder statement and basic working of if else-if ladder statement in C++ programming language


Syntax of if else-if ladder statement

if (condition1)
{
	// code to be executed here.
}
else if (condition2)
{
	// code to be executed here.
}
else if (condition3)
{
	// code to be executed here.
}
else if (condition4)
{
	// code to be executed here.
}
else
{
	// code to be executed here.
}


Basic working of if else-if ladder statement in C++ programming language

Now let us look at the basic syntax of if else if ladder statement in C++ programming language. We have started with the if key word followed by the opening and closing round brackets in which we can write the condition as per our porgramming logic. And just after that we have opening and closing curly brackets in which we can write the coding part that we want to be executed when a specific condition is true. Now let us move little bit deeper in this concept, if else if ladder, the if condition will work exactly the same way as it does in a simple if statement, or if else statement.

Now the most important part to keep in mind is that when the if condition is false, then the else if condition part will be checked. And if that else if condition is true than that specific piece of code will run and the remaining else if conditions will be ignored and the compiler will not even check those else if conditions. And if all the else if conditions become false then at last the else part will run and the code with in its opening and closing curly brackets will be executed.

So this is how the basic working of if else if ladder statement will be executed. 


Let us look at simple example of if-else-if ladder statement in C++ programming language

#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
	int Number;
	cout << "Enter Number = ";
	cin >> Number;
	if (Number == 10)
	{
		cout << "Entered Number is " << Number;
	}
	else if (Number == 20)
	{
		cout << "Entered Number is " << Number;
	}
	else if (Number == 30)
	{
		cout << "Entered Number is " << Number;
	}
	else if (Number == 40)
	{
		cout << "Entered Number is " << Number;
	}
	else
	{
		cout << "Entered Number is not 10, 20, 30, or 40";
	}
	_getch();
}


Explanation of C++ coding example

In above given example we print a message to the console screen to enter a number. 

cout << "Enter Number = ";
cin >> Number;

Once we enter a number then the entered number will be checked by the if-else-if ladder statement.

First of all the if condition is checked in which we specified that if entered number is equal to 10 then simply print a message that entered number is 10. 

if (Number == 10)
{
	cout << "Entered Number is " << Number;
}

And if the first condition is false then the else-if condition will run which checkes if the entered number is equal to 20. Andn if that else if condition becomes true it will simply print the message that entered number  is 20, and if that condition is false then the next else-if will run.

else if (Number == 20)
{
	cout << "Entered Number is " << Number;
}
else if (Number == 30)
{
	cout << "Entered Number is " << Number;
}
else if (Number == 40)
{
	cout << "Entered Number is " << Number;
}

And if all the else if conditions are false then the else part will run which indicates that the entered number is not 10, 20, or 30.

else
{
	cout << "Entered Number is not 10, 20, 30, or 40";
}

So, in this way the if else-if ladder statement is executed successfully.

Congratulations! like always we have successfully implemented the if else-if ladder statement in C++ programming language.

Comments

Popular posts from this blog

Install Visual Studio Community 2022 for Free (2026 Guide)

For Loop In C++ Programming Language

Your First C Program: How to Write a Hello World Program in C

Nested Loops In C++ Programming Language

Functions In C++ Programming Language