While Loop in C++ Programming Language

Image
  While Loop in C++ Programming Language We have discussed about the entry-controlled loop or pre-tested loop previously in our blog posts. These statements or loops are used to repeat a piece of code for a specific number of times as per our program requirement. We have studied about the for loop already. Today in this blog post we are going to study about the while loop in C++ programming language. And we will look at how this while loop actually works and how we can stop it by making some condition false so that the loop might not run infinitely. We use while loop in C++ program when the number of iterations is not known in advance and depends on the condition. When we know exactly that how many times a loop should run then we use the for loop instead of while loop. Basic Syntax Of While Loop in C++ Programming Language Basic Working Of While Loop In C++ Programming Language Now we are going to discuss about the basic working of while loop in C++ programming....

If Else If ladder statement in C++ Programming Language

 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

Popular posts from this blog

How to run c project in visual studio community 2022

While Loop in C++ Programming Language

if and if else statement in C++ programming language