Presentation is loading. Please wait.

Presentation is loading. Please wait.

Loops. More Flow of Control  Sometimes we want to do something many times.  Don’t have to write all the steps out multiple times.  Use a LOOP – control.

Similar presentations


Presentation on theme: "Loops. More Flow of Control  Sometimes we want to do something many times.  Don’t have to write all the steps out multiple times.  Use a LOOP – control."— Presentation transcript:

1 Loops

2 More Flow of Control  Sometimes we want to do something many times.  Don’t have to write all the steps out multiple times.  Use a LOOP – control statement

3 While Loop while(BOOLEAN){ ACTION } Keep doing actions while the boolean evaluates to true.

4 Example  Calculate the sum of the first 100 integers.  I don’t want to write out 1+2+3+… int sum = 0; int number = 1; while(number<=100){ sum = sum + number; number = number +1; }

5 How it works  Boolean is the “Gatekeeper” – lets you into the block of code if the boolean is true.  Elevator always bring you back up to the gate keeper at the end of the block.

6 Simulation int sum = 0; int number = 1; while(number<=3){ sum = sum + number; number = number +1; } sum : 0 number : 1 1<=3? True!

7 Simulation int sum = 0; int number = 1; while(number<=3){ sum = sum + number; number = number +1; } sum : 0 1 number : 1 2

8 Simulation int sum = 0; int number = 1; while(number<=3){ sum = sum + number; number = number +1; } sum : 0 1 number : 1 2 2<=3? True!

9 Simulation int sum = 0; int number = 1; while(number<=3){ sum = sum + number; number = number +1; } sum : 0 1 3 number : 1 2 3

10 Simulation int sum = 0; int number = 1; while(number<=3){ sum = sum + number; number = number +1; } sum : 0 1 3 number : 1 2 3 3<=3? True!

11 Simulation int sum = 0; int number = 1; while(number<=3){ sum = sum + number; number = number +1; } sum : 0 1 3 6 number : 1 2 3 4

12 Simulation int sum = 0; int number = 1; while(number<=3){ sum = sum + number; number = number +1; } sum : 0 1 3 6 number : 1 2 3 4 4<=3? False!

13 Practice int x = 1; int y = 3; while(x 0){ y = y+1; } x = x+2; }

14 Practice – trick question! int x = 1; int y = 3; while(x 0){ y = y+1; } x = x-2; }

15 Nested Whiles int w = -1; int x = 0; int y = 2; int z = 3; while(x 0){ w = w*z; y = y-1; } x = x+1; z = z-1; } y = z/2;

16 Problem Solving: Counter int counter = 1; while (counter <= 5){ System.out.println(counter); System.out.println(counter); counter ++; counter ++;}

17 Problem Solving: Running total int counter = 1; int total = 0; while (counter <= 5){ total = total + counter; total = total + counter; counter++; counter++;}

18 Practice Session 1  Write a method that takes a String and a number and prints the string to the screen that many times.  Take an integer and compute how many times you can divide it by 2 before you get 1.

19 Problem Solving: Make a chart Timmy Turtle is crawling to a wall. He starts out crawling 2 inches every minute. But, he is getting tired. And every minute he crawls half as fast. How far has he crawled in four minutes?

20 Making a Chart  What you would do on paper: MinuteTotalCrawl Rate 00 in2 in 12 in1 in 23 in.5 in 33.5 in.25 in 43.75 in.125 in

21 Making a Chart MinuteTotalCrawl Rate 00 in2 in 12 in1 in 23 in.5 in 33.5 in.25 in 43.75 in.125 in The columns are your variables, and the first row is their initial value int minute = 0; double total = 0; double crawlRate = 2;

22 Making a Chart MinuteTotalCrawl Rate 00 in2 in 12 in1 in 23 in.5 in 33.5 in.25 in 43.75 in.125 in How do you know when to keep going? int minute = 0; double total = 0; double crawlRate = 2; while (minute < 4){

23 Making a Chart MinuteTotalCrawl Rate 00 in2 in 12 in1 in 23 in.5 in 33.5 in.25 in 43.75 in.125 in How do you get from one row to the next? int minute = 0; double total = 0; double crawlRate = 2; while (minute < 4){ minute++; total = total + crawlRate; crawlRate = crawlRate / 2;

24 Making a Chart MinuteTotalCrawl Rate 00 in2 in 12 in1 in 23 in.5 in 33.5 in.25 in 43.75 in.125 in What’s the answer? int minute = 0; double total = 0; double crawlRate = 2; while (minute < 4){ minute++; total = total + crawlRate; crawlRate = crawlRate / 2; } return total;

25 Practice Session 2  The turtle takes go-go juice! Each minute he crawls twice as fast as the last minute. Given his start speed, calculate how long it takes him to crawl 20 inches?

26 For loop  Does the same thing as the while loop, with different syntax.  Most useful for – do something X times.  for(INIT;BOOLEAN;UPDATE){ ACTION; }

27 For-while comparison  Calculate 5!  int prod = 1; int num = 1; while(num <= 5){ prod = prod*num; num++; }

28 For-while comparison  Calculate 5!  int prod = 1; int num = 1; while(num <= 5){ prod = prod*num; num++; } int prod = 1; for(int num = 1; num <= 5; num++){ prod = prod*num; }

29 Do-While Loop do{ ACTIONS; } while(BOOLEAN); Difference is when the Boolean condition is checked – before or after the loop

30 While comparisons  Calculate 5!  int prod = 1; int num = 1; while(num <= 5){ prod = prod*num; num++; } int prod = 1; int num =1; do{ prod = prod * num; num++; }while(num<=5);

31 Do-While statement  Always executes the body of the loop at least once!  Checks the boolean after the loop, not before  When would you want this?

32 Extras  One trip through a loop is called an iteration  Do some iterations by hand to get an idea of what’s going on  Put in print lines for yourself when debugging gets bad

33 Last Practice  Given a number, draw that many circles in a row to the screen.  Calculate the factorial of a number using a for loop.


Download ppt "Loops. More Flow of Control  Sometimes we want to do something many times.  Don’t have to write all the steps out multiple times.  Use a LOOP – control."

Similar presentations


Ads by Google