150 likes | 313 Views
09 Non-deterministic iteration. CE00858-1: Fundamental Programming Techniques. Objectives. In this session, we will: cover repetition using a while loop see how while is used to read-ahead. Non-deterministic iteration. repeating while a condition holds true
E N D
09 Non-deterministic iteration CE00858-1: Fundamental Programming Techniques 09 Non-deterministic iteration
Objectives In this session, we will: • cover repetition using a while loop • see how while is used to read-ahead 09 Non-deterministic iteration
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 09 Non-deterministic iteration
While statement in Java while (condition) { statement ; } condition is any expression that returns true or false something inside loop must change loop condition braces are needed to block more than one statement 09 Non-deterministic iteration
While example – ThrowSix • problem: • simulate throwing a dice • count how many throws to throw a 6 09 Non-deterministic iteration
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 09 Non-deterministic iteration
Random numbers • random numbers used in games of chance • need random number in range 1 – 6 multiply value by 6 to get range 0.0 – 5.9999 random number is in range 0.0 up to 0.9999 int dice = (int)(Math.random() * 6) + 1; take integral part to get range 0 - 5 add 1 to get range 1 - 6 09 Non-deterministic iteration
ThrowSix code ThrowSix.java //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 09 Non-deterministic iteration
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 09 Non-deterministic iteration
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 numto sum • input next num • what operations are done once after the loop? • output sum 09 Non-deterministic iteration
PositiveSum.java //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 ); read first num before entering loop remain in loop while num is not sentinel statement inside loop to change condition 09 Non-deterministic iteration
Common mistake 1 • problem… num = kybd.nextInt(); while (num != -1) sum = sum + num ; //input next number num = kybd.nextInt(); 09 Non-deterministic iteration
Common mistake 2 • problem... num = kybd.nextInt(); while (num != -1); { sum = sum + num ; //input next number num = kybd.nextInt(); } 09 Non-deterministic iteration
Common mistake 3 • problem… num = kybd.nextInt(); while (num != -1) { sum = sum + num ; } 09 Non-deterministic iteration
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 09 Non-deterministic iteration