Using Loops in Your code

Updated: January 25, 2022
No Credit Card Required
ClaimYour
FreeTrialClass

Transcript

Baking muffins from scratch can be challenging. After putting all the ingredients in a mixing bowl, the recipe asks that you stir the batter for five minutes at one stir per second, that's 300 stirs. Without an electric stir, your arms are bound to get tired. If you had an electric mixer however, you could simply place the bowl under the mixer and let it do the repetitive work of mixing the batter, it would certainly ease up the process.

In computer programming, programmers are faced with a similar problem. Except instead of mixing better, the programmers problem is having to manually enter a command or instruction over and over. To solve the problem, they use what's called a programming loop. Programming loops take the repetition out of entering a command. Loops are useful when you have to write the same message multiple times, or write all the numbers in a given range. It would be like if a friend let you use a standing mixer, you could just place the bowl under the mixer and let the appliance do all the work.

There are several types of programming loops. Three common loops are the for loop, the while loop, and the do while loop. The for loop executes the code a number of times without testing conditions, it uses a counter and each time it executes the code. For example, the stir once code, it increases its count by one. It does this until the goal has been reached. four loops are useful when we know the exact number of times a specific instruction needs to be repeated. It would be just like if you could program the electric mixer to perform exactly 300 Revolutions or stirs of the batter. Since you know the exact number the recipe requires. The advantage of using a robot and a programming loop is that we could even make the robot stir faster. For example, stir three times at each iteration and get a total of 900 stores in five minutes.

The while loop and do while loops are a tad different as they both execute code when a condition is true. A condition is what you check before you do something. For example, being hungry is a condition and eating is what you do when you are hungry. The while loop will check the condition first and then execute the instruction. The instant that the condition becomes false, the while loop stops executing. So the while loop only executes the instruction if the condition is true. If you could program the mixer to keep mixing the batter until it was a certain thickness or consistency, then stop that would be similar to a while loop.

As much as we enjoy learning about programming loops, we should watch out for infinite loops. These loops run for an infinite number of times, mostly due to a programming error. In this case, the mixer would just keep stirring and stirring and never stop. So no delicious muffins.

Loops are very helpful in writing code, avoiding mistakes and communicating with a computer. They reduce the number of lines of code needed to write an entire program. Loops are even more useful when we need to execute the same code a huge number of times. All we need to do is create a loop and write the code just once and depending on how we want the code to execute in regards to conditions we can simply use a while or do while loop and when making muffins you should replace looping for stirring if you want an easy life

Loading...