A type of loop that the condition is evaluated first before the loop body is executed.
Python has many tools and features that can help you automate repetitive tasks. One of those features is loops. Loops are a helpful and frequently used feature in all modern programming languages. Loops are helpful when you want to automate a specific repetitive task or prevent yourself from copying and pasting the same code in your program. Loops in computer programming repeat the same block of code or the same sequence of instructions multiple times
until a condition is met or until a condition is no longer met. So, all in all, loops save you from writing the same code over and over again. There are two types of loops built into Python: In this article, you will learn how to construct Here is what we will cover: A So, a A A Boolean condition is a condition that evaluates to either A It will keep executing the desired set of code statements until that condition is no longer Let’s take a hypothetical example. You may ask a user to submit a secret keyword so they can access a specific part of your site. Say that for them to be able to view some content, they first have to enter the keyword ‘Python’. To do this, you would ask them to enter that keyword. That said, you don’t know how many times the user will enter the wrong keyword. Each time they enter the wrong one, you continue to prompt them for the correct keyword. And for as long as they enter the wrong keyword, you will not allow them to continue. When they finally enter the keyword ‘Python’ you will allow them to view that content, you will stop prompting them, and that block of code will stop executing. To do something similar to this example, you would need to make use of Python's How To Write A while Loop in Python - A Syntax Breakdown for BeginnersThe general syntax for writing a
Let's break it down:
What Is An Example of A while Loop in Python?Now, let’s write the example I mentioned earlier using a Python while loop. First, I will store the secret keyword
Then, I will ask the user to input the required secret keyword they are supposed to know to access the rest of the content. To do this, I will use the
Something to note here is that the user input by default is case-sensitive, which means that if the user enters 'python' instead of 'Python' they still won't be able to continue. To fix this, you can use a string method such as
Next, it is time to construct the I am going to check if the variable
Essentially, I am checking whether what the user has entered is not equal to the string 'Python'. To write this condition in Python, I will use the
Inside the body of the
The way this works is that if the user enters the string 'Python' the loop will terminate, and the program will not run anymore. However, if the string that the user enters is not equal to 'Python', the loop will continue. So, if the And there is no set amount of times this will run and then stop, which means that for as long as the user doesn’t enter the string 'Python', the
If you are following along and want to terminate the program, type Now, if I re-run the program and finally enter the correct secret keyword, the loop will exit, and the code will stop running.
And this happens if also I enter 'python' thanks to the
The loop terminates because the condition no longer evaluates to What Is A while True Loop in Python?Earlier, you saw what an infinite loop is. Essentially, a
As you saw earlier, the way to escape this is by typing Another way to explicitly escape this is by using the Since Let's take the following example:
In this example, To stop this from being an infinite loop, I first introduce an The
ConclusionAnd there you have it! You now know how to write I hope you found this tutorial helpful. To learn more about the Python programming language, check out freeCodeCamp's Python certification. You'll start from the basics and learn in an interactive and beginner-friendly way. You'll also build five projects at the end to put into practice and help reinforce what you've learned. Thank you for reading and happy coding! Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started In which loop a condition is checked before executing the body of a loop?In an entry control loop in C, a condition is checked before executing the body of a loop. It is also called as a pre-checking loop. In an exit controlled loop, a condition is checked after executing the body of a loop.
Which of the following loop is executed first before evaluating the condition?The do-while loop is an exit controlled loop, where even if the test condition Is false, the loop body will be executed at least once. If the condition is true, then it will again execute the body of the loop otherwise the control is transferred out of the loop.
In which looping statement condition is tested first?while Loops ( Condition-Controlled Loops )
While loops check for the stopping condition first, and may not execute the body of the loop at all if the condition is initially false.
Which loop is a test first loop? The while loop is known as a pretest loop, because it tests the boolean expression before it executes the statements in its body. Note: This implies that if the boolean expression is not initially true, the body is never executed. In all but rare cases, loops must contain a way to terminate within themselves.
|