1 / 16

The Repetition Control Structure 2.

The Repetition Control Structure 2. 0. Admin. 1. Loop design. 2. In-class and lab exercise on designing loops. 0. Admin. Return program 3. Questions on program 4 / Hw 3? Next week: Tuesday: Review for exam 1. Thursday: exam 1. On program 4.

beau-malone
Télécharger la présentation

The Repetition Control Structure 2.

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. The Repetition Control Structure 2. 0. Admin. 1. Loop design. 2. In-class and lab exercise on designing loops.

  2. 0. Admin. • Return program 3. • Questions on program 4 / Hw 3? • Next week: • Tuesday: Review for exam 1. • Thursday: exam 1.

  3. On program 4 • Note that the main validation loop should run until the entire date is valid. • Inside the loop it may be easiest to first check the month and year to be reasonable, then focus on the most complex case, whether the day is consistent with the month and year.

  4. 1. Loop design. • Recall from last time--there are 4 factors in correct loop design: • 1) INITIALIZATION; • 2) LOOP CONDITION; • 3) UPDATE; • 4) FINALIZATION.

  5. Loop design (cont.). • 1) INITIALIZATION. • For any loop, set counters and accumulators to zero (or other initial value) and set LCV: • A. For count-controlled loops, initialize counter. • Count = 0; • B. For sentinel-controlled, loops, do priming read of sentinel variable. • Console.Write (“Enter age (-1 to quit)”); • Age = Convert.ToInt32 (Console.ReadLine()); // read first age before loop

  6. Loop design (cont.). • C. For flag-controlled loops, set a flag if needed. • bool ValidOption = false; • Data, unlike people, are “guilty” until proven “innocent.”

  7. Loop design (cont.). • 2) LOOP CONDITION. • Make sure the loop runs the correct number of times. Suppose you want a loop to execute 10 times and code: • Count = 0; • while (Count <= 10) { // do processing Count++; } // What will it do?

  8. Loop design (cont.). • LOOP CONDITION (cont.). • Also be careful with logic. What will the following loop do? • while ( (Code != ‘A’) | | (Code != ‘C’) | | (Code != ‘E’) | | (Code != ‘G’) ) { Console.Write(“Error. Code must be A,C, E or G.”); Code = Convert.ToChar (Console.ReadLine()); // re-input } // end while

  9. Loop design (cont.). • LOOP CONDITION (cont.). • What is the problem with the code? • How can it be corrected?

  10. Loop design (cont.). • 3. UPDATE. • Ensure that the loop control variable and any other counters and accumulators, etc. are updated in the loop body. • If the LCV is not updated, what happens? • If other counters and accumulators are not updated, what happens?

  11. Loop design (cont.). • 4. FINALIZATION. • When the loop is exited are the final values of key variables correct? • E.g. what goes wrong in the next example?

  12. Loop design (cont.). TotalAge = 0; Console.Write (“Enter age (-1 to quit)”); Age = Convert.ToInt32 (Console.ReadLine()); // priming read while (Age != -1) { Console.Write (“Enter age (-1 to quit)”); Age = Convert.ToInt32 (Console.ReadLine()); // read next TotalAge += Age; } // end loop What will it do? 2 mistakes.

  13. Loop design (cont.). • What happens to the first age? • What happens to the sentinel value? • Consider the following input ages: 20 25 30 Should total 75, but according to the code?

  14. Loop design (cont.). • What is the problem? • How can we solve it? What should the code look like?

  15. Loop design (cont.). TotalAge = 0; Console.Write (“Enter age (-1 to quit)”); Age = Convert.ToInt32 (Console.ReadLine()); // priming read while (Age != -1) { TotalAge += Age; Console.Write (“Enter age (-1 to quit)”); Age = Convert.ToInt32 (Console.ReadLine()); // read next } // end loop Note the read next must occur at the bottom of the loop.

  16. 2. In-class / lab exercise on coding loops. • See the hand-out problem sheet. • In class: use the 4 factors of loop design to create solutions for the 2 problems. • Desk-check your solutions. • Go into the lab, and create 2 programs that implement your design in C#. • Test and debug your programs.

More Related