Learn do-while loop in C++

 

We’re going to be learning something called do-while loop in C++ in the last tutorial we have learnt the while loop now it is used to execute some statements again and again and in while loops we were testing the condition or the expression at the beginning of the loop this do I loop allows us to execute the statement at least once regardless of whether the expression or the condition is true or false and the syntax is first we need to write the keyword do then the pair of curly braces and between these curly braces we can write the statements and then after this curly braces .

We can write while and then in parentheses we need to write the expression and after that, we need to add a semicolon so here what happens is the statement inside this block will be executed at least once and once that statements are executed you know this expression is checked here.

In this while if this expression is evaluated to true then again the statement will be executed and and when this expression becomes false you know the control will come out of this while loop and you know it will continue with the next statements in the program ,so this do-while loop is very frequently used while making the menus.

For example, if you are making any program where you need to display the menu and you want to display the menu at least once and after that if the user wants to see that menu again then he can press some keys and you know to see that again so to do that or to demonstrate this do I loop.

We’re going to be writing a small program which is going to print some message about the learning lag to the screen and if the user wants to see that message again you need to press any keys and if you want to quit he can quit that okay to demonstrate that I’m going to create a variable which is of type care you know we’re going to store a character value entered by the user and I’m going to call it as input alright and next we need to use the do-while loop so it’s going to do keyword and then a pair of curly braces and between these curly braces .

We can write the message or the statements that we want to execute and then we need to write the expression inside this while and after that we need to add a semicolon so here just for the demonstration purpose we can I use C out and print out welcome to learning Ladd and then let’s add a new line and in the next CEO statement.

We’re going to say this is where you will find awesome video tutorials and again I’m going to add the new line and now I’m going to ask the user to enter X to exit or any other key to see this message again okay and we’re going to add an inline again all right the next thing that we’re gonna do is we’re gonna receive the input from the user and we can store that in our input variable okay now.

We have some statements inside this block and this statement will be executed at least once when you run your program so it doesn’t matter what we have in this expression whether it evaluates to true or false regardless of what the statements will be executed at least once and depending on the outcome of this expression this statement may or may not execute any other times all.

Right now I’m going to write the expression here so our input variable is going to contain the character enter by the user if the user enters a character X then we’re going to exit this loop and we’re going to check that in while loop I’m going to right input not equal to and in single quotes.

I’m gonna write X so if the input variable is containing any character other than X then execute all the statements if the user has entered the X then just come out of this do-while loop alright I’m going to say this and I’m going to build and run these alright guys now you guys can see welcome to the learning lab this is where you will find awesome video tutorials enter X to exit or any other key to see this message again.

I’m going to enter a key K and hit the enter button again and the message is printed again welcome to the learning lad etcetera etcetera enter X to exit or any other key to say this message again I’m going to enter D again that message and I’m going to enter X that’s it you know the loop is dominated so this is it guys this is about the two while loop in C++ which is used to loop through some statements depending on the expression or the outcome of an expression.

You Might Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *