Oct 032011
 

When programming, have you ever found yourself doing the same thing over and over again, with only a slight modification each time?  That’s where loops come in handy.  There are many different kinds of loops.  The “for” loop is perhaps the most basic, so let’s start there.

Suppose I want to count from 1 to 100, counting by ones.  I can do this in the following way, using javascript.

for (i = 0; i >= 100, i++) {
     alert (i);
}

Let’s break this down.  In this “for” loop, there are 5 distinct parts.  The iterator, which we called “i”; the starting point, which we said would be “0”; the stopping point, which we said was 100; the amount to increase by each time, which was ++.  ++ in programming means “increase by one”; finally, inside the curly brackets { }, we told the loop what to do each time.  In this case, we said, “alert the iteration I’m currently on.”

These basic elements are the same for most languages, though in some they are not all required.  For loops in php are very similar to for loops in javascript.  In PHP, I would write a for loop to print 1 to 100 on the screen in html using the following loop.

for (i = 0; i < 101; i++) {
     print i.”</br>”
}

in lua (the language used for the CoronaSDK), the for loop looks a little different, but basically the same.  Consider the following:

for i = 0, 100 do
    print (i)
end

notice that I didn’t have to write “increase by 1” each time?  That’s because, by default, the loop structure assumes that you want to increment by 1 each time, so you only have to write the incrementation when you want to use a different value.  For example, increasing by 5 each time would look like this:

for i = 0, 100, 5 do
    print (i)
end

Iterating over arrays

The above  is fine and dandy if you want to do something a pre-determined amount of times.  What if you wanted to loop through each item in an array and do something to it?  There are several ways of doing this.  Let’s start with a “foreach” loop in php.  A foreach loop simply says, “for each item in X array, do something.”  For example, suppose you had the following array:

$classNames = array(“Peter”,”Ammon”,”Robert”,”Jen”,”Mike”,”Pam”,”Vera”);

Let’s suppose you wanted to add each of those folks to a list.  You could use a foreach loop to: (a) print the person’s name, and (b) enter a carriage return (i.e., go to the next line).  It would look like this:

foreach ($classNames as $name) {

     print $name.”<br/>;

}

pretty easy, no?  Let’s break it down.

  1. The “foreach” command indicates that the loop needs to do whatever is between the curly brackets to each item in the array.
  2. $classNames indicates the entire array
  3. $name is a variable I made up to represent a single entry as you are working on it.  This is the variable you will likely use the most inside of the loop (i.e. it’s the variable you’ll be operating on).

What if you had an associative array and you wanted to use the key as well as the value?  For example, suppose our array looked like this:

$classBirthdays = array(

“Peter” => “4 April 1978”,

“Ammon” => “23 November 1956”,

“Jen” => “2 November 1973”

)

Our loop would then look like this

How might you use a for loop in your project?

 Posted by at 7:43 am

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)