Presentation is loading. Please wait.

Presentation is loading. Please wait.

While Statement.

Similar presentations


Presentation on theme: "While Statement."— Presentation transcript:

1 while Statement

2 Objectives: Learn the Java syntax for while loops
Learn about the Java increment and decrement operators.

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

4 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;

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

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

7 Common while loop Errors
{ statements; ... } Extra semicolon It is safer to always use braces in while loops while (...) statement1; statement2; ... Missing braces

8 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

9 Top Down Design

10 Top-Down Design (cont’d)
Start JCreator. Open the file “Lab07.java”. Lab08.java is in your Lab07 folder.

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

12 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 }

13 Top-Down Design (cont’d)
input(), process() and output( ) are the smaller parts of the problem.

14 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;

15 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 }

16 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

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

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

19 Top-Down Design (cont’d)
public void output() { }

20 Top-Down Design (cont’d)
public void output() { int ascii = start; }

21 Top-Down Design (cont’d)
public void output() { int ascii = start; while ( ) { }

22 Top-Down Design (cont’d)
public void output() { int ascii = start; while (ascii <= end) { }

23 Top-Down Design (cont’d)
public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); }

24 Top-Down Design (cont’d)
public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; }

25 Top-Down Design (cont’d)
Run The Program: Enter the starting value: Enter the ending value: 255

26 Top-Down Design (cont’d)

27 Top-Down Design (cont’d)

28 Top-Down Design (cont’d)

29 Top-Down Design (cont’d)

30 Top-Down Design (cont’d)

31 How does a while loop work?

32 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

33 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

34 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++; }

35 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++; }

36 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++; }

37 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++; }

38 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++; }

39 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++; }

40 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++; }

41 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++; }

42 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++; }

43 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++; }

44 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++; }

45 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++; }

46 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++; }

47 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

48 Questions?

49 Begin Lab 07


Download ppt "While Statement."

Similar presentations


Ads by Google