How do-while loop works in C programming language?
How do-while loop works in C programming language?
Loops are an essential part of any programming language. These looping statements are basically control structures that helps the users to execute the block of code. The looping statement that we are going to look at today is do while loop in C programming language.There is a key characteristic of do while loop is that, when we use the do while loop in our C programming or any other programming it will run atleast once no matter if the specific condition is true or false. In both the cases it will atleast once, without even checking the condition.
We use control statements in programming languages to execute code based on certain conditions. In C programming the concept of control statement is the same. These control statements iterate pieces of code multiple times according to user requirements. These iterations are controlled by some conditions that are given by the programmer or the developer. There are mainly three types of control statements or looping statements, which are,
1) do-while loop
2) while loop
3) for loop
These looping statements are basically used to repeat a specific piece of code, which increases the reusability of the piece of code and also boost the performance of our C programming code.
Today our main focus is on the do-while loop. We are going to look at the do-while loop and the basic syntax of the do-while loop. After that, we are going to look at some examples to get a better understanding of do-while loop and its working. These loop statements increase the performance of the code.
Some of the common uses of the do while loop are that if you are making a system where you want to ask the user or prompt him for the input even before assessing his input choice, then we can use do while loop in such conditions or scenarios.
Now before diving deep into the concept of do while loop let us look at the basic syntax and working of do while loop in C programming language.
Basic syntax of do while loop and working of do while loop in C programming langugage
do { // code to be written here. // update expression (e.g., i++) } while (condition);
Now let look at the basic syntax of do while loop and its working. At the very beginning of the do while loop we have used the do key word which is the starting point of our do while loop. After that we have opening and closing curly brackets with in which we can write the piece of code as per our requirement or programming logic.
And at the end of the do while loop closing curly brackets we have used the while key word followed by opening and closing round brackets with a condition part between them like this,
while (condition);Now remember that we have to increment the value of variable before the closing curly bracket which will control the number of iterations of our do while loop.
So this is the basic syntax and working of the do while loop in C programming language.
Just rememeber the very important point about the do-while loop, remember that the do-while will run at least once whether the condition is true or false it is going to run at least one time. The do-while loop starts with the do keyword. After the do keyword, the coding block comes, and at last, the end of the loop a while condition part is used in which some condition is given and this condition ends with a semicolon. If the condition is true then the do-while loop runs again. And if the condition is false then the loop terminates.
Simple coding examples of do while loop in C programming language
#include<stdio.h> void main() { int i = 1; do
{ printf("%d\n", i); i++; } while (i <= 5); _getch(); }
while (i <= 5);i++;
int i = 1; do
{ printf("%d\n", i); i++; } while (i <= 5);
#include<stdio.h> void main() { int i = 7; do
{ printf("%d\n", i); i++; } while (i <= 5); _getch(); }
Now if we look at the second C coding example of do while loop in C programming language we can see that this coding example is exactly the same as the previous one the only difference is that, the do while loop condition is false from the very start, because we assigned a value of 7 to int i and the condition is specifying i should be less or equal to 5, but still the do while loop will run at least once,
int i = 7; do
{ printf("%d\n", i); i++; } while (i <= 5);
so this example shows us that do while loop runs atleast once even if the condition is false. So this is the basic working of do while loop in C programming language.
Congratulations! like always we successfully implemented the do while loop in C programming langauge.
Comments
Post a Comment