1 / 33

Loops

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 statement. While Loop. while(BOOLEAN){ ACTION } Keep doing actions while the boolean evaluates to true. Example.

Télécharger la présentation

Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 sum : 0 number : 1 int sum = 0; int number = 1; while(number<=3){ sum = sum + number; number = number +1; } 1<=3?True!

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

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

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

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

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

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

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

  14. Practice – trick question! int x = 1; int y = 3; while(x<y){if (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 < z){while(y >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); counter ++; }

  17. Problem Solving: Running total int counter = 1; int total = 0; while (counter <= 5){ total = total + 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: Minute Total Crawl Rate 0 0 in 2 in 1 2 in 1 in 2 3 in .5 in 3 3.5 in .25 in 4 3.75 in .125 in

  21. Making a Chart The columns are your variables, and the first row is their initial value int minute = 0; double total = 0; double crawlRate = 2; Minute Total Crawl Rate 0 0 in 2 in 1 2 in 1 in 2 3 in .5 in 3 3.5 in .25 in 4 3.75 in .125 in

  22. Making a Chart How do you know when to keep going? int minute = 0; double total = 0; double crawlRate = 2; while (minute < 4){ Minute Total Crawl Rate 0 0 in 2 in 1 2 in 1 in 2 3 in .5 in 3 3.5 in .25 in 4 3.75 in .125 in

  23. Making a Chart 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; Minute Total Crawl Rate 0 0 in 2 in 1 2 in 1 in 2 3 in .5 in 3 3.5 in .25 in 4 3.75 in .125 in

  24. Making a Chart 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; Minute Total Crawl Rate 0 0 in 2 in 1 2 in 1 in 2 3 in .5 in 3 3.5 in .25 in 4 3.75 in .125 in

  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.

More Related