Loops

Your Basic while Loop
The most basic of all looping statements in Java is while. The while statement creates a type of loop that’s called a while loop, which is simply a loop that executes continuously as long as some conditional expression evaluates to true. while loops are useful in all sorts of programming situations, so you use while loops a lot.

The while statement
The basic format of the while statement is like this:
while (expression)
statement
The while statement begins by evaluating the expression. If the expression is true, statement is executed. Then, the expression is evaluated again, and the whole process repeats. If the expression is false, statement is not executed, and the while loop ends.
Note that the statement part of the while loop can either be a single statement or a block of statements contained in a pair of braces. Loops that have just one statement aren’t very useful, so nearly all the while loops you code use a block of statements. (Well, okay, sometimes loops with a single statement are useful. It isn’t unheard of. Just not all that common.)
A counting loop
Here’s a simple program that uses a while loop to print the even numbers from 2 through 20:
public class EvenCounter
{
public static void main(String[] args)
{
int number = 2;
while (number <= 20)
{
System.out.print(number + “ “);
number += 2;
}
System.out.println();
}
}
If you run this program, the following output is displayed:
2 4 6 8 10 12 14 16 18 20

The conditional expression in this program’s while statement is number <= 20. That means the loop repeats as long as the value of number is less than or equal to 20. The body of the loop consists of two statements. The first prints the value of number followed by a space to separate this number from the next one. Then, the second statement adds 2 to number.

The Famous for Loop
In addition to while and do-while loops, Java offers one more kind of loop: the for loop.
The basic principle behind a for loop is that the loop itself maintains a counter variable — that is, a variable whose value is increased each time the body of the loop is executed. For example, if you want a loop that counts from 1 to 10, you’d use a counter variable that starts with a value of 1 and is increased by 1 each time through the loop. Then, you’d use a test to end the loop when the counter variable reaches 10. The for loop lets you set this up all in one convenient statement.
People who majored in Computer Science call the counter variable an iterator.
They do so because they think we don’t know what it means. But we know perfectly well that the iterator is where you put your beer to keep it cold.

The for loop follows this basic format:
for (initialization-expression; test-expression; countexpression)
statement;

The three expressions in the parentheses following the keyword for control how the for loop works. The following paragraphs explain what these three expressions do:
✦ The initialization expression is executed before the loop begins. Usually, you use this expression to initialize the counter variable. If you haven’t declared the counter variable before the for statement, you can declare it here too.
✦ The test expression is evaluated each time the loop is executed to determine whether the loop should keep looping. Usually, this expression tests the counter variable to make sure it is still less than or equal to the value you want to count to. The loop keeps executing as long as this expression evaluates to true. When the test expression evaluates to false, the loop ends.
✦ The count expression is evaluated each time the loop executes. Its job is usually to increment the counter variable.

Here’s a simple for loop that displays the numbers 1 to 10:
public class CountToTen
{
public static void main(String[] args)
{
for (int i = 1; i <= 10; i++)
System.out.println(i);
}
}
This for loop apart has the following pieces:
✦ The initialization expression is int i = 1. This expression declares a variable named i of type int and assigns it an initial value of 1.
✦ The test expression is i <= 10. As a result, the loop continues to execute as long as i is less than or equal to 10.
✦ The count expression is i++. As a result, each time the loop executes, the variable i is incremented.
✦ The body of the loop is the single statement System.out. println(i). As a result, each time the loop executes, the value of the i variable is printed.

No comments: