Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.

Similar presentations


Presentation on theme: "Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission."— Presentation transcript:

1 Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission

2 Loop A block of code that repeats until some condition is met which stops the repetition If the condition is never meet then the loop repeats indefinitely (infinite loop) Syntax: while ( Boolean expression ) statement to repeat; If expression == true, then the loop executes If expression == false, then the loop ends loop body If you want to repeat more than one statement, then put brackets around the loop body, and then you can repeat as many statements as you want.

3 Beware of Empty Loop Body
as long as ( x > 3 ) then do nothing (and so the loop never stops, since x never changes) if ( x > 3 ) then do nothing if (x > 3) ; System.out.println("Hello"); while (x > 3) ; x = 0; ALWAYS printed, no matter the value of x (should not be indented) Do not add a semicolons at the end of IF statements or WHILE statements

4 Simple Loop Example Goal: print "Hello" to the screen 3 times
System.out.println("Hello"); this line is repeated 3 times Solution: Set up a loop which will repeat the println statement 3 times while ( loop has not repeated 3 times ) System.out.println("Hello");

5 Simple Loop Example Goal: print "Hello" to the screen 3 times
How do you know if the loop has repeated 3 times? while ( loop has not repeated 3 times ) System.out.println("Hello"); Solution: Use a variable to count each time you have been through the loop Stop when the count becomes 3 int count; count = 0; count = 1; count = 2; count = 3; (continue while the count is below 3) 3 times

6 Simple Loop Example If you are using a loop, you must write your code in a way that it can be repeated int count; count = 0; count = 1; count = 2; count = 3; int count; count = 0; count = count + 1; There's nothing here that repeats! This repeats and achieves the same result!

7 Simple Loop Example Goal: print "Hello" to the screen 3 times
while ( loop has not repeated 3 times ) System.out.println("Hello"); count = count + 1; int count; count = 0; count = count + 1; brackets are needed, since there are two lines which must be repeated this line is repeated 3 times

8 Simple Loop Example Goal: print "Hello" to the screen 3 times
while ( loop has not repeated 3 times ) { System.out.println("Hello"); count = count + 1; } int count; count = 0; this is NOT repeated must be added above the loop because 1) it is not repeated 2) count must be declared and initialized before the loop begins

9 Simple Loop Example Goal: print "Hello" to the screen 3 times
int count; count = 0; count = 1; count = 2; count = 3; 3 times int count; count = 0; while ( loop has not repeated 3 times ) { System.out.println("Hello"); count = count + 1; } int count; count = 0; while ( count < 3 ) { System.out.println("Hello"); count = count + 1; } count < 3 loop condition must be written in terms of what it takes to keep the loop going

10 Simple Loop Example Goal: print "Hello" to the screen 3 times RAM
OUTPUT int count; count = 0; while ( count < 3 ) { System.out.println("Hello"); count = count + 1; } // program end count Hello Hello < 3 Hello 1 < 3 2 < 3 3 < 3

11 Sum Example: Non-loop Solution
Goal: write a program that will get 4 integers from the user and calculate their sum int num1, num2, num3, num4, sum; System.out.println("Enter 4 integers:"); num1 = kbd.nextInt(); num2 = kbd.nextInt(); num3 = kbd.nextInt(); num4 = kbd.nextInt(); sum = num1 + num2 + num3 + num4; System.out.println("The sum is:" + sum);

12 Sum Example: Loop Solution
Goal: write a program that will get 4 integers from the user and calculate their sum Notes: No need to store all 4 integers Only need to store the running total (the sum) Only need to hold an inputted number long enough to add it to the total (then it's never used again) We need to count how many items are entered so we know when we've processed 4 numbers

13 Sum Example: Loop Solution
USER ENTRY RAM holds user input running total tracks number of items num sum count clear + 2 2 1 Loop get user input add to sum increase count 2 + + 3 5 2 3 + + 3 7 + 7 12 + 6 18 4 6 =

14 Step 1: Step 2: Step 3: Step 4: Step 5: Step 6: Step 7:
Hit the clear button Step 2: Check to see if loop should execute; if not, go to step 7 (while loops always do this at the TOP of the loop) Step 3: Get input from the user Step 4: Add input to the running total Step 5: Increase the count Step 6: Go back to step 2 Step 7: Print running total to the screen loop

15 int sum, num, count; sum = 0; count = 0; while (count < 4) { System.out.print("Enter an integer: "); num = kbd.nextInt(); sum = sum + num; count = count + 1; } System.out.println("The sum is: " + sum);

16 Test the Program RAM OUTPUT sum num count 2 2 3 1 5 7 2 12 6 3 18 4
int sum, num, count; sum = 0; count = 0; while (count < 4) { System.out.print("Enter an integer: "); num = kbd.nextInt(); sum = sum + num; count = count + 1; } System.out.println("The sum is: " + sum); sum num count Enter an integer: 2 Enter an integer: 3 2 < 4 Enter an integer: 7 2 3 1 Enter an integer: 6 < 4 The sum is: 18 5 7 2 < 4 12 6 3 < 4 18 4 < 4


Download ppt "Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission."

Similar presentations


Ads by Google