Java Reference
In-Depth Information
This will not compile: In a variable declaration the comma separates the
different variables being declared, and Cell is a type, not a variable. De-
clarations of different types of variables are distinct statements termin-
ated by semicolons. If you change the comma to a semicolon, however,
you get a for loop with four sections, not threestill an error. If you need
to initialize two different types of variables then neither of them can be
declared within the for loop:
int i;
Cell node;
for (i = 0, node = head;
i < MAX && node != null;
i++, node = node.next)
{
System.out.println(node.getElement());
}
Typically, the for loop is used to iterate a variable over a range of values
until some logical end to that range is reached. You can define what
an iteration range is. A for loop is often used, for example, to iterate
through the elements of a linked list or to follow a mathematical se-
quence of values. This capability makes the for construct more powerful
than equivalent constructs in many other languages that restrict for -
style constructs to incrementing a variable over a range of values.
Here is an example of such a loop, designed to calculate the smallest
value of exp such that 10 exp is greater than or equal to a value:
public static int tenPower(int value) {
int exp, v;
for (exp = 0, v = value - 1; v > 0; exp++, v /= 10)
continue;
return exp;
}
 
Search WWH ::




Custom Search