I wrote my first program when I was 12 years old. Wow, that's 15 years ago; I'm not sure how I feel about that. I had a class that taught BASIC. I know we used Apple computers in that class, but my memory isn't good enough to remember exactly which model, only that it was command-line only. Perhaps I didn't care enough to know which model it was. The first program I wrote displayed the American flag and showed the lines of the Pledge of Allegiance. Then I wrote a "choose-your-own-adventure" game. I was to show off the game at the school's open house, but I guess I forgot to save my work from when I finished as it was incomplete, so I showed the flag instead. After the open house, I really got into programming. I cut one of my not-so-important classes to go to the computer lab and code. I spent class time handwriting code when I should have been reading a textbook or studying instead. Yep, I wrote code by hand. Sometimes, I still do. The class I cut all the time to work on my new program was Life Management, and I almost failed it! I kind of find it funny that I nearly failed Life Management because I became obsessed with writing code.
The new program I was coding at the time (in case you were wondering), was another game. It was another "choose-your-own-adventure" game. This one had a spy theme. I was including an inventory system, money, allies (that could die if you weren't careful), and a password system so that you can pick up where you left off. The programs we made were unable to save/load data. I'm not sure why, maybe I just hadn't learn how to do that. One thing I remember from back then was using the GOTO statement. Handwriting code using the GOTO statement is not fun. For those of you not familiar with the GOTO statement, here is an example (disclaimer: the syntax written for the GOTO statements is pseudo code, I do not wish to fill my head with too much GOTO nonsense):
GOTO 125
That means jump to line 125 and continue from there. This is how control statements worked. For example:
6 if (something = 0) GOTO 8
7 GOTO 10
8 line of code if the if statement is true
9 GOTO 11
10 line of code if the if statement is false
11 code after the control statement
Oh, what a mess. And this is how loops were done as well.
5 some line of code
6 some line of code
7 if (something = something) GOTO 5
8 code that comes after the loop
That's actually not as bad to look at as the control statement really, since that example had only one GOTO. However, this article is not about GOTO. It's about the For Loop. I like to know exactly what is happening when I write code. I had always felt like there was more going on with For Loops that what meets the eye. What does this have to do with GOTO? Well, in BASIC the GOTO statement is how the loop restarts itself. In the For Loop, it's the bracket that closes the loop. But I wondered, when does the counter get incremented? When does the comparison happen? Of course these questions are pretty easy to answer. The comparison happens at the beginning of the loop and the increment happens at the end. The initalization for the counter variable happens before the loop starts the first time and then never again. But this wasn't enough for me. So I decided to play around with it a bit. I took a simple For Loop (all this is in Javascript by the way, I'm not sure if the same applies to other programming languages.):
for (var a = 0; a < 3; a++)
{
alert(a);
}
and decided to "translate" it to how I think it would work as a while loop:
var a = 0;
while (a < 3)
{
alert(a);
a++
}
Why "translate" it to a while loop? I feel that while (true) { } is the purest form that a loop can take. I used it many times when I needed a quick loop and didn't feel like thinking much. While looking at it, I wondered....what would happen if I replaced "var a = 0" with something else? I'm sure that it would be an error. So I tried it. I went from
var a = 0;
alert("Starting the Loop");
while (a < 5)
{
alert(a);
a++;
}
to
var a = 0;
for (alert("Starting the Loop"); a < 5; a++)
{
alert(a);
}
And holy cow it worked! I got the message "Starting the Loop", and then a message for each iteration of a. Why did it work? It isn't supposed to work like this. Every tutorial I've read, every lecture about loops I've listened to...none of them ever mentioned that you could do this. How far down the rabbit hole does this go? Are the other statements in the For Loop similar? I tried out other ways to write the For Loop, getting more and more absurd just so I can see how far I could go. All of these ended up working:
for (var a = 0; a < 5; alert(a))
{
a++;
}
for (var a = 0; 1; a++)
{
if (a > 3)
break;
else
alert(a);
}
for (; a < 5; a++)
{
alert(a);
}
for (; ; a++)
{
alert(a);
if (a > 5)
break;
}
for (; ; )
{
a++;
alert(a);
if (a > 5)
break;
}
for (var a = 0; alert("printAlert"); a++)
{
if (a > 3)
break;
}
The last one only printed "printAlert" once before ending the loop. I guess since alert() has no return value it evaluates to false.
In what situations would I need to mangle a For statement so badly? Probably none. I suppose the reason why no one ever mentions that it's possible to do this is because you shouldn't be doing this. That's what the While loop is for. However, I now feel like I understand the For Loop much better than before, even if all that nonsense above is useless 99.9% of the time.
You can go here to view to view all the examples I put here and see them actually working.
No comments:
Post a Comment