Word That Means Loop or Again

Loops are a useful and ofttimes used feature in all modern programming languages.
If you want to automate a specific repetitive task or prevent yourself from writing repetitive code in your programs, using a loop is the all-time option for that.
Loops are a set of instructions that run repeatedly until a condition is met. Let's learn more than about how loops piece of work in Python.
Loops in Python
There are 2 types of loops congenital into Python:
-
for
loops -
while
loops
Permit'southward focus on how you tin can create a while
loop in Python and how information technology works.
What is a while loop in Python?
The general syntax of a while
loop in Python looks similar this:
while condition: execute this code in the loop'southward body
A while loop volition run a piece of lawmaking while a condition is True. It will keep executing the desired fix of code statements until that status is no longer True.
A while loop will always outset bank check the status before running.
If the status evaluates to True
then the loop will run the code within the loop's body.
For example, this loop runs as long as number
is less than 10
:
number = 0 while number < 10: print(f"Number is {number}!") number = number + 1
Output:
Number is 0! Number is i! Number is ii! Number is 3! Number is four! Number is 5! Number is vi! Number is 7! Number is eight! Number is 9!
Here, the variable number
is set to 0
initially.
Before whatsoever code is run, Python checks the condition (number < x
). It evaluates to Truthful so the impress statement gets executed and Number is 0!
is printed to the console.
number
is and then incremented by 1
. The condition is re-evaluated and it is again Truthful, and then the whole process repeats until number
is equal to ix
.
This time Number is 9!
is printed and number
is incremented, only now number
is equal to 10
and so the condition is no longer met and therefore the loop is terminated.
Information technology'south possible that the while
loop never runs if it doesn't come across the condition, like in this example:
number = l while number < 10 : impress(f"Number is {number}!")
Since the condition is always False, the instructions in the loop's body don't execute.
Don't create infinite loops
As you saw from the example above, while
loops are typically accompanied past a variable whose value changes throughout the duration of the loop. And information technology ultimately determines when the loop will end.
If you do non add together this line, you will create an infinite loop.
number
will not be incremented and updated. Information technology will e'er exist set and remain at 0
and therefore the condition number < 10
volition be True forever. This means that the loop will continue to loop forever.
# don't run this number = 0 while number < x: print(f"Number is {number}!")
Output:
Number is 0! Number is 0! Number is 0! Number is 0! Number is 0! Number is 0! Number is 0! ...
Information technology runs infinitely.
It is the same as doing this:
#don't run this while True: print("I am e'er true")
What if you find yourself in a situation like this?
Press Control C
to escape and end the loop.
What is a do while loop?
The general syntax of a practice while
loop in other programming languages looks something similar this:
do { loop block statement to be executed; } while(condition);
For example, a do while loop in C looks like this:
#include <stdio.h> int master(void) { int i = 10; practise { printf("the value of i: %i\n", i); i++; } while( i < 20 ); }
What is unique in do while loops is the fact that the code in the loop cake will be executed at least one time.
The lawmaking in the statement runs ane time and and so the status is checked only after the code is executed.
So the code runs one time starting time and then the condition is checked.
If the status checked evaluates to true, the loop continues.
In that location are cases where you would want your code to run at least one time, and that is where exercise while loops come in handy.
For case, when you're writing a programme that takes in input from users you may ask for but a positive number. The code will run at least once. If the number the user submits is negative, the loop will proceed on running. If it is positive, information technology will finish.
Python does not have congenital-in functionality to explicitly create a do while
loop like other languages. But it is possible to emulate a do while
loop in Python.
How to emulate a exercise while loop in Python
To create a do while
loop in Python, you need to modify the while
loop a bit in guild to go like beliefs to a do while
loop in other languages.
As a refresher and then far, a do while
loop will run at least once. If the condition is met, then it will run once again.
The while
loop, on the other hand, doesn't run at least once and may in fact never run. It runs when and only when the condition is met.
So, allow'due south say nosotros take an example where we want a line of lawmaking to run at least once.
secret_word = "python" counter = 0 while Truthful: word = input("Enter the underground word: ").lower() counter = counter + ane if word == secret_word: break if discussion != secret_word and counter > 7: intermission
The code will run at least in one case, asking for user input.
It is always guaranteed to run at least one time, with Truthful
, which otherwise creates an space loop.
If the user inputs the right cloak-and-dagger word, the loop is terminated.
If the user enters the wrong hole-and-corner word more than than seven times, and then the loop will be completely exited.
The suspension
argument allows you lot to control the flow of a while
loop and not stop up with an infinite loop.
pause
will immediately end the current loop all together and break out of it.
So this is how you create the a like upshot to a do while
loop in Python.
The loop always executes at least once. It will go on to loop if a status is non met and and so terminate when a condition is met.
Decision
You now know how to create a do while
loop in Python.
If you lot're interested in learning more than virtually Python, you lot tin can watch the 12 Python Projects video on freeCodeCamp's YouTube channel. You lot'll get to build 12 projects and it's geared towards beginners.
freeCodeCamp likewise has a free Python Certification to help you lot gain a good understanding and a well rounded overview of the important fundamentals of the language.
You'll likewise get to build v projects at the end of the course to practice what you've learned.
Thanks for reading and happy learning!
Larn to code for costless. freeCodeCamp's open source curriculum has helped more 40,000 people get jobs as developers. Get started
Source: https://www.freecodecamp.org/news/python-do-while-loop-example/
Post a Comment for "Word That Means Loop or Again"