1 / 17

MSAS5104 Programming with Data Structures and Algorithms

MSAS5104 Programming with Data Structures and Algorithms. Week 6 Scott Marino. Iteration Pretest loops Post test loops Counter controlled loops Nested loops. Lab 6 and 7 / Homework. Topics. Iteration. A computer is good for handling repetitive tasks

ddoe
Download Presentation

MSAS5104 Programming with Data Structures and Algorithms

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. MSAS5104Programming with Data Structures and Algorithms Week 6 Scott Marino Scott Marino MSMIS Kean University

  2. Iteration Pretest loops Post test loops Counter controlled loops Nested loops Lab 6 and 7 / Homework Topics Scott Marino MSMIS Kean University

  3. Iteration • A computer is good for handling repetitive tasks • The process of repeating a set of sequence instructions is known as an iteration sequence or a loop • Each loop requires a test/condition for exiting the loop, else it will run forever • Each iteration of a loop is commonly referred to as a cycle Scott Marino MSMIS Kean University

  4. Iteration • The exit test for a loop can come at the beginning or end of the looping cycle • Pretest or top-tested loops have the exit test before the loop cycle starts • Top tested loops may never execute if the exit condition is present before the loop starts • Posttest or bottom-tested loops have the exit test at the end of the loop cycle • Bottom tested loops will always execute at least one time since the test is at the end of the cycle Scott Marino MSMIS Kean University

  5. Iteration • From the payroll example: • Gather time card datawhile more employees compute payrollend-while • Gather time card datado compute payrollwhile more employees Scott Marino MSMIS Kean University

  6. Pretest Loops • Syntax: • while (condition) statement; • while (condition) {statement(s);} • This top-tested looping code will execute the statements as long as the condition evaluates to true Scott Marino MSMIS Kean University

  7. Posttest Loops • Syntax: • do statement; while (condition); • do {statement(s); } while (condition); • This bottom-tested looping code will always execute at least one cycle • All loops should have some change of the variable in the condition to allow exit from the loop Scott Marino MSMIS Kean University

  8. Sentinel Values • A sentinel value is an expected value, usually entered via the keyboard, used to exit a loop • It is always a value that would not occur during the normal course of processing • A sentinel value controlled loop is a common programming construct Scott Marino MSMIS Kean University

  9. Counter Controlled Loops • There are instances where a loop would need to be executed a specific number of times • Usually done when the number of input or output items is known • Elements in an array • Characters in a string • Lines on a report • Underscores or asterisks on a report • Using a counter controlled loop, a set of sequence instructions is executed n times Scott Marino MSMIS Kean University

  10. Counter Controlled Loops • The following elements are required for a counter controlled loop • Initialization - to start the counter with a specified value • Test - to exit the loop when the number of iterations is complete • Body - the sequence instructions • Counter - the count of the number of cycles • End - denotes the end of the cycle Scott Marino MSMIS Kean University

  11. Counter Controlled Loops • loopctr = 0;while (loopctr < 4) { cout << loopctr << “\n”; loopctr++;} • Will execute the loop four times • What will the value of loopctr be after the loop is exited? Scott Marino MSMIS Kean University

  12. Counter Controlled Loops - FOR • for (initialization; test; counter) statement; • for (initialization; test; counter) {statement(s);} // end for • The for loop is a specific construct to handle counter based looping • “FOR” loops have a top tested condition Scott Marino MSMIS Kean University

  13. Counter Controlled Loops - FOR • for (loopctr = 0; loopctr < 4; loopctr++) { cout << loopctr << “\n”;} • Will execute the loop cycle four times • Cleaner coding than the while loop with “manual” counter incrementing • What will it print? Scott Marino MSMIS Kean University

  14. Nested Looping • There are instances where a loop inside a loop needs to be executed • The loops are typically called inner and outer loops • For each iteration of the outer loop, a complete cycle of the inner loop is performed Scott Marino MSMIS Kean University

  15. Nested Looping • for (outerctr = 1; outerctr <= 5; outerctr++) { for (innerctr = 1; innerctr <= 10; innerctr++) { cout << “*”; } // end inner loop cout << “\n”;} // end outer loop • Will print a block of asterisks 10 columns wide by 5 lines • The new line character is executed as part of the outer loop Scott Marino MSMIS Kean University

  16. Homework / Lab 6 • Enhance the Celsius / Fahrenheit conversion program • Add a loop to continue execution until the user requests an exit • Add an exit value to the prompt that asks for the conversion of F -> C or C -> F to include an exit value Scott Marino MSMIS Kean University

  17. Homework / Lab 7 • Write a program the prints the multiplication (times) table up to 10x • 1 2 3 41 1 2 3 42 2 4 6 83 3 6 9 124 4 8 12 16 • Hints: • Use the setw() operator to control the spacing • A nested loop is required to produce the output Scott Marino MSMIS Kean University

More Related