Looping is one of the most important core concepts when learning to program. I often see a lot of people get confused with looping. So let us quickly have a look at how loops work. There are various types of loops like the while, do while and for loops. However, we will only focus on the While loop in this article. We will learn about the other loops later in this series.
A loop is something that repeats itself. In computer programming, a loop is a piece of code that runs until a certain condition exists.
Loops are very useful in running some code multiple times until the desired condition has been met. Each time the code is run in a loop, it is called an iteration. Most of the time, loops have a variable that increments on each iteration. This gives each iteration a unique number. Let us understand this by code.
The best way to understand anything is by a real example. Let’s say we had to print the number 1 to 10 on the screen. We could either write print(“1”), print(“2”), print(“3”) and so on. But this would be really stupid and time-consuming. This is where loops come to help. If you’ve been following the series, you know how we roll. Reproduce the following code from the image below in your local systems.
We have barely written 5 lines in the code above. Let us see what we have written. We firstly declared 2 variables i and x and gave them values of 1 and 10 respectively. Next, we started our while loop. You can see that we’ve writtenwhile i <= x this means that the loop will run as long as the value of i is less than or equal to 0.
The code that will run for each iteration of the loop is written in the indented block. The working is very simple. Initial values of variable i is 1, x is 10. Therefore, the first time the loop runs, the condition is very well satisfied and the code in the indented block runs, it prints out the value of I i.e 1. It then increments the value of I from 1 to 2(i+=1). This continues until the value of I becomes 11. Now the condition will fail and the loop will stop executing. The output for the same should be as follows.
We know how loops work and that is great. However, there may be times when we may want to skip running the code during a particular iteration or stop running the loop if certain conditions are met or fail. Let us try to print all even numbers from 1 to 10.
Here we will be using the continue keyword.
There is nothing new here with the exception of a simple if statement that checks if the value of i is an odd number. If it is an odd number, the continue keyword skips running any more code in the iteration and begins a fresh iteration. This should give you the following output.
Next, we will use the break keyword. The break keyword is used to stop the execution of the entire loop. Have a look at the following code.
The code above is supposed to run a loop that lasts till 10. However, We have added a condition that says if the value of I reaches 7, break the loop. So it should print numbers up to 6. Here is the output.
Loops are very useful in running a piece of code multiple times. The while loops is one of such loops that will run till a condition is exists. The continue keyword is used to skip running code from a particular iteration. The break keyword is used to stop the execution of the current and all future iterations of the loop. We will be studying more about the for loop in another article. If you have any questions regarding loops drop them in the comment section and I’ll be there for you.