Loops

Lesson Progress
0% Complete

In computer science, a loop is a programming structure that repeats a sequence of instructions until a specific condition is met. Programmers use loops to cycle through values, add sums of numbers, repeat functions, and many other things.

Loops are supported by all modern programming languages, though their implementations and syntax may differ. Two of the most common types of loops are the while loop and the for loop.

While Loop

A while loop is the simplest form of a programming loop. It states that while a condition is valid, keep looping. In the PHP example below, the while loop will continue until i is equal to num.

   $i = 1;
   $num = 21;

   while ($i < $num)  // stop when $i equals $num
   {
       echo “$i, “;
       $i++;   // increment $i
   }

If $i is 1 and $num is 21, the loop will print out 1, 2, 3, 4… etc. all the way to 20. Then the loop will stop or “break” after 20 iterations because the while condition has been met.

For Loop

A for loop is similar to a while loop, but streamlines the source code. The for loop statement defines the start and end point as well as the increment for each iteration. Below is the same loop above defined as a while loop.

   $num = 21;

   for ($i = 1; $i < $num; $i++)  // stop when $i equals $num
   {
       echo “$i, “;
   }

Though for loops and while loops can often be used interchangeably, it often makes more sense to use one over the other. In most cases, for loops are preferred since they are cleaner and easier to read. However, in some situations, a while statement can be more efficient. For instance, the following PHP statement can be used to load all the values from a MySQL result into an array using only one line of code.

   while ($row = mysql_fetch_array($result))

NOTE: Since loops will repeat until a given specific condition is met, it is important to make sure the loop will break at some point. If the condition is never met, the loop will continue indefinitely creating an infinite loop. Writing code that allows infinite loops is bad programming practice, since they can cause programs to crash.

English EN Français FR
Technology comprises an entire system of people and organizations, knowledge, processes, and devices that go into creating and operating technological artifacts, as well as the artifacts themselves. Modern technology is a product of science and engineering, and technological tools are used in both fields
Science is the study of the natural world, including the laws of nature associated with physics, chemistry, and biology and the treatment or application of facts, principles, concepts, and conventions. Science is a body of knowledge that has been accumulated over time.
Engineering is both a body of knowledge—about the design and creation of human-made products—and a process for solving problems. Engineering utilizes concepts from science and mathematics as well as technological tools.

Magic Milk Experiment

When dish soap is dropped into milk, there is a swirl of activity of soap molecules finding and bonding with fat molecules in the milk. With millions of molecules swirling around looking for mates, the colors in the milk get all mixed up!

Dry Ice Experiment

Dry Ice is a fun and cold experiment. There’s many things you can create, so what can you make with dry ice?
Dry ice is a cool and a fun outdoor experiment. Have you ever worked with it? The big caution is that you don’t want to touch it less it burns your skin. Got that? So it has to be handled with tongs and/or insulated gloves.

DIY Lava Lamp Experiment

With this project, you can make your own (temporary) DIY lava lamp with household materials! It’s easy and safe, and it looks very cool.
This lava lamp experiment is super cool! Your kids will love exploring colored water and oil, but a surprise ingredient will make this science activity even more exciting! It’s always a hit with the kids. So grab a few household supplies and give this lava lamp science activity a try!