A loop is one of the most important constructs of coding. It is very commonly used while coding and young programmers are introduced to loops in the very beginning of their coding journey.
Basic structures of a computer program
A computer program can be defined as a set of instructions given to the computer. The instructions are often written in a specific computer programming language. There are three primary structures that are commonly used in programming languages:
1. Sequences: A sequence is a set of instructions within a program that will be executed one after another.
2. Selection: A selection refers to a decision point in a program where the program control is transferred to a branch of code based on a condition.
3. Loops: A loop is a block of code within a program that will be executed multiple times such that the program’s control will go in a loop around the block of code.
What is the definition of a loop?
A loop is a coding concept that is used to repeat any block of code multiple times. It is a very elegant way to perform repetitive tasks in programming. Most high level languages like C, C++, C#, Python and Java have support for multiple types of loops.
Why do we use loops?
We use loops in programs for performing the same thing multiple times. For example, if you are writing a program to draw 10 houses, you will write the same lines of code to draw a house to draw each of the ten houses. In other words, you will write code to draw 1 house and repeat writing the same code nine more times.
What if you wanted to draw 1000 houses? It will become extremely tedious to write the same lines of code 1000 times. What if you wanted to draw 100,000 houses? Coding will become a very painful, time consuming and repetitive chore.
Coding will often involve writing programs for several repetitive tasks and programmers will often encounter the job of writing repetitive lines of code. Loops in coding is designed to solve this problem of writing repetitive lines of code.
Instead of writing the same lines of code multiple times, programmers writing code can write the lines of code required to complete the repetitive task, example drawing houses, once, and then use a loop to repeat the same lines of code multiple times.
What are the different types of loops?
For Loop: The for-loop is a simplest form of loop and it is used to repeat a block of code multiple times. The for-loop simply repeats a block of code for a specified number of times. It is used whenever there are no conditions involved in repeating a task within a program.
While Loop: The while loop is used whenever you need to check for a condition before performing a repetitive task. In other words, a while loop is used when you do not know the number of times a task needs to be repeated.
For example, if you are writing code to check inventory levels of a product, you will use a while loop to check if the inventory has reached say 5 and whenever it reaches that level, you’ll exit the loop, change status to low inventory and submit a re-order for the product.
Do While Loop: The do-while loop is similar to the while loop and is used to repeat a task based on a condition. The difference is that in the while loop, the condition is checked before repeating the task and in the do-while loop, the condition is checked after the task is repeated each time.
Repeat Until Loop: The repeat-until loop is similar to the do-while loop and is used to repeat a task based on a condition. The difference is that in the repeat-until loop, the task is repeated as long as a condition is met. Once the condition is not met, you will exit the loop.
For example, if you are writing a program to read 100 pages, you can use a do-while loop to read a page and check if you have reached page 100. Whenever you reach page 100, you’ll exit the loop and display a message to say that the book has been fully read.
Where can I learn more about loops?
Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.