Presentation is loading. Please wait.

Presentation is loading. Please wait.

Loops (Part 1) Computer Science Erwin High School Fall 2014.

Similar presentations


Presentation on theme: "Loops (Part 1) Computer Science Erwin High School Fall 2014."— Presentation transcript:

1 Loops (Part 1) Computer Science Erwin High School Fall 2014

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

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

4 Main Types of Loops A “while” loop A “do-while” loop A “for” loop

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

6 “while” Loop Layout while ( condition goes here ) { do something; do something else; }

7 “while” Loop Example int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); }

8 “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.

9 “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.

10 “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.

11 “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).

12 “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

13 “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

14 “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

15 “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

16 “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

17 “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 2

18 “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:1 2

19 “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:1 2

20 “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:1 2 3 4 5

21 “while” Loop Example int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); } After the 5 th iteration, count = 5, which means the condition is not true (5 < 5 is false). Output:1 2 3 4 5

22 “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:1 2 3 4 5

23 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; }

24 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++; }

25 Problem 3 What is the output? int x = 25; while ( x >= 10 ) { System.out.println(x); x = x - 5; }

26 Problem 4 What is the output? int total = 0, x = 1; while ( x < 6 ) { total = total + x; x++; } System.out.println(total);

27 Problem 5 What is the problem with the following loop? int count = 0; while ( count < 5 ) { System.out.println(count); }

28 Problem 6 What is the problem with the following loop? while ( run < 5 ) { int run = 0; System.out.println(run); run++; }

29 Problem 7 What happens as a result of the following code segment? int run = 10; while ( run < 5 ) { System.out.println(run); run++; }

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

31 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?

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

33 “do-while” Loop Layout do { do something; do something else; } while ( condition goes here );

34 “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.

35 “do-while” Example int count = 0; do { count++; System.out.println(count); } while ( count < 4 ); The do-while loop is entered.

36 “do-while” Example int count = 0; do { count++; System.out.println(count); } while ( count < 4 ); count is incremented (from 0 to 1).

37 “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

38 “do-while” Example int count = 0; do { count++; System.out.println(count); } while ( count < 4 ); The 1 st iteration is complete and the condition is checked. Since 1 < 4, the loop will run again. Output:1

39 “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:1 2 3 4

40 Problem 10 What is the output of the loop? int run = 4; do { System.out.println(run); run--; } while (run > 0);

41 Problem 11 What is the output of the loop? int x = 25; do { System.out.println(x); x -= 5; } while (x >= 10);

42 Problem 12 What is the output of the loop? int count = 0; do { System.out.println(count + 1); count = count + 2; } while (count < 10);

43 Problem 13 What are the errors in this loop? ** Hint: there are 2 of them ** int count = 0; do { System.out.println(count); } while (count < 5)


Download ppt "Loops (Part 1) Computer Science Erwin High School Fall 2014."

Similar presentations


Ads by Google