How to Start a Loop Again in C
Loops are a useful and frequently used feature in all modernistic 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 best pick for that.
Loops are a set of instructions that run repeatedly until a condition is met. Let'southward larn more than near how loops piece of work in Python.
Loops in Python
There are two types of loops built into Python:
-
for
loops -
while
loops
Allow's focus on how you can create a while
loop in Python and how it works.
What is a while loop in Python?
The full general syntax of a while
loop in Python looks similar this:
while condition: execute this code in the loop's torso
A while loop volition run a slice of code while a condition is True. It volition keep executing the desired set of lawmaking statements until that condition is no longer Truthful.
A while loop volition always get-go check the status before running.
If the condition evaluates to True
then the loop will run the lawmaking within the loop's torso.
For example, this loop runs as long equally number
is less than 10
:
number = 0 while number < x: print(f"Number is {number}!") number = number + 1
Output:
Number is 0! Number is 1! Number is ii! Number is 3! Number is 4! Number is 5! Number is half dozen! Number is seven! Number is viii! Number is nine!
Hither, the variable number
is set to 0
initially.
Before any code is run, Python checks the status (number < x
). It evaluates to Truthful so the impress statement gets executed and Number is 0!
is printed to the console.
number
is then incremented by 1
. The condition is re-evaluated and it is over again True, so the whole procedure repeats until number
is equal to 9
.
This fourth dimension Number is 9!
is printed and number
is incremented, but now number
is equal to 10
so the condition is no longer met and therefore the loop is terminated.
It'due south possible that the while
loop never runs if it doesn't meet the condition, like in this case:
number = l while number < x : print(f"Number is {number}!")
Since the status 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 it ultimately determines when the loop will end.
If you do not add this line, you will create an infinite loop.
number
will not be incremented and updated. It will ever be set and remain at 0
and therefore the condition number < x
will exist True forever. This means that the loop volition 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 state of affairs like this?
Printing Control C
to escape and end the loop.
What is a do while loop?
The general syntax of a do while
loop in other programming languages looks something similar this:
do { loop cake argument to be executed; } while(status);
For example, a do while loop in C looks similar this:
#include <stdio.h> int main(void) { int i = 10; do { printf("the value of i: %i\north", i); i++; } while( i < 20 ); }
What is unique in exercise while loops is the fact that the code in the loop block will exist executed at to the lowest degree one fourth dimension.
The lawmaking in the argument runs one time and then the condition is checked only afterwards the code is executed.
So the code runs in one case get-go and so the condition is checked.
If the status checked evaluates to true, the loop continues.
There are cases where you would want your lawmaking to run at to the lowest degree ane time, and that is where do while loops come in handy.
For example, when you're writing a program that takes in input from users you may ask for just a positive number. The code will run at least once. If the number the user submits is negative, the loop volition keep on running. If it is positive, information technology will cease.
Python does non take built-in functionality to explicitly create a do while
loop like other languages. But information technology is possible to emulate a do while
loop in Python.
How to emulate a do while loop in Python
To create a do while
loop in Python, you demand to modify the while
loop a bit in guild to get like behavior to a do while
loop in other languages.
Equally a refresher and then far, a do while
loop will run at to the lowest degree once. If the status is met, and then it will run again.
The while
loop, on the other hand, doesn't run at to the lowest degree once and may in fact never run. Information technology runs when and only when the condition is met.
So, let'due south say we have an case where we want a line of code to run at least in one case.
secret_word = "python" counter = 0 while Truthful: word = input("Enter the secret word: ").lower() counter = counter + ane if word == secret_word: pause if word != secret_word and counter > 7: intermission
The code will run at least one fourth dimension, asking for user input.
It is always guaranteed to run at least once, with Truthful
, which otherwise creates an infinite loop.
If the user inputs the correct clandestine give-and-take, the loop is terminated.
If the user enters the wrong secret give-and-take more than 7 times, then the loop will be completely exited.
The break
statement allows you to command the catamenia of a while
loop and not end up with an space loop.
intermission
will immediately terminate the current loop all together and break out of it.
So this is how you create the a like effect to a do while
loop in Python.
The loop e'er executes at least once. Information technology will continue to loop if a status is non met and and then terminate when a status is met.
Conclusion
You at present know how to create a do while
loop in Python.
If you're interested in learning more almost Python, you tin watch the 12 Python Projects video on freeCodeCamp's YouTube channel. You lot'll become to build 12 projects and it's geared towards beginners.
freeCodeCamp too has a free Python Certification to assistance you gain a proficient understanding and a well rounded overview of the of import fundamentals of the language.
You'll also get to build v projects at the cease of the course to practise what yous've learned.
Thanks for reading and happy learning!
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs every bit developers. Get started
Source: https://www.freecodecamp.org/news/python-do-while-loop-example/
0 Response to "How to Start a Loop Again in C"
Post a Comment