|
Spring Semester 2002 |
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++)
System.out.print(i * j % 10);
System.out.println();
}
i is not changed in the loop body.
for (i = 1; i <= 10; i++) ... for (i = 0; i < 10; i++) ... for (i = 10; i > 0; i--) ... for (i = -10; i <= 10; i++) ... for (i = 10; i >= 0; i++) ... for (i = -10; i <= 10; i = i + 2) ... for (i = -10; i <= 10; i = i + 3) ...
for loop into a while loop.
int s = 0; for (int i = 1; i <= 10; i++) s = s + 1;
do loop into a while loop.
int n = 1;
double x = 0;
double s;
do {
s = 1.0 / (n * n);
x = x + s;
n++;
} while (s > 0.01);
System.in -- describe both methods.
Explain how the "end of file" is signaled in both cases.
Then look at the directory listing. How many characters does the file contain? Remember to count the newline characters. (In DOS you may be surprised that the count is not what you expect. DOS text files store each newline as a two-character sequence. The input readers and output streams automatically translate between this carriage return / line feed sequence used by the files and theHello cruel world
'\n' character used by Java programs,
so you don't need to worry about it.) Why does this prove that there is no "end of file"
character? Why do you nevertheless need to type Ctrl+Z / Ctrl+D to end console input?
System.in
into tokens. What are the resulting tokens?"Hello, cruel world!"
name of bridge length of bridgeHere the name of the bridge can be a single word ("Brooklyn") or consist of several words ("Golden Gate"). The length is a floating-point number.
loop {
read employee name
if not OK, exit loop
read employee salary
if not OK, exit loop
give employee 5% raise
print employee data
}
Use a boolean variable, a break statement, and a method
with multiple return statemtens. Which of these three approaches do you
find clearest?
Hint: consider the number of data sets and the origin of the data (keyboard input vs. file input).
for loop in which symmetric bounds are more natural. Give an example of a
for loop in which asymmetric bounds are more natural.
Give an example where a nested loop is typically used.