The while-statement.ppt

Download Report

Transcript The while-statement.ppt

The while-statement
The loop statements in Java
• What is a loop-statement:
• A loop-statement is a statement that repeatedly
executes statements contained in its body as long as a
loop conditional is satisfied.
• I.e.: the loop-statement is executed until the loop
condition is not valid
The loop statements in Java (cont.)
• Purpose of loop-statements:
• A loop-statement can accomplish a complex task
incrementally using smaller steps
• The body of a loop-statement achieves only a small
step towards completing a complex task
• By executing the body repeatedly, the algorithm can
perform the task incrementally using smaller (simpler)
steps
Different loop-statements in programming
languages
• Most programming languages provides 3 types of loopstatements:
1. The while-statement
2. The for-statement
3. The do-while-statement
Different loop-statements in programming
languages (cont.)
• The main loop-statement is the while-statement
• The for-statement and the do-while-statement can be rewritten as a while-statement (but the result can be very
verbose)
• We will first study the while-statement
Syntax and meaning of the while-statement
• Syntax of the while-statement:
Syntax and meaning of the while-statement
(cont.)
• Explanation:
• The keyword while announces (to the Java compiler)
that we started an while-statement
• A conditional clause ( LOOP-CONTINUATIONCONDITION ) follows the keyword while
• The LOOP-CONTINUATION-CONDITION is a
Boolean expression (exactly the same as in the
condition clause of an if-statement)
• This is the condition of the while-statement
Syntax and meaning of the while-statement
(cont.)
• Following the loop-continuation-condition clause, you can
write (only) one statement
• This is the body of the while-statement
• The body will be executed as long as the loopcontinuation-condition is true !!!
Common practice in loop-statements
• Fact:
• The body of any loop-statement will almost always
contains multiple statements
• Common practice in while-loops:
• Use a block as body of loop-statements
Common practice in loop-statements
(cont.)
• A typical while-loop looks like this:
Example while-statement: print the
numbers 1 through 10
• Consider the following Java program:
public class While01
{
public static void main(String[] args)
{
int a;
a = 1;
while ( a <= 10 )
// While-statement
{
System.out.println(a); // Print a
a++;
// Increment a
}
System.out.println("Done");
System.out.println("Exit: a = " + a);
}
}
Example while-statement: print the
numbers 1 through 10 (cont.)
• Execution of this Java program:
• Program begins:
The program reserve memory space for the variable a
Example while-statement: print the
numbers 1 through 10 (cont.)
• Then executes the assignment statement:
Stores the value 1 into the variable a
Example while-statement: print the
numbers 1 through 10 (cont.)
• Then executes the while-statement.
First, the loop-continuation-condition is tested:
The result is true and the while-body is executed
Example while-statement: print the
numbers 1 through 10 (cont.)
• Execution of the while-statement's body:
• System.out.println(a);
Example while-statement: print the
numbers 1 through 10 (cont.)
• a++;
Example while-statement: print the
numbers 1 through 10 (cont.)
• When the execution of the while-body is completed:
Example while-statement: print the
numbers 1 through 10 (cont.)
the while-statement resumes from the start again !!!
Example while-statement: print the
numbers 1 through 10 (cont.)
• The execution the while-statement continues.
First, the loop-continuation-condition is tested again:
The result is true and the while-body is executed
Example while-statement: print the
numbers 1 through 10 (cont.)
• Execution of the while-statement's body:
• System.out.println(a);
Example while-statement: print the
numbers 1 through 10 (cont.)
• a++;
Example while-statement: print the
numbers 1 through 10 (cont.)
• When the execution of the while-body is completed:
Example while-statement: print the
numbers 1 through 10 (cont.)
the while-statement resumes from the start again !!!
Example while-statement: print the
numbers 1 through 10 (cont.)
• And so on...
The while statement will continue executing until a = 11
because then the loop-continuation-condition a ≤ 10
becomes false (which will cause the while statement to
terminate).
This while-statement will loop 10 times !
Example while-statement: print the
numbers 1 through 10 (cont.)
Result:
The number 1, 2, ..., 10 will be printed (and the variable a
will have the value 11).
Example while-statement: print the
numbers 1 through 10 (cont.)
• Example Program: (Demo above code)
– Prog file:
http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/07/Progs/
While01.java
• How to run the program:
• Right click on link and save in a scratch directory
• To compile: javac While01.java
• To run:
java While01
Example while-statement: print the
numbers 1 through 10 (cont.)
• Output:
1
2
3
4
5
6
7
8
9
10
Done
Exit: a = 11
Flow chart of a while-statement
• While-statement:
Flow chart of a while-statement (cont.)
• Flow chart representing a while-statement:
Flow chart of a while-statement (cont.)
• How to "read" the flow chart:
• When the loop-cont-condition is true, the execution
takes the downward branch:
• It executes the statements which for the body
of the while-statement
• Then it goes back and retests the loop-contcondition
Flow chart of a while-statement (cont.)
• When the loop-cont-condition is false, the execution takes
the side branch
• In this case, the execution proceeds
(continues) with the statement following the
while-statement
I.e.,: the while-statement is completed
Flow chart of a while-statement (cont.)
• Example:
a = 1;
while ( a <= 10 )
{
System.out.println(a);
a++;
}
System.out.println("Done");
Flow chart of a while-statement (cont.)
• Flow chart of this program:
Flow chart of a while-statement (cont.)
• Execution:
Program "loops" as
long as a ≤ 10
Program exits loop
when a > 10
Structure diagram of a while-statement
• While-statement:
Structure diagram of a while-statement
(cont.)
• Structure diagram representing a while-statement:
Structure diagram of a while-statement
(cont.)
How to "read" the diagram:
• The outer rectangle containing the LOOPCONTINUATION-CONDITION represents the whilestatement:
Structure diagram of a while-statement
(cont.)
• The while-statement is one (single) statement)
• The while-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
The statements in the inner rectangle are executed when
the LOOP-CONTINUATION-CONDITION is true
Structure diagram of a while-statement
(cont.)
• Example:
a = 1;
while ( a <= 10 )
{
System.out.println(a);
a++;
}
System.out.println("Done");
Structure diagram of a while-statement
(cont.)
• Structure diagram of this program:
• The while-statement will keep
on executing as long as a ≤ 10
• As long as a ≤ 10, the whileloop executes these statements:
Print a;
a++; (increment a by 1)