Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lec 4: while loop and do-while loop. Review from last week if Statements if (num < 0.5){...println("heads"); } if –else Statements if (num < 0.5){...println("heads");

Similar presentations


Presentation on theme: "Lec 4: while loop and do-while loop. Review from last week if Statements if (num < 0.5){...println("heads"); } if –else Statements if (num < 0.5){...println("heads");"— 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 Strings – a class for representing sequence of letters Scanner – a class for reading input from keyboard

5 Scanner options a Scanner reads Strings by default – Scanner input = new Scanner(System.in); – String word = input.next(); a Scanner can also read int or double: – int age = input.nextInt(); – double value = input.nextDouble();

6 Scanner in Lab 3, 4 Lab Suggests the following steps to read a double: – Read a String String firstNum = input.next(); – Convert the String we just read to a (double) number: double num1 = Double.parseDouble( firstNum); It's much less confusing to just write: – double num1 = input.nextDouble(); // read double – int age = input.nextInt(); // read an int

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 < 7) { 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 = 7; 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 Lab 4 Dr. Kow's Hamburger Palace Use loops to draw text graphic (while) read input and validate (do-while) you may just use lettuce = input.nextInt(); instead of String s = input.nextLine(); lettuce = Integer.parseInt(s);


Download ppt "Lec 4: while loop and do-while loop. Review from last week if Statements if (num < 0.5){...println("heads"); } if –else Statements if (num < 0.5){...println("heads");"

Similar presentations


Ads by Google