4.3 Simple for Statements
Processing's final loop statement is the for statement. Returning one last time to
our cooking analogy, we might have instructions for eliminating lumpiness
phrased in the form "stir mixture for 100 strokes". Here we are not asked
to check on the state of the mixture before or after each stroke - we just
keep stirring until we have done one hundred repetitions. We can write
these instructions in a Processing-like form as follows:
int count;
for (count = 0; count < 100; count++)
give sauce a stir;
To see exactly what this means, we can rewrite it using an equivalent
fragment that uses a while format, as follows:
int count = 0;
while (count < 100)
{
give sauce a stir;
count++;
}
By comparing the for and while forms, we can see that the first
expression inside the parentheses of the for is an initializer, the second
expression (which must be of type boolean) is a condition that controls
the loop, and the third expression is a modifier that is evaluated after each
execution of the body of the loop. The next example shows how an actual
Processing for statement works. The example uses a for loop to find the value
of the sum of a series.
Example 1 The following fragment finds the sum of the series 12 +22 + ... + 1002. In
the fragment, the variables i and sum are both of type int.
sum = 0;
for (i = 1; i <= 100; i++)
sum += i*i;
This for statement first initializes i to 1. Then it repeatedly checks that
i ≤ 100 and, if it is, it increments sum by i*i and increments i by l.
|
|
The general form of Processing's for statement follows the form seen in the
example. The syntax of a for statement is given below.
for (<expression1>; <expression2>; <expression3>)
<statement>
As we did with our cooking example, we can write a general for statement
using a while statement. The form of the for statement shown above
is exactly equivalent to the following fragment.
<expressionl> ;
while (<expression2>)
{
<statement>
<expression3> ;
}
Although the for and the while are equivalent, the format of the for often
makes the loop's action much clearer to the reader. The structure of a for
statement and its relationship to a while statement may be made clearer
by studying its flow chart.
Often, for statements are controlled by counters (as we saw in Example
1 where the variable i controlled the loop). Since we often need
such loop control variables, Processing allows us to declare them right in the for
statement as the next example shows.
Example 2 The program fragment shown here prints values of the function y = x3 - 3
for x taking values -6, -4, ... ,6.
for (int x = -6; x <= 6; x += 2)
println(x +" "+ x*x*x - 3);
|
|
Variables declared in the initialization part of a for statement are only
usable inside that statement. Thus, in Example 2, the variable x is only
usable inside the for. If we added the statement
println(x);
after the for, we would get an error message because a variable declared
within a statement is only known inside that statement.
Exercise 4.3
- What does each statement print?
for (int i = -3; i <= 3; i++)
print("*");
for (int countDown = 5; countDown> 0; countDown--)
println(countDown + "seconds");
for (char letter = 'P'; letter <= 'S'; letter++)
println("Give me a " + letter);
for (int i = 2; i < 100; i *= i)
println(i);
- Write statements that will print a table of values of the function f(x) = 2x + 5 for the indicated values of x.
- x = 6,5,4, ... ,0
- x = 0,3,6, ... ,30
- x = -15,-10,-5, ... ,15
- x = 1,2,4,8, ... ,1024
Write a fragment that uses a for statement to perform the indicated
action.
- Set the float variable sum to the value of
- Set the float variable sum to the value of
- Set the long variable product to the value of
1 x 2 x 3 x ... x 20
- Set the int variable total to the value of
(-12)3 + (-11)3 + (-10)3 + ... + (20)3
- Set the float variable sum to the value of
Suppose that a large piece of paper with an area of 1.0 m2 and a
thickness of 0.090 mm is cut in half and the two pieces are stacked,
one on top of the other. Suppose further that the process of cutting
in half and stacking is repeated over and over again. Write a program
to find both the thickness of the pile and the area of each piece after
the procedure has been carried out forty times.
Write a program that reads a positive integer n and then prints an
"n-times table" containing values up to n x n. For example, if the
program reads the value 5, it should print
Assume that the input is valid.
   a.    Write a program that will print a table containing the integers
from one to forty along with their squares, square roots, and
reciprocals.
   b.    Modify your program so that it inserts a blank line after every
fifth line of the table.
| |