for Loops

In your programming, you find many circumstances in which a loop is useful. You can use them to keep doing something several times, such as an antivirus program that opens each new email received to look for viruses. You also can use loops to cause the computer to do nothing for a brief period, such as an animated clock that displays the current time once per minute.

A loop statement causes a computer program to return to the same place more than once, like a stunt plane completing an acrobatic loop.

Java’s most complex loop statement is for. A for loop repeats a section of a program a fixed number of times. The following is an example:

for (int dex = 0; dex < 1000; dex++) {
    if (dex % 12 == 0) {
        System.out.println("#: " + dex);
    }
}

This loop displays every number from 0 to 999 evenly divisible by 12.

Every for loop has a variable that determines when the loop should begin and end. This variable is called the counter (or index). The counter in the preceding loop is the variable dex.

The example illustrates the three parts of a for statement:

• The initialization section: In the first part, the dex variable is given an initial value of 0.

• The conditional section: In the second part, there is a conditional test like one you might use in an if statement: dex < 1000.

• The change section: The third part is a statement that changes the value of the dex variable, in this example by using the increment operator.

In the initialization section, you set up the counter variable. You can create the variable in the for statement, as the preceding example does with the integer variable dex. You also can create the variable elsewhere in the program. In either case, you should give the variable a starting value in this section of the for statement. The variable has this value when the loop starts.

The conditional section contains a test that must remain true for the loop to continue looping. When the test is false, the loop ends. In this example, the loop ends when the dex variable is equal to or greater than 1,000.

The last section of the for statement contains a statement that changes the value of the counter variable. This statement is handled each time the loop goes around. The counter variable has to change in some way or the loop never ends. In the example, dex is incremented by one in the change section. If dex was not changed, it would stay at its original value of 0 and the conditional dex < 1000 always would be true.

The for statement’s block is executed during each trip through the loop.

The preceding example had the following statements in the block:

if (dex % 12 == 0) {
    System.out.println("#: " + dex);
}

These statements are executed 1,000 times. The loop starts by setting the dex variable equal to 0. It then adds 1 to dex during each pass through the loop and stops looping when dex is no longer less than 1,000.


Note

An unusual term you might hear in connection with loops is iteration. An iteration is a single trip through a loop. The counter variable that is used to control the loop is called an iterator.


As you have seen with if statements, a for loop does not require brackets if it contains only a single statement. This is shown in the following example:

for (int p = 0; p < 500; p++)
    System.out.println("I will not sell miracle cures");

This loop displays the text “I will not sell miracle cures” 500 times. Although brackets are not required around a single statement inside a loop, you can use them to make the block easier to spot.

The first program you create during this hour displays the first 200 multiples of 9: 9 × 1, 9 × 2, 9 × 3, and so on, up to 9 × 200. In NetBeans, create a new empty Java file named Nines and enter the text in Listing 8.1. When you save the file, it is stored as Nines.java.

Listing 8.1. The Full Text of Nines.java


1: class Nines {
2:     public static void main(String[] arguments) {
3:         for (int dex = 1; dex <= 200; dex++) {
4:             int multiple = 9 * dex;
5:             System.out.print(multiple + " ");
6:         }
7:     }
8: }


The Nines program contains a for statement in Line 3. This statement has three parts:

• Initialization: int dex = 1, which creates an integer variable called dex and gives it an initial value of 1.

• Conditional: dex <= 200, which must be true during each trip through the loop. When it is not true, the loop ends.

• Change: dex++, which increments the dex variable by one during each trip through the loop.

Run the program by choosing Run, Run File in NetBeans. The program produces the following output:

Output


9 18 27 36 45 54 63 72 81 90 99 108 117 126 135 144 153 162 171
180 189 198 207 216 225 234 243 252 261 270 279 288 297 306 315
324 333 342 351 360 369 378 387 396 405 414 423 432 441 450 459
468 477 486 495 504 513 522 531 540 549 558 567 576 585 594 603
612 621 630 639 648 657 666 675 684 693 702 711 720 729 738 747
756 765 774 783 792 801 810 819 828 837 846 855 864 873 882 891
900 909 918 927 936 945 954 963 972 981 990 999 1008 1017 1026
1035 1044 1053 1062 1071 1080 1089 1098 1107 1116 1125 1134 1143
1152 1161 1170 1179 1188 1197 1206 1215 1224 1233 1242 1251 1260
1269 1278 1287 1296 1305 1314 1323 1332 1341 1350 1359 1368 1377
1386 1395 1404 1413 1422 1431 1440 1449 1458 1467 1476 1485 1494
1503 1512 1521 1530 1539 1548 1557 1566 1575 1584 1593 1602 1611
1620 1629 1638 1647 1656 1665 1674 1683 1692 1701 1710 1719 1728
1737 1746 1755 1764 1773 1782 1791 1800


The output window in NetBeans does not wrap text, so all the numbers appear on a single line. To make the text wrap, right-click the output pane and choose Wrap text from the pop-up menu.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.223.82