Do-While Loop in C++ Programming Language
Do-While Loop In C++ Programming Language
The do-while loop is an exit-controlled loop. We have studied about loops before in our previous blog posts let us now look at the do-while loop in C++ programming language.
An interesting thing about do-while is that this loop executes at-least once no matter if the given condition is true or false at the very first iteration. It means in both the cases if condition is true or false the do-while will run and execute the piece of code. After that the given condition will decide wether the do-while loop will run or not if the condition is false then the do-while loop will terminate and not run for the second iteration and if the condition is still true then the do-while loop will run.
Basic Syntax of Do-while Loop in C++ Programming Language
Basic Working of Do-While Loop in C++ Programming Language
As we know that these looping statements are used to repeat or iterate some piece of code. In do-while loop first we should have a variable which will decide when to exit the loop body depending upon some given condition.
After that we simply create a do-while body and make some condition that would become false after some iterations, and before the closing curly brackets of do-while loop we increment the variable by adding 1 with each iteration or as per the logic of our C++ program. And when that specific condition is reached the do-while loop will be terminated. So, this is how the do-while loop will work in a C++ program.
Simple Programming Example of do-while loop in C++ Programming Language
Now we are going to look at three different examples with respect to do-while loop in C++ programming language.
In the above coding example we have declared and initialized a variable i with with value 1. After that we have used do-while loop and inside the do-while loop body we simply printed the value of variable i in the console screen from 1 to 10 because we have set the condition that the do-while loop should run until the value of i is less or equal to 10. And at the end of the do-while loop we simply incremented the the value of i by adding 1 with each iteration.
In the above second coding example which is almost similar to the first example. In this example we have declared and initialized i = 11. And if we notice in the condition part we mentioned that i should less then or equal to 10. So this condition is false from very beginning. And if we run the program we will notice that the do-while loop will run at least once, although the condition is false.
This proves that the do-while loop will run atleast once even if the condition is false.
In the third coding example we are going to create or calculate a table of any given number and at any specific length. In this coding example we will use do-while loop in order to generate the table of any specific number. In this coding example we will use three integer variables TableNumber, TableLength, and i for incrementing the do-while loop from 1 to any specific length.
The first input is TableNumber in which we will enter the number, whose table we want to generate. The second input is TableLength which will generate the table upto some specific length or number like if we want to generate the table of 2 we will enter TableNumber = 2 and TableLength = 10 it will generate table of 2 from 1 to 10, and we will use variable of i to control the iterations of the do-while loop. And in the condition part we will compare the value of variable i with TableLength. So that the do-while loop will run upto the value of TableLength. This is how we can generate the table of any given number in C++ program using do-while loop.
Congratulation like always we have successfully executed the do-while loop in C++ Programming language.



Comments
Post a Comment