while Statement
Objectives: Learn the Java syntax for while loops Learn about the Java increment and decrement operators.
Iterations It is essential that a program be able to execute the same set of instructions many times: otherwise a program would do only as much work as a programmer! Repeating the same code fragment several times is called iterating. Java provides three control statements for iterations (a.k.a. loops): for, while, and do-while. Iterations are what makes a program different from a cookbook recipe.
The while Loop condition is any logical expression, as in if while ( condition ) { statement1; statement2; ... statementN; } The body of the loop Many people prefer to always use braces, as it is easier to add statements to the body of the loop. If the body has only one statement, the braces are optional while ( condition ) statement1;
The while Loop (cont’d) Example: // Prints “Hello World” ten times int p = 0; while ( p < 10 ) { System.out.print(“Hello World”); p++; } Initialization Test Increment When we say “increment” we really mean any change in the variables tested in the condition.
The while Loop (cont’d) Initialization: the variables tested in the condition must be initialized to some values. If the condition is false at the outset, the loop is never entered. Test: the condition is tested before each iteration. If false, the program continues with the first statement after the loop. Increment (change): At least one of the variables tested in the condition must change in the body of the loop.
Common while loop Errors { statements; ... } Extra semicolon It is safer to always use braces in while loops while (...) statement1; statement2; ... Missing braces
Increment and Decrement Operators Symbol Purpose Example Result Increment ++ increment by 1 int n = 5; out.print(n++); 5 n == 6 out.print(++n); 6 Decrement -- decrement by 1 out.print(n--); n == 4 out.print(--n); 4
Top Down Design
Top-Down Design (cont’d) Start JCreator. Open the file “Lab07.java”. Lab08.java is in your Lab07 folder.
Top-Down Design (cont’d) Write a program that reads two integers from the keyboard and outputs all the ASCII values from the first number up to the second number.
Top-Down Design (cont’d) import java.util.Scanner; public class Lab07 { public static void main(String[ ] args) { Lab08 lab = new Lab08( ); lab.input(); // Enter data from the kybd lab.output( ); // Display output }
Top-Down Design (cont’d) input(), process() and output( ) are the smaller parts of the problem.
Top-Down Design (cont’d) What fields are required to solve the problem? NOTE: Fields should be the values we will read from the keyboard. int start; int end;
Top-Down Design (cont’d) import java.util.Scanner; public class Lab07 { private int start; private int end; public static void main(String[ ] args) Lab07 lab = new Lab07( ); lab.input(); // Enter data from the kybd lab.output( ); // Display output }
Top-Down Design (cont’d) input Prompt the user to enter a sentence then read the sentence from the keyboard. Enter the starting value: 65 Enter the ending value: 70
Top-Down Design (cont’d) public void input() { Scanner reader = new Scanner(System.in); System.out.print("Enter the starting value: "); start = reader.nextInt(); System.out.print("Enter the ending value: "); end = reader.nextInt(); }
Top-Down Design (cont’d) output We want to display all the ASCII values from start to end. 65 = A 66 = B 67 = C 68 = D 69 = E 70 = F Do you see the loop? Something that is progressively growing or shrinking.
Top-Down Design (cont’d) public void output() { }
Top-Down Design (cont’d) public void output() { int ascii = start; }
Top-Down Design (cont’d) public void output() { int ascii = start; while ( ) { }
Top-Down Design (cont’d) public void output() { int ascii = start; while (ascii <= end) { }
Top-Down Design (cont’d) public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); }
Top-Down Design (cont’d) public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; }
Top-Down Design (cont’d) Run The Program: Enter the starting value: Enter the ending value: 255
Top-Down Design (cont’d)
Top-Down Design (cont’d)
Top-Down Design (cont’d)
Top-Down Design (cont’d)
Top-Down Design (cont’d)
How does a while loop work?
Top-Down Design (cont’d) public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; } start = 65 end = 68
Top-Down Design (cont’d) ascii end acsii <= end output 65 68 public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; } start = 65 end = 68
Top-Down Design (cont’d) ascii end acsii <= end output 65 68 true public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; }
Top-Down Design (cont’d) ascii end acsii <= end output 65 68 true 65 = A public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; }
Top-Down Design (cont’d) ascii end acsii <= end output 65 68 true 65 = A 66 public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; }
Top-Down Design (cont’d) ascii end acsii <= end output 65 68 true 65 = A 66 public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; }
Top-Down Design (cont’d) ascii end acsii <= end output 65 68 true 65 = A 66 66 = B public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; }
Top-Down Design (cont’d) ascii end acsii <= end output 65 68 true 65 = A 66 66 = B 67 public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; }
Top-Down Design (cont’d) ascii end acsii <= end output 65 68 true 65 = A 66 66 = B 67 public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; }
Top-Down Design (cont’d) ascii end acsii <= end output 65 68 true 65 = A 66 66 = B 67 67 = C public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; }
Top-Down Design (cont’d) ascii end acsii <= end output 65 68 true 65 = A 66 66 = B 67 67 = C public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; }
Top-Down Design (cont’d) ascii end acsii <= end output 65 68 true 65 = A 66 66 = B 67 67 = C public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; }
Top-Down Design (cont’d) ascii end acsii <= start output 65 68 true 65 = A 66 66 = B 67 67 = C 68 = D public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; }
Top-Down Design (cont’d) ascii end acsii <= end output 65 68 true 65 = A 66 66 = B 67 67 = C 68 = D 69 public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; }
Top-Down Design (cont’d) ascii end acsii <= end output 65 68 true 65 = A 66 66 = B 67 67 = C 68 = D 69 false public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; }
Top-Down Design (cont’d) ascii end acsii <= end output 65 68 true 65 = A 66 66 = B 67 67 = C 68 = D 69 false public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; } OUTPUT 65 = A 66 = B 67 = C 68 = D
Questions?
Begin Lab 07