Java Reference
In-Depth Information
1.6. Flow of Control
"Flow of control" is the term for deciding which statements in a program
are executed and in what order. The while loop in the Fibonacci program
is one control flow statement, as are blocks, which define a sequential ex-
ecution of the statements they group. Other control flow statements in-
clude for , ifelse , switch , and dowhile . We change the Fibonacci sequence
program by numbering the elements of the sequence and marking even
numbers with an asterisk:
class ImprovedFibonacci {
static final int MAX_INDEX = 9;
/**
* Print out the first few Fibonacci numbers,
* marking evens with a '*'
*/
public static void main(String[] args) {
int lo = 1;
int hi = 1;
String mark;
System.out.println("1: " + lo);
for (int i = 2; i <= MAX_INDEX; i++) {
if (hi % 2 == 0)
mark = " *";
else
mark = "";
System.out.println(i + ": " + hi + mark);
hi = lo + hi;
lo = hi - lo;
}
}
}
 
Search WWH ::




Custom Search