Presentation is loading. Please wait.

Presentation is loading. Please wait.

09 Non-deterministic iteration1June 15 09 Non-deterministic iteration CE00858-1: Fundamental Programming Techniques.

Similar presentations


Presentation on theme: "09 Non-deterministic iteration1June 15 09 Non-deterministic iteration CE00858-1: Fundamental Programming Techniques."— Presentation transcript:

1 09 Non-deterministic iteration1June 15 09 Non-deterministic iteration CE00858-1: Fundamental Programming Techniques

2 09 Non-deterministic iteration2June 15 Objectives In this session, we will: cover repetition using a while loop see how while is used to read-ahead

3 09 Non-deterministic iteration3June 15 Non-deterministic iteration repeating while a condition holds true repeating until a condition becomes true putting clothes on the line: while there are more clothes in the basket put item on line putting clothes on the line: until the basket is empty put item on line

4 while (condition) { statement ; } 09 Non-deterministic iteration4June 15 While statement in Java condition is any expression that returns true or false braces are needed to block more than one statement something inside loop must change loop condition

5 09 Non-deterministic iteration5June 15 While example – ThrowSix problem: simulate throwing a dice count how many throws to throw a 6

6 09 Non-deterministic iteration6June 15 ThrowSix analysis what data is used? dice: integer in range 1 – 6 randomly generated by program count: integer number of throws calculated by program what operations are needed? iteration needed as dice may be thrown several times what operations are done once before the loop? generate first value in range 1 – 6 and store in dice how many times is loop repeated? loop repeated while a 6 has not been thrown what operations are repeated inside the loop? output dice add 1 to count generate next value in range 1 – 6 and store in dice what operations are done once after the loop? output dice output count

7 09 Non-deterministic iteration7June 15 Random numbers random numbers used in games of chance need random number in range 1 – 6 int dice = (int)(Math.random() * 6) + 1; 4.add 1 to get range 1 - 6 3.take integral part to get range 0 - 5 1.random number is in range 0.0 up to 0.9999 2.multiply value by 6 to get range 0.0 – 5.9999

8 09 Non-deterministic iteration8June 15 //simulate dice throw and output number of throws to get a 6 int dice = (int)(Math.random() * 6) + 1; int count = 1; //while not a 6, throw again while (dice != 6) { System.out.println("Throw: " + dice); count = count + 1; dice = (int)(Math.random() * 6) + 1; } //output count System.out.println("Throw: " + dice); System.out.println("It took " + count + " goes to throw a 6"); loop repeated while dice isn't 6 statement inside loop to change condition ThrowSix.java ThrowSix code

9 09 Non-deterministic iteration9June 15 Read ahead example – PositiveSum data to be processed is terminated by a special marker called a data sentinel program must read first item while item is not the data sentinel the loop is executed next item must be read inside loop problem: read positive integers until -1 entered form their sum and output it

10 09 Non-deterministic iteration10June 15 PositiveSum analysis what data is used? num: positive integer input by user sum: integer tally of numbers calculated by program what operations are needed? iteration needed as several numbers may be input and added what operations are done once before the loop? create Scanner prompt user for number initialise sum to 0 input first num how many times is loop repeated? loop repeated while -1 has not been entered what operations are repeated inside the loop? add num to sum input next num what operations are done once after the loop? output sum

11 09 Non-deterministic iteration11June 15 //form sum of positive integers Scanner myKeyboard = new Scanner(System.in); System.out.print("Enter integers to sum (type -1 to end): "); int sum = 0; // input first number int num = myKeyboard.nextInt(); while (num != -1) { sum = sum + num ; //input next number num = myKeyboard.nextInt(); } System.out.println("Sum is: " + sum ); remain in loop while num is not sentinel PositiveSum.java read first num before entering loop statement inside loop to change condition

12 09 Non-deterministic iteration12June 15 Common mistake 1 problem… num = kybd.nextInt(); while (num != -1) sum = sum + num ; //input next number num = kybd.nextInt();

13 09 Non-deterministic iteration13June 15 Common mistake 2 problem... num = kybd.nextInt(); while (num != -1); { sum = sum + num ; //input next number num = kybd.nextInt(); }

14 09 Non-deterministic iteration14June 15 Common mistake 3 problem… num = kybd.nextInt(); while (num != -1) { sum = sum + num ; }

15 09 Non-deterministic iteration15June 15 Summary In this session we have: covered the while loop and how it is used when the number of repetitions is not known in advance analysed problems involving non-deterministic iteration implemented the while loop seen how while loops are used in situations where the data is read in advance In the next session we will: analyse and implement a program that uses iteration and selection


Download ppt "09 Non-deterministic iteration1June 15 09 Non-deterministic iteration CE00858-1: Fundamental Programming Techniques."

Similar presentations


Ads by Google