Introduction to loops
For most people, doing the same thing over and over again is very boring. So why not let the computer do that for us? Computers never get bored, so they’re great at doing repetitive tasks. Let’s learn the programming concept behind this today!
Fortunately, there is a way to tell a computer to repeat an action without writing separate code for each repetition. In programming, this concept is called loops. Loops allow you to use just one command to repeat the same process multiple times.
Loops are great when you need something to happen over and over again. The command allows you to keep your code short so typing out the same code is not necessary. This greatly reduces programming time.
Types of loops
There are two types of loops: counting loops and conditional loops. Below are examples to explain both kinds of loops!
A counting loop tells the computer to repeat an action a certain number of times. Let’s consider the example of sipping on a glass of milk.
Let’s say you have to drink a glass of milk. Instead of drinking it in one go, you need to sip on it ten times. Here, you will repeat the action of drinking a glass of milk by sipping on it ten times.
Likewise, computers need a command to repeat an action many times. This command is called a counting loop. Whenever you use this command, the computer repeats an action multiple times.
A conditional loop tells a computer to repeat a sequence of actions until a condition is satisfied. Let’s consider the example of reading a book under the table lamp.
Imagine you’re reading a book under the table lamp. You need to keep reading the book until the table lamp is off. In this case, you are repeating the action until a condition is met. Similarly, conditional loops tell the computer to perform an action in computer programming until a condition is met.
Here, the ’condition’ is ‘until the table lamp is off’. Until it happens, you will perform the ‘action’ of ‘reading a book’.
Loops in different programming languages
Loops in programming languages are commands that facilitate the execution of a set of instructions/functions repeatedly while some condition is satisfied. Almost all the programming languages provide a concept called loop, which helps execute one or more statements up to a desired number of times.
In block-based programming languages such as Scratch, there are three loops: repeat loop, forever loop and repeat until loop. The "repeat" loop executes the blocks inside the loop a certain number of times. The "repeat until" loop executes the blocks inside a loop until a condition is satisfied. Finally, the "forever" loop executes the blocks inside the loop in order forever.
Kids learn programming concepts like loops, sequence, algorithms, and many more when they learn with Codingal! We provide a personalized coding journey for every child with an unparalleled pedagogy.
Codingal’s curriculum is built on BIDE, STEAM, and Bloom's taxonomy model for enhancing cognitive, logical, sensory, and computational skills of children. With our full range of coding courses, there’s something for kids of every age!
Book a free one-on-one trial class and start learning cool coding concepts today.