Loopy Variables, Variable Loops ("Lucky Seven" Project #4)

I’m going to combine two things in one post today, variables and loops. Variables are sort of like what you used back in algebra (or whatever math you like, I’m partial to calculus myself); they hold something. They stand for something else. But in algebra, x could be 2 or it could be 3.1415926535. It’s just x, but it can be different types of numbers. Javascript is like algebra in that it uses variables to stand for a value, but it’s different because Javascript variables can hold lots more things–including letters, words, and sentences–but you need to tell your computer what will go in there.
I have a Gatorade bottle on my desk. Pretend that Gatorade, Mountain Dew, Pepsi, and Coke are all made in the same factory by the same company. Each bottle looks a bit different, but it’s all the same concept. Then, pretend that they all have to go in their own bottles. Come on, you’d be pretty angry if you went to the store and bought a bottle of your favorite drink only to open it up and find that it’s not the right thing. At the factory, the people put labels on the bottles that tell you what has to go in there. The label sticks tight, so even if you don’t fill it up with drink until later on, you can’t change your mind and put Pepsi in the Coke bottle.
Now pretend that Gatorade is a single character (like “Z” or “s”, and keep in mind that there’s a big difference to your computer between big and little letters, even of the same letter), Mountain Dew is a word or sentence, Pepsi is an integer, and Coke is a floating point number (a number with a decimal point in it). Each bottle is the little bit of memory in your computer that holds the stuff you want to put there. The label on the bottle is called the variable’s type. The drink is the data. The greatest thing about the drinks you get from Javascript Drink Co. is that you can dump out your bottle whenever you want and fill it up with fresh drink, but only drink of the same type. No, you can’t even put Pepsi in your Coke bottle (They’re NOT the same!), that’s not allowed. You can do this as many times as you like, whenever you like, for whatever reason. There’s no need to worry about wasting it, either.
I have no idea whether that was necessary or not. I hope it helped someone. But if it didn’t, I hope it made someone laugh.
Variables have to be initialized before you can use them. At first, they’re just null variables. They’re bottles with labels and nothing in them. Not Coke Zero (zero or a ” ” in the case of a string), not water, plain air. If you put those in a store, chances are no one wants them. If you tried to sell them to someone as filled drink bottles, people would be pissed when they realized that that you tried to get them to drink an empty bottle. Your computer feels the same way. If you give it an uninitialized variable and tell it to use it, it’s going to get pissed. With Javascript, it’s probably going to do nothing. With languages that you have to compile, they’ll be tough on you, but at least they’ll let you know what’s wrong.
Loops are very important for computer programs. In fact, a professor once told me that about 90% of the time a program is running, it’s going through loops. Loops are the way you tell the computer to do something over and over and over and over again. You can tell it to do it a certain number of times or until something happens.
For example, I could tell you to hop on one foot five times. That’s called definite iteration. I could also tell you to keep hopping on one foot until I say that I want you to stop. That’s indefinite iteration.
Loops are pretty easy to write, but also really easy to mess up. In fact, I mentioned giving you a warning before letting you go on with this project.
If you use Internet Explorer, I don’t recommend executing any of your Javascript programs that contain a loop unless you’re sure they don’t contain what’s called an infinite loop. My mom hates the thought of using Firefox. I’ve told her that it’s not different to use and that it’s a trillion times better than IE, but she refuses to let me put it on our home computer. Well, one day I was using the home computer to do some Javascript programming and…I accidentally let loose an infinite loop. Internet Explorer did what some computer scientists like to call “throwing up.” It flipped out, and I was very unhappy. Firefox is much more polite when it comes to telling you that there’s an infinite loop going on in your program. I believe there’s a pop-up that tells you that something’s going wrong and asks you if you want to make it stop. I can’t tell you how other browsers take an infinite loop, but I know IE will make you miserable. (Even if you’d rather not use Firefox permanently, use it for Javascript programming. Plus, I think everyone should have at least two browsers on their computer. Ask me, I’ll give you a couple reasons.)
Oh, I never told you what an infinite loop actually is. Well, they’re evil. They happen to everyone every once in a while. They usually happen with definite iteration loops (at least for me), but they can happen either way. Basically, your computer starts to execute the loop and can’t stop.
An example in pseudocode with definite iteration:
number=0
while(number<5){
print number
}
The problem with that is that the number never changes. You're always going to print the same number forever and ever and ever. A bunch of zeros. Ugh.
Here's how you SHOULD do it:
number=0
while(number<5){
print number
number=number+1
}
That makes the number change each time through the loop and eventually the number will be equal to five and the number won't be printed. The output will be…
0 1 2 3 4

The same thing happens with indefinite iteration. If there's no way for the end condition to occur (when you're jumping on one foot, my mouth is taped shut so I can't tell you to stop), then the loop just keeps going.
(This post is in memory of my MacBook, which drowned when a cup of water blew off my windowsill. I'm normally good to my computers; the one I have now is almost eight years old and still going. It won't charge now, and I'm going to see if it just needs a new battery or if it's completely screwed.)

Comments are closed.