Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lec 4: while loop and do-while loop

Similar presentations


Presentation on theme: "Lec 4: while loop and do-while loop"— Presentation transcript:

1 Lec 4: while loop and do-while loop

2 Review from last week if Statements if (num < 0.5){
...println("heads"); } if –else Statements if (num < 0.5){ ...println("heads"); } else { ...println("tails");

3 Review from last week if –else if Statements if (n < 0.4){
...println("heads"); } else if (n <0.8){ ...println("tails"); else if (n < 0.85){ ...println("edge"); else { ... println("fell off table");

4 Review from last week How to store text data in a variable?
make String variable String message = new String("Hello world"); System.out.println( message ); How to read input from keyboard?

5 Use a Scanner object! Scanner input = new Scanner(System.in);
like a vending machine w/ three buttons next() nextInt() nextDouble() input

6 Scanner options Create a Scanner object and connect to keyboard
Scanner input = new Scanner(System.in); // make it only make one Scanner object in your program near top of program, before you read anything a Scanner reads Strings using the next method String word = input.next(); // read a String a Scanner can also read int or double: int age = input.nextInt(); // read an int double value = input.nextDouble(); // read a double

7 while loops A while loop is a way of repeating a set of instructions as long as some condition is met In general, a while loop looks like this: while(condition) {         statements } The condition must be a boolean expression There can be any number of statements inside the loop Sort of like ``an if statement that keeps repeating until the condition becomes false''

8 A while loop example int i = 0; while(i < 5) { System.out.println(i); i = i + 1; } System.out.println(); System.out.println("i=" + i); i (i<7) Console

9 Flowchart of previous slide

10 Processing a while loop
Java does the following when it meets a while loop: 1) Check to see if the condition is true 2) If the condition was false, bail out of the loop, otherwise go to 3) 3) Execute the body of the while loop and go back to 1)

11 Another while loop example
int i = 4; while(i > 0){ System.out.println(i); i = i - 1; } i (i>0) Console

12 Some common loop errors
False from the get-go int i = 0; while(i > 7) {     System.out.println(i);     i = i + 1; } nothing is printed!

13 Never terminates int i = 0; while(i < 7) { System.out.println(i); i = i - 1; } This is an "infinite loop" In BlueJ, R-click red run indicator, reset JVM

14 Another no-no loop int i = 0; while(i < 7) ; { System.out.println(i); i = i + 1; } This is an "infinite loop" In BlueJ, R-click red run indicator, reset JVM

15 What will this loop output?
int i = 10; while(i < 30) {     System.out.println(i);     i = i + 5; } i (i<30) Console

16 Another shortcut Programmers are very lazy
Instead of i = i + 1; we say i++; instead of i = i – 1; we say i --; instead of i = i + 5; we say i +=5; You can experiment with these in Lab 4 if you like

17 do loop A do loop is very much like a while loop, except that the condition is tested at the end of the loop instead of the beginning Looks like this: do {         statements } while(condition); The body of the loop is always executed the first time through, since the condition isn't checked until the end of the loop

18 How many times does the loop cycle?
int i = 0; while(i > 0) { i = i - 1; } System.out.println(i); // prints 0

19 Very Similar, only do-while
i=0; do { i = i - 1; }while(i > 0); System.out.println(i); // prints -1 do-while always executes at least once

20 Example: User Input loop
// Ask for age until a valid number is entered Scanner input = new Scanner(System.in); int age; do { System.out.println("What's your age?"); age = input.nextInt(); }while (age < 0 ); //loop back as long as input is bad System.out.println("Oh yeah? Well I'm " + (age+1) +"!"); }

21 Flowchart of previous slide

22 Lab 4 Dr. Kow's Hamburger Palace
Use loops to draw text graphic (while) read input and validate (do-while) use a Scanner to read input lettuce = input.nextInt();


Download ppt "Lec 4: while loop and do-while loop"

Similar presentations


Ads by Google