330 likes | 422 Views
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.
E N D
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 • 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; }
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.
Simulation sum : 0 number : 1 int sum = 0; int number = 1; while(number<=3){ sum = sum + number; number = number +1; } 1<=3?True!
Simulation sum : 0 1 number : 1 2 int sum = 0; int number = 1; while(number<=3){ sum = sum + number; number = number +1; }
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!
Simulation sum : 0 1 3 number : 1 2 3 int sum = 0; int number = 1; while(number<=3){ sum = sum + number; number = number +1; }
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!
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; }
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!
Practice int x = 1; int y = 3; while(x<y){if (x>0){ y = y+1;}x = x+2; }
Practice – trick question! int x = 1; int y = 3; while(x<y){if (x>0){ y = y+1;}x = x-2; }
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;
Problem Solving: Counter int counter = 1; while (counter <= 5){ System.out.println(counter); counter ++; }
Problem Solving: Running total int counter = 1; int total = 0; while (counter <= 5){ total = total + counter; counter++; }
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.
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?
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
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
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
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
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
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?
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;}
For-while comparison • Calculate 5! • int prod = 1;int num = 1;while(num <= 5){ prod = prod*num; num++;}
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;}
Do-While Loop do{ACTIONS; } while(BOOLEAN); Difference is when the Boolean condition is checked – before or after the loop
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);
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?
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
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.