Loops

Home | Graphical Version | Table of Contents | Next Chapter, "Introduction to Classes and Methods" | Previous Chapter, "Making Decisions"



The wonderful people who brought us Java wanted to eliminate redundancy, and what better way to do that then to allow a language to repeat itself (wait a minute, that made no sense). Well, a programming language needs to have loops almost as much as it needs to have "if" statements. One wonderful thing about looops, is their ability to eliminate reduncy by allowing you to have statements carried out multiple times without typing them more then once (see, now it makes sense). Again, loops in Java are very similar to loops in C/C++.
  1. The For Loop
  2. The While and Do Loops



The For Loop

The "for loop" is one of the first things budding BASIC programmers learn. It allows you to repeat a group of statements a given number of times. Allow me to demonstrate:
for (int p = 1 ; p <= 5 ; p++) {
	This code will be executed five times.
}
A "for loop" has three parameters seperated by semi-colons(;). First, is the variable declaration ("int p = 1"). This declares a local variable of the type you specify (in this case - int). Next, is the condition. Every time the statements in the braces are executed, the loop checks the condition. If the condition is true, the statements in the braces are executed again. This continues until the condition is proved false. Next, is the third part. After every execution of the loop, this statement is executed. In this case, every time the loop is executed, it adds one to the variable "p."
Now, we need a more worthwhile example. Let's say your teacher, in an attempt to annoy the heck out of you for getting on his nerves, wants you to add up all the numbers from one to one hundred. You might have heard the story about the kid who was given this assignment by an angry teacher (before $2,000+ computers). He used a mathematical equation to find his answer quickly and surprised the teacher, but we do not want to bother with silly mathematical equations. No, we can just use loops. Observe:
int answer = 0;
for (int p = 1 ; p <= 100 ; p++) {
	answer += p;
}
First, the loop created a variable named "p" with a value of one. Next, it checked the condition. It found that "p" was, indeed, less-than or equal to one hundred. Because the condition was true, it executed the statement. It added "p" to "answer." "p" was one, and "answer" was zero; so "answer" became one. The loop then increments the variable "p" because it is in the loops third parameter ("p++"). The loop continues to repeat this process until "p" reaches 100. The loop checks the condition, and finds it true once more (100 is less-than or equal to 100). It executes the statement again, and again, it increments "p." Now, it checks the condition for the final time. This time, the statement is false; so, the loop quits, and you have your answer. You show the teacher your answer, and now he's really annoyed. He says, "Well then smarty-pants, I want the sum of every even number between one and one hundred, or you'll have to write me a five hundred page report on the structure of the fruit fly." Still, you remain calm. You crack your knucles and calmy turn to your keyboard (it's a computer lab) knowing it has the answer for you. Here it is:
int answer = 0;
for (int p = 2 ; p <= 100 ; p += 2) {
	answer += p;
}
Well, you get to go home sans report. Here are a few more examples just to help you get the hang of it.
for (int p = 100 ; p >= 1 ; p--) { //Count backwords from one hundred
	Code
}

for (int p = 100 ; p >= 1 ; p-=2) { //Count backwords from one hundred skipping odd numbers
	Code
}
That's pretty much it except for one last thing. To exit out of a loop, just use the "break;" statement. All statements in the loop after the "break" statement will not be carried out and the loop will end.

The While and Do Loops

The while loop is similar to the for loop, but not the same. The while loop has a conditional, like so.

while (p == 10) {

First, the loop checks the condition, if the condition is true, the code in the loop is executed. The condition is then checked again. This continues until the check finds the condition to be false. Here is another example used just to prove a point.
int p = 5;
while (p !=5) {
	p = 6;
}
What is wrong with this code? Give up? The code inside the loop will never be executed because the first time the condition is checked, it fails (p is equal to five). You can force the loop to run at least once with a "do loop." Example:
int p = 5;
do {
	p = 6;
} while (p != 5);
The statement in the loop will execute at least once. In this example, the variable "p" is set to six. Then, the condition is checked. It is true (six does not equal five), and the loop will execute again. This loop will execute forever, and so, it is pretty useless. I'm sure you will find better use for the while loop. There is just one more thing worth mentioning. As with the "for loop," you can exit "while loops" and "do loops" with the "break" statement.



Our next chapter deals with the fundamentals of Object-Oriented Programming (OOP). I'm sure you will enjoy it.

Home | Back to Top | Table of Contents | Next Chapter, "Introduction to Classes and Methods" | Previous Chapter, "Making Decisions"