Activity 6.3.1.
Run the while loop below. Can you change it to repeat 20 times? Click on Show CodeLens to step through the code with the Next button.
while structure:
while-structure to code a counting loop, Javaβs for statement is ideally suited for this purpose. For example, the following for statement will also print the word βHelloβ 10 times:
for loop is equivalent to the preceding while loop. The for loop is almost a shortcut way to write a while loop with all three steps that you need in one line.
for( initializer; loop entry condition ; updater )
{ for loop body }
for, which is followed by a parenthesized list of three expressions separated by semicolons: an initializer, a loop entry condition, and an updater. Following the parenthesized list is the for loop body, which is either a single statement or a sequence of statements contained in curly brackets, {β¦}.

for statement.for statement works. It might be useful to compare this flowchart with the flowchart for the the while structure (FigureΒ 6.3.4), which was introduced in ChapterΒ 3. You see that it has exactly the same structure. First, the initializer is evaluated. Thus, the initializer sets the integer variable k to 0. Then the loop entry condition, which must be a boolean expression, is evaluated. If it is true, the body of the loop is executed; if it is false, the body of the loop is skipped and control passes to the next statement following the for statement. The updater is evaluated after the loop body is executed. After completion of the updater, the loop entry condition is reevaluated and the loop body is either executed again or not, depending on the truth value of the loop entry condition. This process is repeated until the loop entry condition becomes false.

while statement and while structure.for loop components are evaluated gives this sequence:
evaluate initializer
evaluate loop entry condition ==> True
execute for loop body;
evaluate updater
evaluate loop entry condition ==> True
execute for loop body;
evaluate updater
evaluate loop entry condition ==> True
execute for loop body;
evaluate updater
.
.
.
evaluate loop entry condition ==> False
for statement. This restricts the variableβs scope to the for statement itself. It would be a syntax error to use k outside the scope of the for loop, as in this example:
for (int k = 0; k < 100; k++)
System.out.println("Hello");
// Syntax error, k undeclared
System.out.println("k = " + k);
for statement, in which case the variable should be declared before the for statement. For example, if we want to print the value of the loop variable, k, after the loop completes, we have to declare it before the loop:
int k = 0; // Declare the loop variable here
for (k = 0; k < 100; k++)
System.out.println("Hello");
System.out.println("k = " + k); // To use it here
k becomes 100, so βk = 100β will be printed.
10 9 8 7 6 5 4 3 2 1 BLASTOFF. In this case, progress toward the loop bound is made by decrementing the loop counter:
for statements will result in an infinite loop?
for (int k = 0; k < 100; k--) // Infinite loop
System.out.println("Hello");
for (int k = 1; k != 100; k+=2) // Infinite loop
System.out.println("Hello");
for (int k = 98; k < 100; k = k / 2)// Infinite loop
System.out.println("Hello");
CONTROL-C or CTRL-ALT-DELETE or CONTROL-APPLE-ESCAPE or refreshing the page in an online environment, but if that doesnβt work you will have to reboot the computer, possibly causing a loss of data. The best way to avoid infinite loops is to determine that the loopβs updater expression will cause the loop to eventually reach its bound.
for (int k = 10; k > 0; k--) // Loop heading
System.out.print (k + " "); // Indent the body
System.out.println( "BLASTOFF" ) // After the loop
for (int k = 10; k > 0; k--)
System.out.print (k + " ");
System.out.println("BLASTOFF");
for (int k = 10; k > 0; k--) System.out.print(k +" ");
System.out.println("BLASTOFF");
for
(int k = 10; k > 0; k--)
System.out.print (k + " ");
System.out.println("BLASTOFF");
System.out.println("BLASTOFF"), is not part of the for loop body and is executed only once when the loop terminates.
println() statement, or the loop body may consist of any Java statement, including an if or if-else statement or a compound statement, which contains a sequence of statements enclosed within braces. Consider the following examples. The first example prints the sequence 0, 5, 10, 15, \(\dots\) 95. Its loop body consists of a single if statement:
for (int k = 0; k < 100; k++)// Print 0 5 10...95
if (k % 5 == 0) // Loop body: single if statement
System.out.println("k= " + k);
char, and it counts the letters of the alphabet. The loop body consists of a single print() statement:
for (char k = 'a'; k <= 'z'; k++)// Print 'a' 'b'...'z'
System.out.print (k + " "); // Loop body: single print()
for (int k = 1; k <= 10; k++) {// Print 5 10 15...50
int m = k * 5; // Begin body
System.out.print (m + " ");
} // End body
for statement consists of the statement that immediately follows the for loop heading. This statement can be either a simple statement or a compound statementβ a sequence of statements enclosed within braces, { β¦ }.
for loop consists of a single statement, some coding styles recommend that braces should always be used for the body of a loop statement. For example, it is always correct to code the for loop as
for (int k = 1; k <= 10; k++) {// Print 1 2 ... 10
System.out.print (k + " "); // Begin body
} // End body
for loop statements.
for (int k = 0; k < 100; k = k )
System.out.println(k);
for (int k = 1; k == 100; k = k + 2 )
System.out.println(k);
for (int k = 1; k >= 100; k = k - 2 )
System.out.println(k);
for (int i = 0; i < 10; i++) {
int j;
j = j + 1;
}
for loop in the active code window below that prints the following sequence of numbers: 1, 5, 9, 13, 17, 21, 25.
for loop body contains a for loop. For example, suppose you are working for Giant Auto Industries, and your boss wants you to print a table for buyers to figure the cost of buying multiple quantities of a certain part. The cost of individual parts ranges from $1 to $9. The cost of N items is simply the unit price times the quantity. Thus, youβll want to print something like the following table of numbers, where the prices per unit are listed in the top row, and the prices for 2, 3 and 4 units are listed in subsequent rows:
1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
for loops:
row as its loop counter. The println() statement is executed after the inner loop is done iterating, which allows us to print a new row on each iteration of the outer loop. The inner loop prints the nine values in each row by printing the expression col * row. Obviously, the value of this expression depends on both loop variables.
for statement executed? The inner loop is executed once for each iteration of the outer loop. Thus, it is executed four times. How many times is the body of the inner loop executed? The body of the inner loop is executed 36 timesβ9 times for each execution of line 2.
# # # # #
# # # #
# # #
# #
#
row be the row number, then in each row we want to print 6 \(-\) row symbols. The following table shows the relationship we want:
| Row | Bound (6-row) | Number of Symbols |
| 1 | 6-1 | 5 |
| 2 | 6-2 | 4 |
| 3 | 6-3 | 3 |
| 4 | 6-4 | 2 |
| 5 | 6-5 | 1 |
for loop to print the following pattern in the active code window below:
#
# #
# # #
# # # #
# # # # #