### The for-statement

• Previously discussed: different loop-statements in Java

• Java provides 3 types of loop-statements:

 The while-statement The for-statement The do-while-statement

• We will now study the for-statement

• Previously discussed: find all divisor of a number n

• Algorithm to find all divisors of a number n:

 ``` for (x = 1, 2, 3, ..., n) do { if ( n is divisible by x ) { print x; // x is a divisor of n } } ```

The variable x takes on the values 1, 2, 3, ..., n one at a time

For each value, we check whether n is divisible by x

If so, we print the value x (and obtain all divisors)

• The for-statement

• The for-statement is ideally suited for making the following type of program:

• Let some variable x take on a series of value one at a time

• For each value taken on by a variable x, the body of the for-statement is executed once.

Example:

 ``` /* --------------------------------------------------------- Execute a statement for each of the value 1, 2, ..., 10 --------------------------------------------------------- */ repeat ( for x = 1, 2, 3, .... 10 ) execute this statement; ```

• The for-statement can best be explained with an example....

• Example for-statement: the most common way to use a for-statement

• The for-statement was originally invented to let some variable take on a series of value

• The most common form of the for-statement is as follows:

 ``` for ( var = START_VALUE ; var <= STOP_VALUE ; var = var + INCR ) { /* for-body (statements) */ } ```

Meaning:

• The variable var will take on the following values:

 START_VALUE START_VALUE + INCR START_VALUE + 2×INCR ... Up to the largest value that is ≤ STOP_VALUE

• For each of the value, the statements in the for-body are executed

• In other words:

 ``` repeat ( for var = START_VALUE, START_VALUE+INCR, START_VALUE+2×INCR, ..., STOP_VALUE ) execute statements in the for-body (one time for each value in the list); ```

• Example 1:

 ``` public class For01 { public static void main(String[] args) { int a; // Example "for-statement" for ( a = 1 ; a <= 10 ; a = a + 1 ) { System.out.println(a); // Print a } System.out.println("Done"); } } ``` ``` Output: 1 2 (1+1×1) 3 (1+2×1) 4 (1+3×1) 5 (1+4×1) 6 (1+5×1) 7 (1+6×1) 8 (1+7×1) 9 (1+8×1) 10 (1+9×1) Done ```

Notice that in the for-statement:

 a = 1 specifies the starting value a <= 10 specifies when the for-loop will stop a = a + 1 specifies how the variable a will change each time through the loop

• Example Program: (Experiment with it yourself outside class !)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac For01.java To run:          java For01

• Example 2: changing the START_VALUE

 ``` public class For01 { public static void main(String[] args) { int a; // Changing the starting value: for ( a = 5; a <= 10; a = a + 1 ) { System.out.println(a); // Print a } System.out.println("Done"); } } ``` ``` Output: 5 6 (5+1×1) 7 (5+2×1) 8 (5+3×1) 9 (5+4×1) 10 (5+5×1) Done ```

Notice that in the for-statement:

 a = 5 specifies the starting value a <= 10 specifies when the for-loop will stop a = a + 1 specifies how the variable a will change each time through the loop

• Example 3: changing the STOP_VALUE

 ``` public class For01 { public static void main(String[] args) { int a; // Changing the continuation condition: for ( a = 1; a <= 3; a = a + 1 ) { System.out.println(a); // Print a } System.out.println("Done"); } } ``` ``` Output: 1 2 (1+1×1) 3 (1+2×1) Done ```

Notice that in the for-statement:

 a = 1 specifies the starting value a <= 3 specifies when the for-loop will stop a = a + 1 specifies how the variable a will change each time through the loop

• Example 4: changing the INCR (increment) value

 ``` public class For01 { public static void main(String[] args) { int a; // Changing the increment statement: for ( a = 1; a <= 10; a = a + 2 ) { System.out.println(a); // Print a } System.out.println("Done"); } } ``` ``` Output: 1 3 (1+1×2) 5 (1+2×2) 7 (1+3×2) 9 (1+4×2) Done ```

Notice that in the for-statement:

 a = 1 specifies the starting value a <= 10 specifies when the for-loop will stop a = a + 2 specifies how the variable a will change each time through the loop: increment by 2!!

• Class exercise: what is the output of this for-loop

 ``` public class For01 { public static void main(String[] args) { int a; // Changing the increment statement: for ( a = 4; a <= 14; a = a + 3 ) { System.out.println(a); // Print a } System.out.println("Done"); } } ```

 ``` 4 7 (4 + 1×3) 10 (4 + 2×3) 13 (4 + 3×3) Done ```

Because:

• We start with the value 4
• We increase the value with 3 each time
• Hence: 4, 7, 10, 13
• The next value in the series is 16, but 16 is not ≤ 14... So we stop

• Syntax and meaning of the for-statement

• The general syntax of the for-statement is as follows:

Explanation:

• The keyword for announces (to the Java compiler) that we started an for-statement

• Following the keyword for, there are 3 clauses:

1. An init-expression: this is an expression that is evaluated once at the start of the for-statement

• Recall that the = symbol is an operator that returns a value

We saw assignment expressions in this webpage: click here

• The init-expression is usually an assignment expression of the following form (but it can be very complicated):

 ``` i = 0 i = 1 i = a + 1 and so on.... ```

2. A LOOP-CONTINUATION-CONDITION: this is a Boolean expression that determines whether the for-statement will be terminated

(This clause has the same function as in the LOOP-CONTINUATION-CONDITION of an while-statement)

3. The incr-expression: this is an expression that is evaluated at the end of the loop.

When is the incr-expression is evaluated:

 After the statements in the for-loop body Before the for-statement repeats to test the LOOP-CONTINUATION-CONDITION

The incr-expression is used to make the for-loop advance to the next (possible solution) case

To make the advancement, the incr-expression is usually an assignment expression of the following form (but it can be very complicated):

 ``` i = i + 1 (move up by 1) i = i - 1 (move down by 1) i = i + 2 (move up by 2) i = i + a (move up by a) and so on.... ```

• The for-statement is completed with one statement

 This is the body of the for-statement The body will be executed as long as the loop-continuation-condition is true !!!       After the body has been executed, the incr-expression is evaluated before repeating the check of the LOOP-CONTINUATION-CONDITION

• Common practice in for-statements

• Common practice in for-loops:

 Use a block as body of for-statements

• A typical for-loop looks like this:

• Flow chart of a for-statement

• For-statement:

(I have used colors to highlight the correspondence of the pieces in the Java language syntax and in the flow chart)

• Flow chart representing the for-statement:

• This is the program flow when LOOP-CONTINUATION-CONDITION is true:

In this case, the for-statement will:

 Execute the statements in the body Evaluate the incr-expression Repeat the for-statement from the LOOP-CONTINUATION-CONDITION

• This is the program flow when LOOP-CONTINUATION-CONDITION is false:

In this case, the for-statement will:

 Terminate The program will proceed with the statement that follows the for-statement

• Example:

 ``` for ( a = 1 ; a <= 10 ; a++ ) { System.out.println(a); } System.out.println("Done"); ```

Flow chart of this program:

Note: this is the same flow chart we saw before belonging to this program fragment:

 ``` a = 1; while ( a <= 10 ) { System.out.println(a); a++; } System.out.println("Done"); ```

• Execution:

 Program "loops" as long as a ≤ 10 Program exits loop when a > 10

• Structure diagram of a for-statement

• For-statement:

• Structure diagram representing a while-statement:

How to "read" the diagram:

• The outer rectangle containing the LOOP-CONTINUATION-CONDITION represents the for-statement:

The for-statement is one (single) statement)

The for-statement will only complete execution when the LOOP-CONTINUATION-CONDITION becomes false

• The inner rectangle (shaded with a green color) is the while-loop body

When the LOOP-CONTINUATION-CONDITION is true, the for-statement will:

 First execute the statements in the inner rectangle (the for-statement's body) Then (always followed by) evaluate the incr-expression