Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introducing Loop Statements Liang, pages 82-86 Loop statements control repeated execution of a block of statements Each time the statements in the block.

Similar presentations


Presentation on theme: "Introducing Loop Statements Liang, pages 82-86 Loop statements control repeated execution of a block of statements Each time the statements in the block."— Presentation transcript:

1 Introducing Loop Statements Liang, pages 82-86 Loop statements control repeated execution of a block of statements Each time the statements in the block are carried out, we speak of one iteration of the loop

2 Structure of a Loop 1.Evaluate the continue-condition (a.k.a. the loop guard) 2.If the continue-condition is true, execute the loop body and go back to 1 3.If the continue-condition is false, skip the loop body entirely and continue with the next line of code

3 The WHILE loop continue- condition? loop-body statements next statement false true while (i < 100) { System.out.print("."); i++; }

4 Printing 100 dots int i = 0; while (i < 100) { System.out.print("."); i++; } i < 100? print dot i++ next statement false true

5 Another example int count = 1; while (count != 100) { System.out.print(count + " "); count++; } Output: 1 2 3 4 5 6 7 8 9 10 11 12........97 98 99

6 Counting backwards int count = 100; while (count != 0) { System.out.print(count + " "); count--; } Output: 100 99 98 97 96 95..............3 2 1

7 Add up some integers 1.Set 'sum' to zero 2.Read in an integer (from JOptionPane; convert to int using Integer.parseInt() 3.While 'data' is not zero.. 1.Add 'data' to 'sum'; 2.Read in another integer into 'data' 4. Print out the value of 'sum'

8 Adding program int sum; int data; String dataString; dataString=JOptionPane.showInputDialog(null,”enter number:”); data = Integer.parseInt(dataString); while(data != 0) { dataString = JOptionPane.showInputMessage(null,”enter number:”); data = Integer.parseInt(dataString); sum = sum + data; //could have used: sum += data; } JOptionPane.showMessageDialog(null,”Numbers sum to ”+sum);

9 What is wrong with this loop? int count = 0; while (count != 99) { System.out.print(count + " "); count = count + 2; }

10 Sequence processing Many tasks require the program to process a series of numbers. Question: how do we know when to stop? Answer 1: know in advance how long the sequence is... Answer 2: provide a sentinel value to signal the end...

11 Sample problems 1) Write a Java program that reads a sequence of positive integer values, terminated by a -1, and calculate their average. 2) Modify your program so that it reads in 5 and only 5 numbers and then calculates their average.

12 Developing a WHILE loop 1.Decide on a loop guard. The loop will terminate when this guard is false. 2.Set up initial conditions for loop. E.g. make sure variables in the guard are initialized. 3.Write the 'work' part of the loop body 4.Write the 'change' part of the loop body. This must potentially alter the value of the guard.

13 Things we know for certain 1.Before the loop: anything could be true 2.At the start of the loop body: the guard is guaranteed to be true 3.Immediately after the loop body: the guard is guaranteed to be false

14 Basic sequence operations Reduction. E.g. finding the average of a sequence Mapping: E.g. printing the square of each member of a sequence Filtering: E.g. doing something only with certain sequence elements

15 Doing it all at once Problem: Write a program which reads a sequence of positive integer values, terminated by -1, and prints the average of the square of the odd elements of the sequence. 1.Find the odd elements (filtering) 2.Compute their squares (mapping) 3.Calculate the average (reduction)


Download ppt "Introducing Loop Statements Liang, pages 82-86 Loop statements control repeated execution of a block of statements Each time the statements in the block."

Similar presentations


Ads by Google