Keywords: loops, iterations sets, conditional loops, counting loops, while loop, for loop,
This chapter will look at loops in Python which is a way to repeat an operation an arbitrary number of times. We will see that there are different types of loops that you can use and we will, of course, go through how they are used and when it is best to use the different types of loops.
When we want to repeat an operation or sequence, we use a loop. With the help of the loop, we can write a program sequence (e.g., a calculation) to be performed while either a particular condition is fulfilled (conditional loop), or a loop that repeats the program sequence a predetermined number of times (counting loop).
The two most common structures for repetition within Python that we will, of course, look at more closely are:
When you want to repeat an operation or a code sequence several times, you should use a loop. The loop is used to repeat a statement or block of statements as long as a particular condition is true. The term “control flow statement” is often used to refer to loops in Python programming generally. There are usually two different types of loops, namely the conditional loop or the counting loop, that is used.
We can illustrate a loop using the flow chart on the right that shows in a simple way how loops work.
Note that we can end up with so-called “infinity loops“, that is, loops that do not proceed in the program (terminated / finished). The condition in the loop is simply never met, and since the condition must be fulfilled for the loop to complete, it runs on. Fortunately, the compiler shuts down the program and gives you an error message, so there’s nothing you need to worry about. It is also good to know that it is fairly simple (and common) error to do.
We use the condition loop when we want to repeat a code sequence, over and over again, until a specified condition is fulfilled. This means that the code block is executed as long as a condition is met and the loop will after each execution of the code block go back and evaluate if the condition is met. The condition loops that we will take a closer look at in this chapter are the while loop
There are countless uses for conditional loops in Python programming, but if we are going to take some, that will give you an idea of how it can be used. For example, you have programmed a program to run as long as the user does not give the exit command. Thus, there will be a condition that as long as input from the user is not “quit”, the loop will run.
Furthermore, another example of uses could be that you want to go through a list or collection of data until you find a particular keyword. If you have a large amount of data and are looking for one, or more, elements, you can use a condition loop until you find the right element. Your condition will then be that as long as you have not found what you are looking for, the loop should check the next place in the data list.
Please leave feedback and help us make this site better
How useful was this article?
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?
The counting loop repeats a code sequence a predetermined number of times. Therefore, we use the counting loop when we want to repeat an operation a specific number of times. You often want to run a code block several times in a row. The counting loop that we are going to look at in this chapter is the for loop.
We use counting loops when the number of iterations is fixed. Therefore, we usually use counting loops to go through lists or a predetermined number of iterations. For example, a list of participants and based on information, a command must be executed. It may be a teacher who has a list of his students who continuously updates their grades based on a number of different tests. Instead of having the teacher go through the list manually, the counting loop can do so and then collect and modify the information.
In general, when it comes to counting loops, it is possible to iterate from the beginning to the end, or vice versa. You can also modify the loop so that you take every other step, or maybe every third step. As you realize, it is possible to adjust the calculation loop to several different purposes.
So-called infinity loops are loops that never gets ended. The condition of the loop is never met, therefore, the loop continues forever. An infinity loop can be due to several reasons; for example, that you forget to update the variables that are in the loop. However, your compiler will end the loop. Therefore, it will also cause your program to shut down, and you will receive an error message