Tutorial 4 Summary

Continuing from Flow Control: Part 1, we cover more of the essential flow control structures in this lesson. These are if-else-if-else statement nesting and for loops.

Using an if-else-if-else chain isn't really anything new. It is simply combining the if-else structure that we already know with additional if-else statements. For example, if we want to check several conditions before we move on to the else clause in our code, we can chain together if-else blocks as follows:

if (condition1) {
	// do something if condition1 is true
} else if (condition2) {
	// do another thing if condition2 is true
} else if (condition3) {
	// I think you're getting the picture
} else if (condition4) {
     .
     .
     .
} else {
	// finally do this if none of the above
	// conditions were true
}

Remember that with if-else-if-else, only one of those code blocks will be executed, because as Java moves down the chain, once it finds one of the conditions to be true, it executes whatever is in that code block and then moves past the chain.

We don't need to end an if-else-if-else chain with an else clause. We could have something like this:

if (condition1) {
	// We got lucky!
} else if (condition2) {
	// it's ok, second prize is still good
} else if (condition3) {
     .
     .
     .
} else if (almostDone) {
	// this prize must suck, like a pez dispenser
	// or something
}

Notice that the above if-else chain did not end with an else clause. This means that Java will check the conditions, going down the line as usual, and if none of them are true, then it simply moves on and doesn't execute any of the code blocks. In this case, the code either executes only one of the code blocks, or none at all.

Now we cover the king of loops, a.k.a. the for loop. OK, it's the king of loops in my mind, because I use it so much, but hopefully it is (or will be) in your mind too. A for loop is like a while loop, and you can actually use a while loop whenever you use a for loop. Take a look at the following while loop:

do one thing;
while (condition) {
	// do stuff
	do thing at end;
}

This structure is used so much that there is actually a whole type of loop that is made for it: the for loop. The following for loop does exactly what the above while loop does:

for (do one thing; condition; do thing at end) {
	// do stuff
}

If you compare the above two loops (and remember that they are exactly the same), then you will notice that the "do one thing" part is done right before we start the loop, so we like to think of it as the initialization. Then, of course, we check the condition. If it is true, we execute what is in the loop, and then we execute the "do thing at end" line.

OK, I lied: the for and while loops we compared above are actually not exactly the same, but the difference between them is very small. Don't worry about it right now, but if we declare a variable in the "do one thing" part, then in the while loop example, we can use that variable after the loop, but in the for loop example, we can only use that variable inside the loop. In nerdy terms, we say that the variable declared in the initialization has a scope that is limited to inside the for loop. We will talk about this more later, but I just needed to say that so that I can stop those angry e-mails from the Sun developers before they happen.

Here is an example of a for loop that you will see and use all the time:

for (int i = 0; i < 10; i=i+1) {
	// do some awesome stuff in the loop!
}

As a side note, the statement "i = i+1" is so common that we actually have an abbreviation for it: "i++". Whenever you see "i++", you can just think of "i = i+1". In the same way, we also have "i--" that stands for "i = i-1".

Now we can write this loop as the following:

for (int i = 0; i < 10; i++) {
	// do some awesome stuff in the loop!
}

As another example, we can use a for loop to sing a song:

for (int i = 99; i > 0; i--) {
    System.out.println(i + " bottles of beer on the wall");
}


Like the site?
Let people know:
Digg! StumbleUpon
			Toolbar del.icio.us