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 execution of the statements they group. Other control flow statements include 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;
}
}
}



Here is the new output:

1: 1
2: 1
3: 2 *
4: 3
5: 5
6: 8 *
7: 13
8: 21
9: 34 *



To number the elements of the sequence, we used a for loop instead of a while loop. A for loop is shorthand for a while loop, with an initialization and increment section added. The for loop in ImprovedFibonacci is equivalent to this while loop:

int i = 2; // define and initialize loop index
while (i <= MAX_INDEX) {
// ...generate the next Fibonacci number and print it...
i++; // increment loop index
}



The use of the for loop introduces a new variable declaration mechanism: the declaration of the loop variable in the initialization section. This is a convenient way of defining loop variables that need exist only while the loop is executing, but it applies only to for loopsnone of the other control-flow statements allow variables to be declared within the statement itself. The loop variable i is available only within the body of the for statement. A loop variable declared in this manner disappears when the loop terminates, which means you can reuse that variable name in subsequent for statements.

The ++ operator in this code fragment may be unfamiliar if you're new to C-derived programming languages. The ++ operator increments by one the value of any variable it abutsthe contents of variable i in this case. The ++ operator is a prefix operator when it comes before its operand, and postfix when it comes after. Similarly, minus-minus (--) decrements by one the value of any variable it abuts and can also be prefix or postfix. In the context of the previous example, a statement like

i++;



is equivalent to

i = i + 1;



Expressions where the value assigned to a variable is calculated from the original value of the variable are common enough that there is a short-hand for writing them. For example, another way to write i= i+ 1 is to write

i += 1;



which adds the value on the right-hand side of the += operator (namely 1) to the variable on the left-hand side (namely i). Most of the binary operators (operators that take two operands) can be joined with = in a similar way (such as +=, -=, *=, and /=).

Inside the for loop body we use an ifelse statement to see whether the current hi value is even. The if statement tests the boolean expression between the parentheses. If the expression is TRue, the statement (which can be a block) in the body of the if is executed. If the expression is false, the statement in the body of the else clause is executed. The else part is optional. If the else is not present, nothing is done when the expression is false. After figuring out which (if any) clause to execute, and then actually executing it, control passes to the code following the body of the if statement.

The example tests whether hi is even using the %, or remainder, operator (also known as the modulus operator). It produces the remainder after dividing the value on the left side by the value on the right. In this example, if the left-side value is even, the remainder is zero and the ensuing statement assigns a string containing the even-number indicator to mark. The else clause is executed for odd numbers, setting mark to an empty string.

The println invocations appear more complex in this example because the arguments to println are themselves expressions that must be evaluated before println is invoked. In the first case we have the expression "1: "+ lo, which concatenates a string representation of lo (initially 1) to the string literal "1: "giving a string with the value "1: 1". The + operator is a concatenation operator when at least one of its operands is a string; otherwise, it's an addition operator. Having the concatenation operation appear within the method argument list is a common short-hand for the more verbose and tedious:

String temp = "1: " + lo;
System.out.println(temp);



The println invocation within the for loop body constructs a string containing a string representation of the current loop count i, a separator string, a string representing the current value of hi and the marker string.

We Are Founder..