Information and technology, FahadTheITGuy, programming languages, web development, desktop development, C, C++, C#, visual basic, HTML, CSS, javascript, jQuery, MS SQL server, in this blog, we will learn different programming languages and implement them with examples, we are also going to learn web development in asp.net, asp.net core, asp.net MVC, asp.net core mvc, we also learn about databases in ms SQL server, we will also cover databases, data structures, data mining, and wisdom mining
If Else If ladder statement in C++ Programming Language
Get link
Facebook
X
Pinterest
Email
Other Apps
-
If Else If ladder statement in C++ Programming Language
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 in C++ programming language
if(condition1)
{
//code to be executed if condition1 is true
}
else if(condition2)
{
//code to be executed if condition2 is true
}
else if(condition3)
{
//code to be executed if condition2 is true
}
else
{
//code to be executed if all the conditions are false
}
Basic working of if-else-if ladder statement
In if-else-if ladder statement first of all there is an if condition in the start of if-else-if ladder. If that condition is true then the code block after if condition will run and the remaining else-if and else conditions will be ignored.
But if the first if condition is false, then the program will check else-if conditions and if any of those conditions are true the program will run that peace of code under that specific else-if block.
Let us look at simple example of if-else-if ladder statement in C++ program
In above given example we print a message to the console screen that to enter a number. Once we enter a number then the entered number is 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. And if the first condition is false then the else-if condition will run which checkes that if the entered number is equal to 20 it will simply print the message that entered number is 20, and if that condition isi false then the next else-if will run.
And if all the conditions are false then the else part will run which indicates that the entered number is not 10, 20, or 30.
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 program.
Comments
Post a Comment