What are loops to kids?

Isn't it irritating when we have to repeat the same instructions? Especially when we are working with the computer which is developed for our convenience. In programming, we have loops to solve this problem. A loop is a command used to repeat a part of code until the desired process is complete.

Loops are essential for many of the repetitive tasks commonly required in programming. Let us understand this with an example: It is easy to make one pizza. But what if you need 100 pizzas for a house party? Instead of doing the same steps over and over again, you would like to prefer to do it once and have it automatically repeated until 100 pizzas were made. This will make your task simple. Programmers often need to repeat the same code hundreds of times in one program. They use loops to write the code only once and repeat it as many times as needed.

Pizza Problem Loop

Let us understand this with another example:-

Let us understand this with another example:-If a set of instructions is needed to be repeated we use a loop. Here is a car in a maze example that is taken from our CuriousJr APP. So, We have to reach our destination by following a specific path. Firstly have a look at the path and write or think about the instruction you will give to your friend who is driving this car to reach the destination. Can you notice any repetition in your instructions? If yes, you are going right, and here comes the use of the loop.

Loops Car Example

Move Forward- Car will move forward till any other turn arrives.

Turn Right- Car will turn right.

Move Left- Car will turn Left.

Repeat Until Destination {
    Do {
        Move Forward 
        Turn Right
        Move Forward
        Turn Left
    }
}

You have to repeat these instructions till we reach our destination which will be 4 times. Instead of writing the same instructions repeatedly, we used the loop here which will end when we reach our destination.

Types of Loops

The syntax for loops varies for different programming languages. We will consider the general case to make you understand the concept behind it.

  1. Counter Loop

In the Counter loop, the program is controlled by an integer that counts up from the initial value to the final value. Here initial value means the starting value of the integer which is set by us and the final value means the upper limit for the integer where it stops the counting and loops stop. Another name for the counter loop is the iterative loop. For loop is an example of a counter loop.

Loop Example

In this picture, i is an integer, and 1 and 5 are its starting and final value whereas it will repeatedly print Hi Curious Jr 5 times as instructed in the program. In this by 1, we mean the counter is increasing with the value 1 and loops stop when the final value is reached, In this case when i value becomes 5.

2. Conditional Loop - In the Conditional loop, the program is controlled by a condition which is set by the programmer initially. It repeats one or more steps until the condition is met. For Example: Read the book till light is on. Here the condition for us is represented by a light bulb. If it gets switched off you will stop reading. While loop is an example of a Conditional loop.

While Loop

This diagram shows the working of Conditional loops. Initialization means giving integers a starting value. we can initialize the integer while writing the program then we check the condition if it is true code goes inside the loop and execute the statement and then update the integer and if the condition fails it goes out of the loop and stops.

When will the loop run infinitely?

For the Counter loop, if we don't give any upper limit then the integer will not understand where to stop and will work infinitely. Whereas for the Conditional loop, if the condition is never met it will run infinitely this problem could occur if we make a logic error or due to some inherent characteristic of the loop.

Why are loops important?

  • Loops help DRY your code

Don't Repeat Yourself

The main purpose of loops is to prevent repetitive code. Typing out the same code over and over increases programming time and likeliness for bugs(errors in our program). Use loops to keep your code DRY - this means you don't repeat yourself, which will make your code easier to manage and debug.

  • Loops Save Time

Take the same example we have taken for counter loops and imagine we don't have loops then What? Then we have to give instruction again and again like this:

print("Hi Curious Jr")
print("Hi Curious Jr")
Print("Hi Curious Jr")
print("Hi Curious Jr")
print("Hi curious Jr")

Isn't it time-consuming?

Thus using Loops we can save our time.

  • Loops Minimize Errors

Without loops, lines of code will increase automatically thus causes problems during debugging as Less code means less time a programmer spends debugging.

Importance Of Loops

Different types of loops used in Programming

There are different types of loops we can use in programming. Each loop is explained with the help of real-life examples for better understanding.

1.For loop

When your program needs to keep doing some tasks for the specific no of times, we use for loop.

Let us understand this with a real-life example:

// 1 year = 12 month
for(each month in a year)
{
    we get our salary
}

We know there are 12 months in a year( specific no). We get our salary at the end of every month and this loop repeats itself till we reach the last month.

2. While loop

Any time your program needs to keep doing something (repeat the same or similar action) until something happens and you don’t know in advance when or if that something happens, a while loop is useful.

Let us understand this with a real-life example:

while(you do NOT press STOP or NOT the END of the playlist)
    get the next song
    play the next song
}

Consider any music player, you can find and even create your own playlist. Here for while loop condition, we are checking if the playlist ends or you stop the music. If any of the conditions are true music will stop and the loop will exit. But if you don't press stop next song will play until the playlist ends.

3. Do-while loop

It is similar to the While loop except for the fact that it is guaranteed to execute at least one time. The do part is executed without checking any condition and while part executes like a normal while loop.

Let us understand this with a real-life example:

do{
    check the door
}
while( door is not locked){
    lock the door
}

Here check the door will execute once for sure. then we will move to the while part and check the condition if it's true it will execute the statement inside the while loop and if false will go out of the loop and exit.

Which loop to use?

You'd use a for loop to iterate over a finite sequence of elements (such as no of chocolates you have in your hand), whereas you'd use a while loop to iterate while a condition was true and a do-while loop to execute the sequence at least once and continue to execute while the condition was true.

Now, Whenever you do any task in your real-life try to identify the loop you are using in it.

Keep following CuriousJr for more updates on Coding for Kids.

Happy coding!