Presentation is loading. Please wait.

Presentation is loading. Please wait.

The for-statement.

Similar presentations


Presentation on theme: "The for-statement."— Presentation transcript:

1 The for-statement

2 Previously discussed: different loop-statements in Java
Java provides 3 types of loop-statements: We will now study the for-statement 1. The while-statement 2. The for-statement 3. The do-while-statement  

3 Previously discussed: find all divisor of a number n
Algorithm to find all divisors of a number 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) for (x = 1, 2, 3, ..., n) do { if ( n is divisible by x ) print x; // x is a divisor of n }

4 The for-statement The for-statement is ideally suited for making the following type of program: The for-statement can best be explained with an example.... 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.

5 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

6 Example for-statement: the most common way to use a for-statement (cont.)
The most common form of the for-statement is as follows: for ( var = START_VALUE ; var <= STOP_VALUE ; var = var + INCR ) { /* for-body (statements) */ }

7 Example for-statement: the most common way to use a for-statement (cont.)
Meaning: The variable var will take on the following values: For each of the value, the statements in the for-body are executed START_VALUE START_VALUE + INCR START_VALUE + 2×INCR ... Up to the largest value that is ≤ STOP_VALUE

8 Example for-statement: the most common way to use a for-statement (cont.)
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

9 Example for-statement: the most common way to use a for-statement (cont.)
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

10 Example for-statement: the most common way to use a for-statement (cont.)
Example Program: (Experiment with it yourself outside class !)               Prog file: How to run the program:             Right click on link and save in a scratch directory To compile:   javac For01.java To run:          java For01

11 Example for-statement: the most common way to use a for-statement (cont.)
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

12 Example for-statement: the most common way to use a for-statement (cont.)
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

13 Example for-statement: the most common way to use a for-statement (cont.)
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

14 Example for-statement: the most common way to use a for-statement (cont.)
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

15 Example for-statement: the most common way to use a for-statement (cont.)
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

16 Example for-statement: the most common way to use a for-statement (cont.)
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!!

17 Example for-statement: the most common way to use a for-statement (cont.)
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");

18 Example for-statement: the most common way to use a for-statement (cont.)
Answer: 4 7 (4 + 1×3) 10 (4 + 2×3) 13 (4 + 3×3) Done

19 Syntax and meaning of the for-statement
The general syntax of the for-statement is as follows:

20 Example for-statement: the most common way to use a for-statement (cont.)
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 ≤ So we stop

21 Syntax and meaning of the for-statement (cont.)
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-statement: this is a statement that is executed once at the start of the for-statement

22 Syntax and meaning of the for-statement (cont.)
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. A INCR-STATEMENT: this is a statement that is executed at the end of the loop.

23 Syntax and meaning of the for-statement (cont.)
The INCR-STATEMENT is executed: After the for-loop body Before the for-statement repeats to test the LOOP-CONTINUATION-CONDITION

24 Syntax and meaning of the for-statement (cont.)
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-STATEMENT is executed before repeating the check of the LOOP-CONTINUATION-CONDITION

25 Common practice in for-statements
Common practice in for-loops: Use a block as body of for-statements

26 Common practice in for-statements (cont.)
A typical for-loop looks like this:

27 Flow chart of a for-statement
(I have used colors to highlight the correspondence of the pieces in the Java language syntax and in the flow chart)

28 Flow chart of a for-statement (cont.)
Flow chart representing the for-statement:

29 Flow chart of a for-statement (cont.)
This is the program flow when LOOP-CONTINUATION-CONDITION is true:

30 Flow chart of a for-statement (cont.)
In this case, the for-statement will: Execute the statements in the body Execute the INCR-STATEMENET Repeat the for-statement from the LOOP-CONTINUATION-CONDITION

31 Flow chart of a for-statement (cont.)
This is the program flow when LOOP-CONTINUATION-CONDITION is false:

32 Flow chart of a for-statement (cont.)
In this case, the for-statement will: Terminate The program will proceed with the statement that follows the for-statement

33 Flow chart of a for-statement (cont.)
Example: for ( a = 1 ; a <= 10 ; a++ ) { System.out.println(a); } System.out.println("Done");

34 Flow chart of a for-statement (cont.)
Flow chart of this program:

35 Flow chart of a for-statement (cont.)
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");

36 Flow chart of a for-statement (cont.)
Execution: Program "loops" as long as a ≤ 10 Program exits loop when a > 10

37 Structure diagram of a for-statement

38 Structure diagram of a for-statement (cont.)
Structure diagram representing a while-statement:

39 Structure diagram of a for-statement (cont.)
How to "read" the diagram: The outer rectangle containing the LOOP-CONTINUATION-CONDITION represents the for-statement:

40 Structure diagram of a for-statement (cont.)
The for-statement is one (single) statement) The for-statement will only complete execution when the LOOP-CONTINUATION-CONDITION becomes false

41 Structure diagram of a for-statement (cont.)
The inner rectangle (shaded with a green color) is the while-loop body When the LOOP-CONTINUATION-CONDITION is true, the for-statement will execute: The statements in the inner rectangle (the for-statement's body) The INCR-statement


Download ppt "The for-statement."

Similar presentations


Ads by Google