The “for” Loop

What is it good for

The “for” loop is a special case “while” loop. Most developers I know generally skip the “while” loop altogether and just use the “for” loop for it’s simplicity and versatility, I think you will see why shortly.

Formula for For

As with many ingredients of programming, different languages will represent the for loop in different ways. The formula, however, is almost always the same.

for <incrementing variable> while <boolean-expression> <incrementing statement> and repeat <statements>

As usual I have not used any language specific keyword for the formula so that actual syntax (wording and structure) is going to be different.

So What Does it Look Like for Real?

JavaScript

// To get the results below open your browsers developer tools, 
// hit F12 on the keyboard. Then select the console and you 
// will be able to type the code below

for(<incrementing variable>; <boolean-expression>; <incrementing statement>) {
    <statements>
}

How Does it Work?

At the beginning of the loop, the incrementing variable is initialized. Then, if the boolean expression of the “for” loop is evaluated as true, the subsequent statements are executed. Once the statements finish executing, the flow of the program jumps up to the incrementing statement. After the incrementing statement is executed the boolean expression is evaluated and the process begins all over again. When the expression is finally evaluated as false the statements are skipped over instead of being executed and normal program execution continues. Let’s look at some examples!

JavaScript

for(var result = 0; result < 5; result += 1) {
 console.log("result: " + result);
}

// output
// result: 0
// result: 1
// result: 2
// result: 3
// result: 4

while(var result = 6; result < 5; result += 1) {
  console.log("result: " + result);
}

// output
// 

But I Want More

Sometimes, you will need to you will need to have multiple incrementing variables. For instance a for loop that can determine if a string is a palindrome might want to count forwards and backwards at the same time. This is possible with most languages using only one “for” loop. For this demo, sometimes things are easier shown rather than explained so here is the example. I will highlight the areas of interest after you see the code.

JavaScript

var test = 'racecar';
undefined

var isPalindrome = true;
undefined

for(var i = 0, j = test.length - 1; isPalindrome && i < j; i++, j--) { 
  isPalindrome = i >= j || test[i] === test[j]; 
}
true

test = 'blah';
for(var i = 0, j = test.length - 1; isPalindrome && i < j; i++, j--) { 
  isPalindrome = i >= j || test[i] === test[j]; 
}
false

I know the logic in the above code sample might be a bit complicated, lets step through it and talk briefly about each part. First, we set a variable named “test” to the value “racecar”. “racecar” is a palindrome because it is spelled the same forwards and backwards. Next we set a variable named “isPalindrome” to the value “true”. “isPalindrome” is known as a flag or predicate value. The “for” loop is essentially the same was what we have seen before with some minor differences. In the incrementing variable section we have defined two variables. They are separated by a comma which is what makes the definition and assignment of those variables a single statement. The second part that is different here is the incrementing statements. Again, we have separated two logical statements with a comma, effectively combining them into a single programmatic statement. Everything else is code that we have shown and discussed before. If some of it is still confusing, pick it apart and experiment with it until you understand it. If you have any questions feel free to leave a comment and I will do my best to answer any questions you might have.

We’re Done

That is all there is to “for” loops. As Always, I hope you learned something from this, but if not please look at some of the other articles as they are more advanced and may lend you some insight in the world of software development.

Leave a Reply

Your email address will not be published. Required fields are marked *