1 / 11

Nested Loops

Nested Loops. CSIS 1595: Fundamentals of Programming and P roblem Solving 1. Nested Loops. Often nest one loop inside another Syntax: while/for of outer loop statements in outer loop while/for of inner loop statements in inner loop more statements in outer loop

eli
Télécharger la présentation

Nested Loops

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. Nested Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1

  2. Nested Loops • Often nest one loop inside another • Syntax:while/for of outer loopstatements in outer loopwhile/for of inner loop statements in inner loop more statements in outer loop • Note that indentation defines what is in inner, outer loop Inner loop run for all cycles For each cycle of outer loop

  3. Example Nested Loop • All three cycles of inner loop executed each time outer loop executed

  4. Modified Trip Mileage Example • Original goal: Keep track of total mileage driven on trip • Additional goals: • Use loop to validate miles entered to insure positive number • Use loop to show “odometer” during this stage of trip

  5. Trip Mileage Example

  6. Incremental Development • If each loop “separate” concept, should build and test one loop at a time • Develop inner loops first, and then put in outer loop (executing inner loop once) • Develop outer loop first, and then add inner loops • Sometimes easier, since don’t have to change indentation

  7. Printing 2-Dimensional Tables • Goal: Print data based on two different components • Example: Tax table based on both income and dependents • Idea: • Each time through outer loop prints entire row • May want to print row header first in loop • Each time through inner loop prints a column in that row

  8. Printing 2-Dimensional Tables • Multiplication table for all X and Y between 1 and 5 • Strategy: • Outer loop runs Y from 1 to 5 • Inner loop runs X from 1 to 5 for each X • Computes and prints X * Y for that X, Y pair • Problem: Must print tab for each print, not end of line • Solution: Use end=“\t” as argument to print • Will also need to print end of line after each row • Use print(“”)at end of outer loop to do this

  9. Multiplication Table

  10. Row and Column Headers • Often want to print labels for each row, column • Strategy: • Use loop beforeouter loop to print column headers • Print additional tab first to leave space for row labels • Print row value at start of each outer loop cycle • Before starting inner loop for that cycle

  11. Modified Multiplication Table

More Related