What is Loop?

Web Development Jargons Explained in 5 Different Ways

TLDR: Loop is a program that repeats a set of instructions until it is asked to stop. As an example, we can write a loop program to print out "Hello" 3 times. Loop is a fundamental concept in programming.


Explain like I only speak in plain English

Loop is a program that repeats a set of instructions until it is asked to stop. As an example, we can write a loop program to print out "Hello" 3 times. Loop is a fundamental concept in programming.

Loop must always be given an exit condition. When the condition is met, the loop will stop. If the exit condition is faulty, or doesn't exist. We risk running into an infinite loop scenario. Where the loop will continue to execute the instructions indefinitely. This is usually undesirable.

Explain like I'm a total nerd

Loops, together with conditionals, are used to design control flow. Loops are used to repeat certain tasks. Two main types of loops exist, count controlled loop or condition controlled loop. They both refer to a type of exit condition.

Count controlled loop will repeat instructions based on a number count. Such as 1 to 10. The loop will repeat 10 times, counting from 1 to 10 while doing it.

Condition controlled loop will repeat instructions based on a set condition. Such as "Stop when temperature is above 200 degree celcius?" This could be a program in an electric oven that controls the heating element.

The hardest loop to write is the recursive loop. Which is a concept in functional programming where a function will execute itself over and over again. Like a serpent chasing its own tail.

Many programming languages will offer more than one ways to write loops. In JavaScript, you can write for loop, while loop, recursive loop.

Explain like I Like Examples

If I asked you to write a program to calculate the sum of all the numbers between 1 and 20. You will probably write it out like this.

var sum = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20;

What if I asked you for the sum between 1 and 1000, are you going to do it the same way? Hopefully not. A simple count controlled loop can help us do that. The following is a simple for loop written in JavaScript. It will add the numbers 1 to 1000 and print out the sum.

var sum = 0;
for (var i = 0; i <= 1000; i++) {
  sum += i;
}
console.log(sum); // It will print 500500

This loop took 0.1 milliseconds to execute in the browser.

Explain like I like videos

Explain like I already know it

Condition controlled loops are the easiest to run into infinite loop scenarios. It is super important that you take into account all possible edge cases when writing condition loops.

The hardest loop to master is probably recursion. Programmers often rejoice whenever they see an elegant recursive function in use.

We are trusted by

Students and instructors from world-class organizations

Imperial College London
Carnegie Mellon University
City University of Hong Kong
Hack Reactor
Cisco Meraki
University of Oxford
Swift
Bazaarvoice
Waterloo
Uber
AtlanTech
Tumblr
Boston College
Bombardier Aerospace
University of St. Andrews
New York University
Minerva Schools at KGI
Merrill Lynch
Riot Games
JP Morgan
Morgan Stanley
Advanced Placement®
Google
KPMG
The University of Hong Kong
University of Toronto
SCMP
Moat
Zynga
Hello Toby
Deloitte
Goldman Sachs
Yahoo
HSBC
General Assembly
Tesla
McGill University
Microsoft

Join the upcoming Cohort #72

Enroll for December 5th, 2022