The for-statement.ppt

Download Report

Transcript The for-statement.ppt

The for-statement
Previously discussed: different loopstatements in Java
• Java provides 3 types of loop-statements:
1. The while-statement
2. The for-statement
3. 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.
• 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
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) */
}
Example for-statement: the most common
way to use a for-statement (cont.)
• 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
Example for-statement: the most common
way to use a for-statement (cont.)
• 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
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
Example for-statement: the most common
way to use a for-statement (cont.)
• Example Program: (Experiment with it yourself outside
class !)
– Prog file:
http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/07/Progs/
For01.java
• 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 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
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
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
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
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
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!!
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");
}
}
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
Syntax and meaning of the for-statement
• The general syntax of the for-statement is as follows:
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 ≤ 14... So
we stop
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
Syntax and meaning of the for-statement
(cont.)
2. A LOOP-CONTINUATION-CONDITION: this is a
Boolean expression that determines whether the forstatement will be terminated
(This clause has the same function as in the LOOPCONTINUATION-CONDITION of an whilestatement)
3. A INCR-STATEMENT: this is a statement that is
executed at the end of the loop.
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
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 loopcontinuation-condition is true !!!
After the body has been executed, the INCRSTATEMENT is executed before repeating the
check of the LOOP-CONTINUATIONCONDITION
Common practice in for-statements
• Common practice in for-loops:
• Use a block as body of for-statements
Common practice in for-statements (cont.)
• 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 of a for-statement (cont.)
• Flow chart representing the for-statement:
Flow chart of a for-statement (cont.)
• This is the program flow when LOOP-CONTINUATIONCONDITION is true:
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 LOOPCONTINUATION-CONDITION
Flow chart of a for-statement (cont.)
• This is the program flow when LOOP-CONTINUATIONCONDITION is false:
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
Flow chart of a for-statement (cont.)
• Example:
for ( a = 1 ; a <= 10 ; a++ )
{
System.out.println(a);
}
System.out.println("Done");
Flow chart of a for-statement (cont.)
• Flow chart of this program:
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");
Flow chart of a for-statement (cont.)
• Execution:
• Program "loops" as long
as a ≤ 10
• Program exits loop when
a > 10
Structure diagram of a for-statement
• For-statement:
Structure diagram of a for-statement
(cont.)
• Structure diagram representing a while-statement:
Structure diagram of a for-statement
(cont.)
• How to "read" the diagram:
• The outer rectangle containing the LOOPCONTINUATION-CONDITION represents the forstatement:
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
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 forstatement's body)
• The INCR-statement