Presentation is loading. Please wait.

Presentation is loading. Please wait.

* What kind of loop would I use to complete the following: A. Output all of the prime numbers that are less than 100,000 B. Output the Fibonacci sequence.

Similar presentations


Presentation on theme: "* What kind of loop would I use to complete the following: A. Output all of the prime numbers that are less than 100,000 B. Output the Fibonacci sequence."— Presentation transcript:

1 * What kind of loop would I use to complete the following: A. Output all of the prime numbers that are less than 100,000 B. Output the Fibonacci sequence until the numbers are greater than 1000 C. Output the first 100 multiples of 13

2 Pre-AP Computer Science, Cycle 6

3 * 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 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 * “Repeat this ‘x’ times” * “Cycle through ‘x’ things” for (int i=0; i<100; i++) { int y=i*2; }

6 * 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 int x=0; int y; while (x < 100) { y = x*2; x++; } for (int i=0; i<100; i++) { int y=i/3; }

8 * 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: 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 while (answer != “no”) { System.out.print(x); x++; System.out.print(“Keep looping?”); answer = console.next(); }

11 * 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 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 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 * 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. A. Counter-Controlled B. Sentinel-Controlled C. Flag-Controlled

15 * 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 * 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 Pre-AP Computer Science, Cycle 6

18 * 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 * 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 this while this is true” do { //statements } while (condition);

21 while (x != 0) { //statements; } do { //statements; } while (x !=0);

22 * 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 * Print random numbers between 1-100 until the number 42 is encountered do { int num = Math.random()*100 + 1; System.out.println(num); } while (num != 42);

24 * Convert the following while loop into a do-while loop while (number != 15) { System.out.println(number); number = Math.random()*20+1; }

25 * Which loop would be best for the following prompts? FOR loop, WHILE loop, or DO-WHILE loop A. A program that displays all the numbers from 100 to 1000 that are divisible by 5 and 6 B. A program that allows the user to type in test scores until they type “STOP”

26 Pre-AP Computer Science, Cycle 6

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

28 int sum = 0; int number = 0; while (number < 20) { number++; sum = sum + number; if (sum >= 100) { break; }

29 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 * 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 int number = 0; while (number < 5) { if (number == 3) { continue; } System.out.print(number + “ “); } Output: 0 1 2 4 5

32 * 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


Download ppt "* What kind of loop would I use to complete the following: A. Output all of the prime numbers that are less than 100,000 B. Output the Fibonacci sequence."

Similar presentations


Ads by Google