1 / 15

09 Non-deterministic iteration

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

caspar
Télécharger la présentation

09 Non-deterministic iteration

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

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

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

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

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

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

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

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

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

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

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

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

  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 09 Non-deterministic iteration

More Related