An Introduction To The Loop Structure – Everyone’s A Bit Loopy

    

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

    

{ 5 comments… read them below or add one }

1 Paul November 29, 2008 at 12:05 pm

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

2 Matheus May 27, 2010 at 7:30 pm

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 ;]

3 Infrared Heaters October 19, 2010 at 12:58 pm

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!

4 Infrared Heaters October 19, 2010 at 9:55 pm

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.

5 graphic calculator November 24, 2011 at 5:45 am

I found much interest C++ loops (for, while, do while).
graphic calculator recently posted..TI84 Calculator ReviewMy Profile

Leave a Comment

Comment Policy: Please leave the best comment you can. Comments like, "Hey Nice Post" or comments that have been automated and make no sense, will be deleted. Feel free to argue when necessary, but no name calling. They will be edited out. I appreciate it and others will too.

CommentLuv badge

Previous post:

Next post:

Real Time Analytics