Java Reference
In-Depth Information
Initial expression
logical
expression
update
expression
true
statement
false
5
FIGURE 5-4 for loop
The for loop executes as follows:
1. The initial expression executes.
2. The logical expression is evaluated. If the loop condition evalu-
ates to true :
a. Execute the body of the for loop.
b. Execute the updatestatement (the third expression in the parentheses).
3. Repeat Step 2 until the loop condition evaluates to false .
The initial statement usually initializes a variable (called the for loop control, or
indexed, variable).
As the name implies, the initial expression in the for loop is the first statement
to execute; it executes only once.
Primarily, for loops are used to implement counter-controlled loops. For this reason, the
for loop is typically called a counted or indexed for loop. Next, we give various
examples to illustrate how a for loop works.
 
Search WWH ::




Custom Search