0% found this document useful (0 votes)
9 views1 page

Javanotes5 97

The document explains the structure and functionality of the for statement in programming, particularly focusing on the loop control variable and its common usage in counting loops. It illustrates examples of counting up and down, as well as variations in loop conditions that can lead to off-by-one errors. Additionally, it highlights the flexibility of the for statement syntax, allowing multiple expressions in both initialization and update parts.

Uploaded by

dsstudent05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views1 page

Javanotes5 97

The document explains the structure and functionality of the for statement in programming, particularly focusing on the loop control variable and its common usage in counting loops. It illustrates examples of counting up and down, as well as variations in loop conditions that can lead to off-by-one errors. Additionally, it highlights the flexibility of the for statement syntax, allowing multiple expressions in both initialization and update parts.

Uploaded by

dsstudent05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

3.4.

THE FOR STATEMENT 81

Usually, the initialization part of a for statement assigns a value to some variable, and the
update changes the value of that variable with an assignment statement or with an increment
or decrement operation. The value of the variable is tested in the continuation condition, and
the loop ends when this condition evaluates to false. A variable used in this way is called a
loop control variable. In the for statement given above, the loop control variable is years.
Certainly, the most common type of for loop is the counting loop, where a loop control
variable takes on all integer values between some minimum and some maximum value. A
counting loop has the form
for ( hvariable i = hmin i; hvariable i <= hmax i; hvariable i++ ) {
hstatements i
}

where hmini and hmax i are integer-valued expressions (usually constants). The hvariablei takes
on the values hmini, hmini+1, hmini+2, . . . , hmax i. The value of the loop control variable is
often used in the body of the loop. The for loop at the beginning of this section is a counting
loop in which the loop control variable, years, takes on the values 1, 2, 3, 4, 5. Here is an even
simpler example, in which the numbers 1, 2, . . . , 10 are displayed on standard output:
for ( N = 1 ; N <= 10 ; N++ )
[Link]( N );

For various reasons, Java programmers like to start counting at 0 instead of 1, and they
tend to use a “<” in the condition, rather than a “<=”. The following variation of the above
loop prints out the ten numbers 0, 1, 2, . . . , 9:
for ( N = 0 ; N < 10 ; N++ )
[Link]( N );

Using < instead of <= in the test, or vice versa, is a common source of off-by-one errors in
programs. You should always stop and think, Do I want the final value to be processed or not?
It’s easy to count down from 10 to 1 instead of counting up. Just start with 10, decrement
the loop control variable instead of incrementing it, and continue as long as the variable is
greater than or equal to one.
for ( N = 10 ; N >= 1 ; N-- )
[Link]( N );

Now, in fact, the official syntax of a for statemenent actually allows both the initialization
part and the update part to consist of several expressions, separated by commas. So we can
even count up from 1 to 10 and count down from 10 to 1 at the same time!
for ( i=1, j=10; i <= 10; i++, j-- ) {
[Link]("%5d", i); // Output i in a 5-character wide column.
[Link]("%5d", j); // Output j in a 5-character column
[Link](); // and end the line.
}

As a final example, let’s say that we want to use a for loop that prints out just the even
numbers between 2 and 20, that is: 2, 4, 6, 8, 10, 12, 14, 16, 18, 20. There are several ways to
do this. Just to show how even a very simple problem can be solved in many ways, here are
four different solutions (three of which would get full credit):

You might also like