# The for statement

• Introduction

• One of the most common task that is of repetitive nature is process N items (e.g., compute the average - this requires the program to add N numbers)

• So, one of the most common loop constructs that programmers use has the form:

 ``` i = start_value; while ( i <= end_value ) { ... (do something with the ith item)... i++; } ```

• When this program is run, the variable i will assume the following values:
```  start_value, start_value+1, start_value+2, ....., end_value
```

• To help programmers write this construct with more clarity (so the purpose of the program can be understood easier), the for   statement is defined.

• Motivation: Prime Number

• Problem:

• Given an integer n
• Determine if n is prime.

• The following "psuedo" program can be used to solve the prime problem:

 ``` for each value f from 2 to n - 1 do: { compute n % f if (at least one remainder "n % f" is 0) then n is not prime else n is prime } ```

• We rewrite this "psuedo" program to something that is closer to a Java program ("refine") using a boolean variable "isPrime":

 ``` boolean isPrime; isPrime = true; // Assume that n is prime... for each value f from 2 to n - 1 do: { if (n % f == 0) then isPrime = false; // Now we know n is NOT prime... } ```

• To convert the psuedo program into a Java program, we can use a WHILE loop to run f from 2 to n - 1:

 ``` boolean isPrime; int f; isPrime = true; // Assume that n is prime... f = 2; while ( f <= n - 1 ) { if (n % f == 0) then isPrime = false; // Now we know n is NOT prime... f++; } ```

• Example Program: (Demo above code)

• Although we can write a program to solve the prime number problem, the program structure is not easily discerned by looking at the program itself.

We will now learn the for statement that let us write the prime number program with more clarity.

IMPORTANT

• Syntax of the for statement

• The syntax structure (the way to tell the computer) of the for   statement is as follows:

 ``` for ( initialization ; condition ; update ) ONE-single-statement // For body ```

• Again, the key to understanding the FOR statement is by working with many examples.

When you have written enough FOR   statements, I am sure that you will not only memorize this syntax, but can use it effectively.

IMPORTANT

• The meaning of the FOR   statement is:

 The initialization clause is executed first (and executed once only) The condition clause specifies a condition (e.g.: x > 0 or x + y < 0 , etc.) If the condition clause is satisfied , then the ONE-single-statement part is executed . After executing the ONE-single-statement, the update clause executed. After executing the update clause, the computer goes back to the condition clause and evaluate the condition clause again. The ONE-single-statement and the update clause are executed as long as the condition clause is TRUE When the Condition clause is NOT satisfied , then the statement part is SKIPPED OVER, and the program continues with the statement that follows the FOR statement.

• (1) Example of for statement

• Problem statement:

• Given an integer n
• Determine if n is prime.

• We saw that the following "psuedo" program can be used to solve the prime number problem:

 ``` boolean isPrime; isPrime = true; // Assume that n is prime... for each value f from 2 to n - 1 do: { if (n % f == 0) then isPrime = false; // Now we know n is NOT prime... } ```

• This "psuedo" program can be easily converted into a Java program using a FOR loop and the resulting program is much more lucid:

 ``` boolean isPrime; int f; isPrime = true; // Assume that n is prime... for ( f = 2; f <= n - 1 ; f++ ) { if (n % f == 0) then isPrime = false; // Now we know n is NOT prime... } ```

• Example Program: (Demo above code)

• (2) Example of for statement

• Problem statement:

• Given an integer n
• Compute the factorial of n

• The factorial of n is defined as:

 ``` n ! = 1 * 2 * 3 * ... * n ```

• The following "psuedo" program compute the factorial of n:

 ``` fac = 1; for ( i starting from 1 to n ) { fac = fac * i; } ```

• To see that the program compute n!, let us follow the execution of the for statement for n = 4

• fac = 1, i = 1, the program computes
```     fac = fac * i = 1 * 1 = 1
```

• fac = 1, i = 2, the program computes
```     fac = fac * i = 1 * 2 = 2
```

• fac = 2, i = 2, the program computes
```     fac = fac * i = 2 * 3 = 6
```

• fac = 6, i = 4, the program computes
```     fac = fac * i = 6 * 4 = 24
```

• The psuedo program can be easily converted into a Java program using a for statement:

 ``` int i; int fac; fac = 1; for ( i = 1; i <= n; i++ ) { fac = fac * i; } ```

• Example Program: (Demo above code)

• Self Check

• What will the following program print:

 ``` int i; for ( i = 0; i < 10; i++ ) { System.out.println( i ); } ```

```	0
1
2
3
4
5
6
7
8
9
```

• What will the following program print:

 ``` int i; for ( i = 0; i < 10; i++ ) { System.out.println( i ); i++; } ```

Answer: i is increased by 2 each time !

```        0
2
4
6
8
```

• What will the following program print:

 ``` for ( i = 0; i < 10; i++ ); System.out.println( i ); ```

```        The program is read as:

for ( i = 0; i < 10; i++  )
;

System.out.println( i );

The program will therefore print:

10

(Because i = 10 when the for loop stops)
```