Skip to content Skip to sidebar Skip to footer

How Long Will It Take the Two to Meet Again on the Loop

Loops are a useful and frequently used feature in all mod programming languages.

If you want to automate a specific repetitive task or prevent yourself from writing repetitive lawmaking in your programs, using a loop is the best choice for that.

Loops are a fix of instructions that run repeatedly until a condition is met. Let's learn more nearly how loops work in Python.

Loops in Python

At that place are ii types of loops built into Python:

  • for loops
  • while loops

Let's focus on how you can create a while loop in Python and how it 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's body                              

A while loop will run a piece of code while a condition is True. It will keep executing the desired ready of code statements until that condition is no longer True.

A while loop will ever offset bank check the condition before running.

If the status evaluates to True and then the loop will run the lawmaking within the loop'due south torso.

For instance, this loop runs as long every bit number is less than 10:

                number = 0 while number < 10:     print(f"Number is {number}!")     number = number + 1                              

Output:

                Number is 0! Number is one! Number is 2! Number is 3! Number is four! Number is 5! Number is half-dozen! Number is vii! Number is 8! Number is nine!                              

Here, the variable number is gear up to 0 initially.

Before whatever code is run, Python checks the condition (number < 10). It evaluates to Truthful so the print argument gets executed and Number is 0! is printed to the panel.

number is and then incremented by one. The condition is re-evaluated and it is again True, and then the whole procedure repeats until number is equal to nine.

This fourth dimension Number is 9! is printed and number is incremented, but now number is equal to ten so the status is no longer met and therefore the loop is terminated.

It's possible that the while loop never runs if information technology doesn't meet the status, similar in this example:

                number = l while number < ten :     print(f"Number is {number}!")                              

Since the condition is always Imitation, the instructions in the loop's body don't execute.

Don't create space loops

Every bit you saw from the example to a higher place, while loops are typically accompanied past a variable whose value changes throughout the elapsing of the loop. And information technology ultimately determines when the loop will end.

If you do non add together this line, you volition create an infinite loop.

number will not be incremented and updated. It will always be set and remain at 0 and therefore the condition number < 10 will be Truthful forever. This means that the loop will continue to loop forever.

                                  # don't run this  number = 0 while number < 10:     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 equally doing this:

                                  #don't run this while Truthful:     impress("I am always true")                              

What if you find yourself in a situation like this?

Press Control C to escape and stop 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 statement to be executed;   } while(status);                              

For example, a exercise while loop in C looks like this:

                #include <stdio.h>   int master(void)  {    int i = 10;    do {       printf("the value of i: %i\n", i);       i++;       }   while( i < 20 );  }                              

What is unique in practise while loops is the fact that the code in the loop block will exist executed at to the lowest degree one fourth dimension.

The code in the argument runs ane time and then the condition is checked just after the code is executed.

So the code runs once outset and and so the status is checked.

If the condition checked evaluates to true, the loop continues.

There are cases where you would want your lawmaking to run at least one time, and that is where practice while loops come in handy.

For example, when you're writing a program that takes in input from users yous may enquire for just a positive number. The lawmaking volition run at least once. If the number the user submits is negative, the loop will keep on running. If it is positive, it will cease.

Python does non have congenital-in functionality to explicitly create a practise while loop like other languages. But it 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 need to alter the while loop a bit in order to get similar beliefs to a practise while loop in other languages.

As a refresher so far, a do while loop will run at least in one case. If the condition is met, then it will run once again.

The while loop, on the other hand, doesn't run at to the lowest degree one time and may in fact never run. It runs when and only when the condition is met.

So, allow's 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:     give-and-take = input("Enter the secret give-and-take: ").lower()     counter = counter + i     if word == secret_word:         suspension     if word != secret_word and counter > 7:          intermission                              

The code will run at least in one case, asking for user input.

Information technology is always guaranteed to run at least once, with True, which otherwise creates an infinite loop.

If the user inputs the correct secret discussion, the loop is terminated.

If the user enters the wrong cloak-and-dagger word more than 7 times, then the loop will be completely exited.

The break argument allows y'all to control the menses of a while loop and non stop up with an infinite loop.

break volition immediately terminate the current loop all together and break out of it.

Then this is how you create the a similar effect to a do while loop in Python.

The loop always executes at to the lowest degree once. It will continue to loop if a status is not met and then finish when a condition is met.

Determination

You at present know how to create a do while loop in Python.

If you're interested in learning more about Python, you tin watch the 12 Python Projects video on freeCodeCamp's YouTube channel. You'll go to build 12 projects and information technology's geared towards beginners.

freeCodeCamp likewise has a free Python Certification to assist yous proceeds a good agreement and a well rounded overview of the important fundamentals of the language.

You lot'll also become to build 5 projects at the end of the class to practice what you've learned.

Thanks for reading and happy learning!



Learn to code for free. freeCodeCamp'south open source curriculum has helped more than 40,000 people get jobs equally developers. Get started

fyepironerts.blogspot.com

Source: https://www.freecodecamp.org/news/python-do-while-loop-example/

Post a Comment for "How Long Will It Take the Two to Meet Again on the Loop"