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 does a do-while loop work in the c programming language?

 How does a do-while loop work in the c programming language?

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



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 an example to get a better understanding of do-while loop and its working. These loop statements increase the performance of the code.


Below is the syntax of the do-while loop

do{

//Write the coding part here

}while(condition);

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

Now we are going to look at a simple example for a user to enter a password. The loop will keep iterating and repeat until the user enters the correct password. On entering the correct password the while condition will become false and the do-while loop will terminate. Let us look at the example given below.



 

At the start of the code, we used two header files <stdio.h> and <string.h>. The <stdio.h> file deals with standard input and output functions like print() and scanf(). And <string.h> deals with string functions like strcmp().

We declared a char array variable named it as password and assigned it a value "secret" After that we took another variable named as input[20] with an array size of 20.

In the do-while loop, we prompt the user to enter the password in the input[20] variable. In the do-while loop, we used the condition that will check whether the given value is correct or not.

We used strcmp() function and used it like this strcmp(input, password) the strcmp() returns 0(zero) when the two compared strings are equal which are input and password, and returns non-zero when strings are not equal.

When two strings are equal it will return 0. The do-while loop will terminate, and access will be granted to the user for a valid password.

Congratulations we have successfully implemented the do-while loop with an example.






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