Hi,

I hope you can answer these simpler questions, and then translate them into your original questions.

Outside/inside (or what is repeated): How many times is the user prompted? Answer that question to yourself with either one or zero to infinity.

Remember that a loop is a way to get something you wrote once to happen many times. If you write the code that reads from the user once, and then want it to actually happen many times, you must enclose that code in a loop. Then you must instruct the loop how to repeat the inner part.

Foreach / while (or how it is repeated): Do you know how many items you will have in advance? A foreach loop takes an array that is already filled up and then runs the enclosed part once for each element.

A while loop reiterates as long as a condition is true. Is the user finished inputing data? asks the question "are we done?". This translates to while the user hasn't finished entering data, or more simply while the user still wants to enter data. Compare this with for each piece of data the user entered.

I hope this makes things clearer =)

-nuffin
zz zZ Z Z #!perl

In reply to Re: seeking advice on loops by nothingmuch
in thread seeking advice on loops by sierrastar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.