the while-statement. the loop statements in java what is a loop-statement: a loop-statement is a...

40
The while-statement

Upload: shawn-hall

Post on 29-Jan-2016

239 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

The while-statement

Page 2: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

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

Page 3: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

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

Page 4: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

Different loop-statements in programming languages

• Most programming languages provides 3 types of loop-statements:

1. The while-statement

2. The for-statement

3. The do-while-statement    

Page 5: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

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 re-written as a while-statement (but the result can be very verbose)

• We will first study the while-statement

Page 6: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

Syntax and meaning of the while-statement

• Syntax of the while-statement:

Page 7: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

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-CONTINUATION-CONDITION ) 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

Page 8: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

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 loop-continuation-condition is true !!!      

Page 9: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

Common practice in loop-statements

• Fact:

• Common practice in while-loops:

• The body of any loop-statement will almost always contains multiple statements

• Use a block as body of loop-statements

Page 10: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

Common practice in loop-statements (cont.)

• A typical while-loop looks like this:

Page 11: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

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); } }

Page 12: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

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

Page 13: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

Example while-statement: print the numbers 1 through 10 (cont.)

• Then executes the assignment statement:

Stores the value 1 into the variable a

Page 14: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

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

Page 15: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

Example while-statement: print the numbers 1 through 10 (cont.)

• Execution of the while-statement's body:

• System.out.println(a);

Page 16: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

Example while-statement: print the numbers 1 through 10 (cont.)

• a++;

Page 17: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

Example while-statement: print the numbers 1 through 10 (cont.)

• When the execution of the while-body is completed:

Page 18: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

Example while-statement: print the numbers 1 through 10 (cont.)

the while-statement resumes from the start again !!!

Page 19: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

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

Page 20: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

Example while-statement: print the numbers 1 through 10 (cont.)

• Execution of the while-statement's body:

• System.out.println(a);

Page 21: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

Example while-statement: print the numbers 1 through 10 (cont.)

• a++;

Page 22: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

Example while-statement: print the numbers 1 through 10 (cont.)

• When the execution of the while-body is completed:

Page 23: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

Example while-statement: print the numbers 1 through 10 (cont.)

the while-statement resumes from the start again !!!

Page 24: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

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 !

Page 25: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

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).

Page 26: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

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

Page 27: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

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

Page 28: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

Flow chart of a while-statement

• While-statement:

Page 29: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

Flow chart of a while-statement (cont.)

• Flow chart representing a while-statement:

Page 30: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

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-cont-condition

Page 31: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

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

Page 32: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

Flow chart of a while-statement (cont.)

• Example:

a = 1;

while ( a <= 10 ) { System.out.println(a); a++; }

System.out.println("Done");

Page 33: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

Flow chart of a while-statement (cont.)

• Flow chart of this program:

Page 34: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

Flow chart of a while-statement (cont.)

• Execution:

Program "loops" as long as a ≤ 10

Program exits loop when a > 10

Page 35: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

Structure diagram of a while-statement

• While-statement:

Page 36: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

Structure diagram of a while-statement (cont.)

• Structure diagram representing a while-statement:

Page 37: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

Structure diagram of a while-statement (cont.)

How to "read" the diagram:

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

Page 38: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

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

Page 39: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

Structure diagram of a while-statement (cont.)

• Example:

a = 1;

while ( a <= 10 ) { System.out.println(a); a++; }

System.out.println("Done");

Page 40: The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained

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 while-loop executes these statements:

Print a; a++; (increment a by 1)