loops (part 1) computer science erwin high school fall 2014
TRANSCRIPT
How to Use the Slides
• As you go through the slides, answer the questions that appear on a separate piece of paper.
• You will turn this paper in when you complete the slideshow.
• If you have questions as you read, list these on your paper as well.
What is a Loop?
• A loop allows us to repeat a block of code.
• As long as the condition associated with the loop is true, the block of code will continue to be repeated.
The “while” Loop
• The “while” loop is a very simple loop.
• As long as the condition associated with the “while” loop is true, the block of code will be executed and the loop will continue to run.
“while” Loop Example
int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); }
“while” Loop Example
int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); }
An integer variable named count is declared and set equal to zero.
“while” Loop Example
int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); }
A while loop is created with the condition, count < 5, and the condition is checked.
“while” Loop Example
int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); }
As long as count remains less than 5, the loop will run.
“while” Loop Example
int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); }
Inside the loop, the value of count is increased by one, (from 0 to 1).
“while” Loop Example
int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); } The current value of
count is printed to the console.
Output: 1
“while” Loop Example
int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); } The brace indicates we have
reached the end of the block and the end of the 1st run or “iteration” through the loop.
Output: 1
“while” Loop Example
int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); }
The condition is checked again with the current value of count (which is 1).
Output: 1
“while” Loop Example
int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); }
Since count < 5 (1 < 5), the loop will run again.
Output: 1
“while” Loop Example
int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); }
Inside the loop, the value of count is increased by one, (from 1 to 2).
Output: 1
“while” Loop Example
int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); } The current value of
count is printed to the console.
Output: 12
“while” Loop Example
int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); } The brace indicates we have
reached the end of the block and the end of the 2nd iteration of the loop.
Output: 12
“while” Loop Example
int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); }
The condition is checked again with the current value of count (which is 2).
Output: 12
“while” Loop Example
int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); }
The process continues and our output continues to update each time the loop is run (until the value of count is 5).
Output: 12345
“while” Loop Example
int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); }
After the 5th iteration, count = 5, which means the condition is not true (5 < 5 is false).
Output: 12345
“while” Loop Example
int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); }
The loop is complete and the program will continue to any code given after the block statement (after the second brace).
Output: 12345
Problem 1
• What is the output of the loop? (Pay attention to the small change.)
int count = 0; while ( count < 5 ) { System.out.println(count); count = count + 1; }
Problem 2
• What is the OUTPUT of the loop and the FINAL VALUE of run?
int run = 7; while ( run < 10 ) { System.out.println(run); run++; }
Problem 4
• What is the output?
int total = 0, x = 1; while ( x < 6 ) { total = total + x; x++; } System.out.println(total);
Problem 5
• What is the problem with the following loop?
int count = 0; while ( count < 5 ) { System.out.println(count); }
Problem 6
• What is the problem with the following loop?
while ( run < 5 ) { int run = 0; System.out.println(run); run++; }
Problem 7
• What happens as a result of the following code segment?
int run = 10; while ( run < 5 ) { System.out.println(run); run++; }
Problem 8• AP Level Question: Consider the following method.
public String mystery(String input) { String output = “”; int k = 1; while (k < input.length()) { output += input.substring(k, k + 1); k++; } return output; }
• What is returned as a result of the call mystery(“computer”)? ** Hint: Draw a table to keep track of the values of k and ouput. **
Problem 9
• AP Level Question: Consider the following code segment.
int sum = 0; int k = 1;
while (sum < 12 || k < 4) { sum += k; }
System.out.println(sum);
• What is printed as a result of executing the code segment?
The “do-while” Loop
• The “do-while” loop is very similar to the “while” loop, however there is one main difference.
• The block statement is executed once before the condition is ever checked; therefore, the loop is guaranteed to run at least once.
• If the condition is true after the first iteration, the loop is executed again and the condition is checked once more.
• This continues until the condition is not met and the loop is exited.
“do-while” Example
int count = 0; do { count++; System.out.println(count); } while ( count < 4 );
An integer variable named count is declared and set equal to zero.
“do-while” Example
int count = 0; do { count++; System.out.println(count); } while ( count < 4 );
The do-while loop is entered.
“do-while” Example
int count = 0; do { count++; System.out.println(count); } while ( count < 4 );
count is incremented (from 0 to 1).
“do-while” Example
int count = 0; do { count++; System.out.println(count); } while ( count < 4 );
The value of count is printed to the console.
Output: 1
“do-while” Example
int count = 0; do { count++; System.out.println(count); } while ( count < 4 );
The 1st iteration is complete and the condition is checked. Since 1 < 4, the loop will run again.
Output: 1
“do-while” Example
int count = 0; do { count++; System.out.println(count); } while ( count < 4 );
The loop continues to run until the condition is false (i.e. count < 4 is false). This first occurs when count = 4 at the end of the loop, but it’s value would have already been printed out.
Output: 1234
Problem 10
• What is the output of the loop? int run = 4; do { System.out.println(run); run--; } while (run > 0);
Problem 11
• What is the output of the loop? int x = 25; do { System.out.println(x); x -= 5; } while (x >= 10);
Problem 12
• What is the output of the loop? int count = 0; do { System.out.println(count + 1); count = count + 2; } while (count < 10);