* what kind of loop would i use to complete the following: a. output all of the prime numbers that...

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

Upload: jack-grant

Post on 28-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

*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

Pre-AP Computer Science, Cycle 6

*Advanced Loops

*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

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

}

*FOR Loops

* “Repeat this ‘x’ times”

* “Cycle through ‘x’ things”

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

{

int y=i*2;

}

*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

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

}

*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

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

*Sentinel-Controlled Example

while (answer != “no”)

{

System.out.print(x);

x++;

System.out.print(“Keep looping?”);

answer = console.next();

}

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

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

}

}

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

}

}

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

B. Sentinel-Controlled

C. Flag-Controlled

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

* 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

Pre-AP Computer Science, Cycle 6

*Do-While Loops

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

* 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

* Do-While Loops - Structure

* “Do this while this is true”

do {

//statements

} while (condition);

* Converting WHILE loops

while (x != 0)

{

//statements;

}

do {

//statements;

} while (x !=0);

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

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

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

}

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

Pre-AP Computer Science, Cycle 6

* Break and Continue Statements

* Break Statement

* Can be used to immediately terminate a loop early

* Can be used in any kind of loop

* Use by merely writing break;

* Break Statement - Example

int sum = 0;

int number = 0;

while (number < 20) {

number++;

sum = sum + number;

if (sum >= 100) {

break;

}

}

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

else

System.out.println(“Too low!”);

}

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

* Continue Statement - Example

int number = 0;

while (number < 5) {

if (number == 3) {

continue;

}

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

}

Output: 0 1 2 4 5

* 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