Download presentation
Presentation is loading. Please wait.
Published byShawn Hall Modified over 9 years ago
1
The while-statement
2
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
3
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
4
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
5
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
6
Syntax and meaning of the while-statement Syntax of the while-statement:
7
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
8
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 !!!
9
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
10
Common practice in loop-statements (cont.) A typical while-loop looks like this:
11
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); }
12
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
13
Example while-statement: print the numbers 1 through 10 (cont.) Then executes the assignment statement: Stores the value 1 into the variable a
14
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
15
Example while-statement: print the numbers 1 through 10 (cont.) Execution of the while-statement's body: System.out.println(a);
16
Example while-statement: print the numbers 1 through 10 (cont.) a++;
17
Example while-statement: print the numbers 1 through 10 (cont.) When the execution of the while-body is completed:
18
Example while-statement: print the numbers 1 through 10 (cont.) the while-statement resumes from the start again !!!
19
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
20
Example while-statement: print the numbers 1 through 10 (cont.) Execution of the while-statement's body: System.out.println(a);
21
Example while-statement: print the numbers 1 through 10 (cont.) a++;
22
Example while-statement: print the numbers 1 through 10 (cont.) When the execution of the while-body is completed:
23
Example while-statement: print the numbers 1 through 10 (cont.) the while-statement resumes from the start again !!!
24
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 !
25
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).
26
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
27
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
28
Flow chart of a while-statement While-statement:
29
Flow chart of a while-statement (cont.) Flow chart representing a while-statement:
30
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
31
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
32
Flow chart of a while-statement (cont.) Example: a = 1; while ( a <= 10 ) { System.out.println(a); a++; } System.out.println("Done");
33
Flow chart of a while-statement (cont.) Flow chart of this program:
34
Flow chart of a while-statement (cont.) Execution: Program "loops" as long as a ≤ 10 Program exits loop when a > 10
35
Structure diagram of a while-statement While-statement:
36
Structure diagram of a while-statement (cont.) Structure diagram representing a while-statement:
37
Structure diagram of a while-statement (cont.) How to "read" the diagram: The outer rectangle containing the LOOP- CONTINUATION-CONDITION represents the while- statement:
38
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
39
Structure diagram of a while-statement (cont.) Example: a = 1; while ( a <= 10 ) { System.out.println(a); a++; } System.out.println("Done");
40
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)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.