 # * 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 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:

* 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

Pre-AP Computer Science, Cycle 6

* 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

* “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++; }

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

* 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

int x=0; int y; while (x < 100) { y = x*2; x++; } for (int i=0; i<100; i++) { int y=i/3; }

* 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

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

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

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

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

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

* 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

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

* 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

Pre-AP Computer Science, Cycle 6

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

* 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

* “Do this while this is true” do { //statements } while (condition);

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

* 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);

* 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);

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

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

Pre-AP Computer Science, Cycle 6

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

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

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!”); }

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

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

* 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