The for Loop Structure

Java provides the for loop to let you repeat blocks of code under control of a loop counter variable. The general form of the for loop is a for statement followed by a loop body. The for statement consists of three sections, as shown in the following code:

for( initialization ; logical test ; update )

When a for statement is executed, the first thing that happens is that the initialization expressions, if any, are executed. Next, the logical test expression, if present, is executed—if the result is false, the loop is exited immediately. If there is no logical test or the result is true, the loop body statements are executed. Finally, the update expressions, if present, are executed and the logical test is repeated.

Get Java 2™ Programmer Exam Cram™ 2 (Exam CX-310-035) 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.