Loops are things in programming languages and operating systems that process stuff. Loops do what they sound like. If you were to run around in circles you are also looping around in a circle. So, if you can imagine running around in circles in the same spot, you have the ability to process stuff while you loop around in circles.
Processing stuff while you run around in circles could include catching something that someone throws to you, a ball for instance. In turn, you could throw the ball as you run around in circles as well. All the while, processing stuff that comes in and out of the loop.
That is what a loop structure is in programming languages. So let’s look at some examples.
WordPress
WordPress is written in a language called PHP which is a scripting language that is well suited for web application development. A great resource to read up and learn more about PHP is the W3 Schools website where they walk you through PHP in their PHP Tutorial.
Here is the WordPress loop that processes is the common index.php page.
<?php while (have_posts()) : the_post(); ?>
<?php endwhile; ?>
The while and endwhile statements are the loop structure that processes what shows up on a common WordPress blog home page – blog posts. My interpretations is, “Run around in a circle while blog posts show up on the page. When there are no more blog posts, quit.”
As you look at loop structures in PHP as well as other languages, you’ll find out that there are different types of loop structures used for different purposes. These include statements like do..while, for, and foreach. Again visit the PHP Tutorial at the W3 Schools website to study more examples.
Visual Basic
In Visual Basic one type of loop structure looks like this:
for x = 1 to 100
debug.print(x)
next x
The Visual Basic loop tells us that x will count from 1 to 100 as the loop executes. There are other loop structures that can be used in Visual Basic just like any other language to process various actions in various ways for various reasons.
Windows Operating System
At another level, the Windows Operating System even has loop structures. Whenever you type on the keyboard or click the mouse a message is sent to a queue. The message queue (or list) gets processed and sends those messages to a program running in Windows where the program then interprets what the message is and what to do about it.
Once you understand how a loop structure works it’s pretty easy to figure them out in other languages. It then becomes a task of figuring out the nuances of those loop structures for each language. They don’t always work the same in every programming language.
Photo Credit: frostnova
Paul says
Java has a good loop type which loops through the values of an array.
eg.
int[] numberList = {1,5,7,22};
for (int getNum : numberList)
{
System.out.println(“Count is: ” + getNum);
}
This also works for character values eg. peopleList = {“John”,”Bill”}
Pauls last blog post..HTC Touch HD review
Matheus says
i use a very good loop in php..
to receive data from post, instead of calling every input value it is much better to use a while loop structure.
loop can really save you time ;]
Infrared Heaters says
In my opinion, PHP’s ‘foreach’ loop is the most elegant loop structure of them all. For those not familiar with it, ‘foreach’ takes code like this:
// $x is array
for($y = 0; $y $value) {
// do something with $value
}
I just wish Java (the language I’m using on my current project) had this feature!
Infrared Heaters says
OK, the middle of my last comment somehow got left out. (Guess there must be a tin whisker in my laptop somewhere!) I meant to say that ‘foreach’ takes code like the above, and turns it into something like this:
// $x is array
foreach($x as $y => $value) {
// do something with $value
}
This, to me, is one of the most elegant loops you can have in a modern programming language.
graphic calculator says
I found much interest C++ loops (for, while, do while).
Reima says
Other interesting loop is while, it’s conditional loop that is executed only if statement besides brackets is true.
For example:
<?php
$num = 1;
while ( $num
Will Print: 1 2 3 4 5 6 7 8 9 10
crystal paperweights says
The loop structure is one of the basic building block of any programming language.These are very strong in creating the programs that are very tedious but very important to have.