32
* Warm-Up: April 14 * 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

Warm-Up: April 14

  • Upload
    morrie

  • View
    22

  • Download
    0

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: Warm-Up: April 14

*Warm-Up: April 14

* 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

Page 2: Warm-Up: April 14

Pre-AP Computer Science, Cycle 6

*Advanced Loops

Page 3: Warm-Up: April 14

*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

Page 4: Warm-Up: April 14

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

}

Page 5: Warm-Up: April 14

*FOR Loops* “Repeat this ‘x’ times”* “Cycle through ‘x’ things”

for (int i=0; i<100; i++){

int y=i*2;}

Page 6: Warm-Up: April 14

*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

Page 7: Warm-Up: April 14

*Counter Controlled Loops Example

int x=0; int y;while (x <

100){

y = x*2;x++;

}

for (int i=0; i<100; i++)

{ int y=i/3;

}

Page 8: Warm-Up: April 14

*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

Page 9: Warm-Up: April 14

*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

Structurewhile (variable != sentinel)

Page 10: Warm-Up: April 14

*Sentinel-Controlled Example

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

Page 11: Warm-Up: April 14

*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

Structureboolean flag = false;while (!flag)

Page 12: Warm-Up: April 14

*Flag-Controlled Exampleboolean stop = false;

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

Page 13: Warm-Up: April 14

*Flag-Controlled Example Bboolean loop = true;

while (loop){System.out.println(“banana!”);System.out.println(“More bananas?”);String ans = console.next();if (ans.charAt(0) == ‘n’){loop = false;}}

Page 14: Warm-Up: April 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.

A. Counter-ControlledB. Sentinel-ControlledC. Flag-Controlled

Page 15: Warm-Up: April 14

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

Page 16: Warm-Up: April 14

* 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

Page 17: Warm-Up: April 14

Pre-AP Computer Science, Cycle 6

*Do-While Loops

Page 18: Warm-Up: April 14

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

Page 19: Warm-Up: April 14

* 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

Page 20: Warm-Up: April 14

* Do-While Loops - Structure

* “Do this while this is true”

do {//statements

} while (condition);

Page 21: Warm-Up: April 14

* Converting WHILE loops

while (x != 0){

//statements;}

do {//statements;

} while (x !=0);

Page 22: Warm-Up: April 14

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

Page 23: Warm-Up: April 14

* Do-While Loops – Example B

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

Page 24: Warm-Up: April 14

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

}

Page 25: Warm-Up: April 14

* Warm-up: April 17(1st period)

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

Page 26: Warm-Up: April 14

Pre-AP Computer Science, Cycle 6

* Break and Continue Statements

Page 27: Warm-Up: April 14

* Break Statement* Can be used to immediately terminate a loop early

* Can be used in any kind of loop

* Use by merely writing break;

Page 28: Warm-Up: April 14

* Break Statement - Example

int sum = 0;int number = 0;while (number < 20) {

number++;sum = sum + number;if (sum >= 100) {

break;}

}

Page 29: Warm-Up: April 14

* Break - Example 2int 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!”);elseSystem.out.println(“Too low!”);}

Page 30: Warm-Up: April 14

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

Page 31: Warm-Up: April 14

* Continue Statement - Example

int number = 0;while (number < 5) {

if (number == 3) {continue;

}System.out.print(number + “ “);

}

Output: 0 1 2 4 5

Page 32: Warm-Up: April 14

* 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