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....

How if else-if else ladder statement works in C program.

 How if else-if else ladder statement work in the C program.

In C programming or any other programming language, there is a concept of control statements or conditional statements. In one of our previous blog posts, we shed some light on the if else statement in the C program. In this post, we will cover the if else-if else ladder statement, which is used when multiple conditions need to be addressed. No, we are going to look at the syntax of the if else-if else ladder statement.



Syntax of if else-if else ladder statement

if (condition) 

{

// code to be executed if the condition is true

}

else if (condition)

{

// code to be executed if the condition is true

}

else if (condition

{

// code to be executed if the condition is true

}

else 

{

// code to be executed if none of the conditions is true

}


The if else-if else ladder statement is very similar to the if else statement. The main difference is the addition of an else-if part in it. In the above syntax part, we used two else-if conditions, it is according to our requirement that how many different conditions are needed to be addressed. We can use multiple else-if conditions according to our requirements.

Below is the sample code given for the if else-if else ladder statement. We are going to take a number as input and then we are going to implement the if else-if else statement.



In the above coding segment, we are entering a number, and we are going to check that the entered number is equal to 10. We are using the if condition and going to print the message within the if block that the entered number is equal to 10.



In the above coding segment if the entered number is 20 then we are going to use the else-if condition and run the code block of else-if which prints that the entered number is equal to 20, and the same logic will be used for the remaining else-if conditions and their code blocks. 



And at last, if the entered number is not equal to 10, 20, 50, or 100 then the else part will run and prints that the entered number is not equal to 10, 20, 50, or 100.


The Main difference between if and else-if statement

If we use multiple if conditions in our program, our program will check each and every if condition even if the very first if condition is true still the program will check all the remaining if conditions. Whereas in the else-if condition if the very first else-if condition is true the program will not check the remaining else-if conditions at all.

Congratulations we have successfully implemented the if else-if else ladder statement in our 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