1 / 32

Warm-Up: April 14

Warm-Up: April 14. What kind of loop would I use to complete the following: Output all of the prime numbers that are less than 100,000 Output the Fibonacci sequence until the numbers are greater than 1000 Output the first 100 multiples of 13. Advanced Loops.

morrie
Download Presentation

Warm-Up: April 14

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Warm-Up: April 14 • What kind of loop would I use to complete the following: • Output all of the prime numbers that are less than 100,000 • Output the Fibonacci sequence until the numbers are greater than 1000 • Output the first 100 multiples of 13

  2. Advanced Loops Pre-AP Computer Science, Cycle 6

  3. Review • Loops are used for repeating tasks or sections of code • While loops are used when the number of times to loop is unknown, or may change • For loops are used when the number of times to loop is known, or can be represented by a variable

  4. WHILE Loops • “While this condition is true, do this” • “Do this until this is true / do this until this isn’t true” int x=0; int y; while (condition) while (x < 100) { { //statements y = x*2; } x++; }

  5. FOR Loops • “Repeat this ‘x’ times” • “Cycle through ‘x’ things” for (inti=0; i<100; i++) { int y=i*2; }

  6. Counter Controlled Loops • Counter controlled loops are characterized by the use of a counter, or variable, that tracks the number of times a loop has executed • Some while loops are counter controlled, but ALL for loops are counter controlled

  7. Counter Controlled Loops Example intx=0; int y; while (x < 100) { y = x*2; x++; } for (inti=0; i<100; i++) { int y=i/3; }

  8. Non-Counter Controlled Loops • Non-counter controlled loops do NOT use a counter to keep track of loop iterations • Number of iterations doesn’t matter • Non-counter controlled loops are ALWAYS while or do-while loops • Examples: sentinel-controlled; flag-controlled

  9. Sentinel-Controlled Loops • Sentinel: Special value that signal when the loop should stop running • Loop continues as long as the sentinel value is not encountered Structure while (variable != sentinel)

  10. Sentinel-Controlled Example while (answer != “no”) { System.out.print(x); x++; System.out.print(“Keep looping?”); answer = console.next(); }

  11. Flag-Controlled Loops • Uses a boolean variable to control the loop • The boolean variable is called a flag • Must eventually be set to true in loop body Structure boolean flag = false; while (!flag)

  12. Flag-Controlled Example boolean stop = false; while (!stop) { System.out.println(“banana!”); System.out.println(“More bananas?”); String ans = console.next(); if (ans == “no” || ans == “No”) { stop = true; } }

  13. Flag-Controlled Example B boolean loop = true; while (loop) { System.out.println(“banana!”); System.out.println(“More bananas?”); String ans = console.next(); if (ans.charAt(0) == ‘n’) { loop = false; } }

  14. Warm-Up: April 15 • For each of the following kinds of loops, state whether a FOR loop, WHILE loop, or both are capable of performing that kind of loop. • Counter-Controlled • Sentinel-Controlled • Flag-Controlled

  15. Warm-up: April 16(3rd period) • List at least 10 steps that the computer would have to complete for the following problem: A program continues to ask the user to input numbers until they input they number -1. Then, the computer outputs the minimum and maximum of the numbers typed in.

  16. Steps • Set up variables for num, min, and max • Begin a loop that runs while num != -1 • Ask the user for a number • Save their response in “num” • IF num != -1 • Compare num to the minimum • Replace minimum with the smallest number • Compare num to the maximum • Replace max with the largest number • Output minimum • Output maximum

  17. Do-While Loops Pre-AP Computer Science, Cycle 6

  18. Review • Counter Controlled • Loop ends when the counter expires • for (int counter=0; counter<100; counter++) • Sentinel-Controlled Loop • Loop ends when the sentinel value is encountered • while (answer != -1) • Flag-Controlled Loop • Loop ends when the flag (boolean) is reversed • while (!stop)

  19. Do-While Loops • Used for looping situations that MUST occur at least one time • Have the same expressive power as WHILE loops • Counter, flags, sentinels • Primary Difference: It checks the condition AFTER the loop statement

  20. Do-While Loops - Structure • “Do this while this is true” do { //statements } while (condition);

  21. Converting WHILE loops while (x != 0) { //statements; } do { //statements; } while (x !=0);

  22. Do-While Loops - Example • Add up all user-inputted numbers until 0 is encountered do { System.out.print(“Enter a value: “); data = input.nextInt(); sum = sum + data; } while (data != 0); System.out.println(“Sum: “ + sum);

  23. Do-While Loops – Example B • Print random numbers between 1-100 until the number 42 is encountered do { intnum = Math.random()*100 + 1; System.out.println(num); } while (num != 42);

  24. Warm-up: April 17(3st period) • Convert the following while loop into a do-while loop while (number != 15) { System.out.println(number); number = Math.random()*20+1; }

  25. Warm-up: April 17(1st period) • Which loop would be best for the following prompts? FOR loop, WHILE loop, or DO-WHILE loop • A program that displays all the numbers from 100 to 1000 that are divisible by 5 and 6 • A program that allows the user to type in test scores until they type “STOP”

  26. Break and Continue Statements Pre-AP Computer Science, Cycle 6

  27. Break Statement • Can be used to immediately terminate a loop early • Can be used in any kind of loop • Use by merely writing break;

  28. Break Statement - Example int sum = 0; int number = 0; while (number < 20){ number++; sum = sum + number; if (sum >= 100) { break; } }

  29. Break - Example 2 int number = (int)(Math.random()*100); while (true) { System.out.println(“Guess my number!); int guess = console.nextInt(); if (guess == number) { System.out.println(“Correct!”) break; } else if (guess > number) System.out.println(“Too high!”); else System.out.println(“Too low!”); }

  30. Continue Keyword • Can be used to immediately break out of the current iteration of a loop, and skip to the next iteration • Can be used to skip certain situations without having to use complex logic for your condition (&& and ||)

  31. Continue Statement - Example int number = 0; while (number < 5) { if (number == 3) { continue; } System.out.print(number + “ “); } Output: 0 1 2 4 5

  32. Using Break and Continue • Typically, use of break and continue is discouraged as they can create loops with too many exit points that are difficult to debug • Break and continue should only be used if they simplify the loop, and make it easier to read

More Related