Loops

Let's once again have a look at the code of the string sort. The for loop inside the while loop will go through all the elements from the first element (indexed with zero in Java) up to the last (indexed with n-1). Generally, this for loop has the same syntax as in C:

for( initial expression ; condition ; increment expression )
  block

First, the initial expression is evaluated. It may contain a variable declaration, as in our example. The j variable in the preceding example is visible only inside the block of the loop. After this, the condition is evaluated, and after each execution of the block, the increment expression is executed. The loop repeats so long as the condition is true. If the condition is false right after the execution ...

Get Java Projects - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.