- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What Does while true do in Python?
Popular loop structures like "While true" may be found in various computer languages, including Python 3.11.1. This kind of loop continues indefinitely until a specific condition is satisfied, at which point it ends. This loop is very helpful when you need to carry out the same operation repeatedly until a particular event occurs.
The following Python code demonstrates the syntax for the while true loop.
while True: # Code block to be executed repeatedly
The keyword “while” precedes the condition "True" in the loop's first declaration. This boolean value evaluates to True every time, so it indicates that the loop will continue until something else breaks it. Any repeatable code can be placed inside the loop.
So here are a few basic examples of how a “while True” statement can be used in Python −
while True: print("Hello, world!")
From the above code snippet,
As the condition True is always true, the while True statement generates a loop that will keep repeating forever.
"Hello, world!" is the argument given to the print() method inside the loop. The phrase "Hello, world!" will be printed to the console.
Now let’s understand the next example with a counter.
count = 0 while True: count += 1 if count > 15: break print(count)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
This is because the loop starts with the variable count equal to 0, and then increments it by 1 on each iteration until it reaches 15.
The while True statement in the loop establishes an infinite loop that runs continuously until the break statement is used to end it.
The += operator is used in the loop to add 1 to a counter variable's count throughout each iteration.
The loop is ended using the break statement if the count value is larger than 15.
To output the value of the count to the console for each iteration of the loop, the print() function is used.
When you need to repeat an action until a specific event occurs, the "while true" loop is a practical choice. For instance, you may use the "while true" loop to keep questioning the user for input until they provide the proper response and an "if" statement inside the loop to check the accuracy of their response. The loop will continue to run and the user will be prompted once more if the response is inaccurate. If the response is accurate, on the other hand, the loop will end and the program will go to the next line of code.
Here is an example of how you could use the "while true" loop to accomplish this task in Python −
while True: user_input = input("Please enter a valid response: ") if user_input == "yes": print("Thank you for your response.") break elif user_input == "no": print("Thank you for your response.") break else: print("Invalid response. Please try again.")
The output produced by the code is as follows −
Please enter a valid response: hello Invalid response. Please try again. Please enter a valid response: yes Thank you for your response.
Let’s have a look at the step-by-step explanation of the code −
Initialize the loop − The first line of the code initiates the "while true" loop.
As long as the "True" condition is still true, the loop will continue to execute.
Prompt the user for input − Within the loop, the following line of code is used to prompt the user for input −
user_input = input("Please enter a valid response: ")
The "input" function is utilized to show the user a prompt and retrieve their input. In this scenario, the prompt is requesting that the user provide a valid response. The answer given by the user is saved in the "user input" variable.
Check the input − The code then checks the user's input to see if it is correct. This is accomplished through the use of an "if" statement.
The "if" statement examines if the "user input" variable is either "yes" or "no". If the user enters "yes," the code inside the first "if" block is executed, displaying a message thanking the user for their response. If the answer is "no," the code inside the second "if" block is executed, and another message thanking the user is displayed. If the user's input is neither "yes" nor "no," the code inside the "else" block is executed, and an error message indicating that the answer is invalid is displayed.
Terminate the loop − The code's final step is to end the loop if the user provides a valid response. This is accomplished with the "break" statement −
The loop is terminated by using the "break" statement. If the user selects "yes" or "no," which are both acceptable responses, the "break" statement will be carried out, breaking the loop. The loop will continue to run and the user will be prompted for input once more if the user's response is invalid.
The "while true" loop needs to be broken by an external event, such as a "break" statement. The loop will continue to run until the user quits the program if there is no "break" statement or another way to stop it. If the loop is carrying out an operation that consumes a lot of resources, such as CPU time or memory, this could lead to issues. To prevent this, it is crucial to carefully prepare the circumstances in which the loop should end and incorporate a means of stopping the loop if these circumstances are realized.
In summary, the "while true" loop is an effective tool for repeating tasks up until a predetermined condition is met. This loop structure is a key component of a Python programmer's toolkit, whether it is used in a simple script or a complex project. You may produce more effective and productive code and confidently take on a variety of programming jobs by becoming an expert at using the "while true" loop.
Kickstart Your Career
Get certified by completing the courseGet Started