Effective programming w/ loops...not getting it :( 
Author Message
 Effective programming w/ loops...not getting it :(

Hello!

    Sorry to keep posting questions about loops. For some reason I'm just
not getting it. From what I've posted, all of my comments have been much
appreciated. Some have said that (the programs that work) even though 100%
correct my programming could be more practical and effective. So here is a
couple of questions:

    1. I would like to create a menu with 4 options  1)Total Amount
2)Entered Amounts  3)Draw amount out   4)Leave      To be effective here
which loop would be the best for this situation? I think I've ruled out
Repeat-Until (since it looks for something particualr). So I'm left with
While-Do and For-Do. I was planning on using While-Do but thought I would
see if For-Do would be better for this case?

    2. In this part of a program, I'm generating a random number. It runs
fine until I get to this procedure and when I get to this point, it puts out
50 sevens. Why?

    {**************************}
      procedure randomgen;

           begin
             clrscr;
             For i := 1 to loopmax do
                  randomize;
                     begin
                        num := random(max);
                        write(num:5);
                     end;
                  readln;
            end;

     {*************************}

    I'm determined to get this figured out sooner or later! Thanks for all
the help.

    :) Evan Anderson



Wed, 18 Jun 1902 08:00:00 GMT  
 Effective programming w/ loops...not getting it :(

Quote:
>     Sorry to keep posting questions about loops. For some reason I'm just
> not getting it. From what I've posted, all of my comments have been much
> appreciated. Some have said that (the programs that work) even though 100%
> correct my programming could be more practical and effective. So here is a
> couple of questions:

>     1. I would like to create a menu with 4 options  1)Total Amount
> 2)Entered Amounts  3)Draw amount out   4)Leave      To be effective here
> which loop would be the best for this situation? I think I've ruled out
> Repeat-Until (since it looks for something particualr). So I'm left with
> While-Do and For-Do. I was planning on using While-Do but thought I would
> see if For-Do would be better for this case?

   No, a Repeat loop is probably best: you stay in the loop _until_ the
user enters the menu command which gets out.  A For loop is completely
wrong (because you'll stay in the loop a fixed number of iterations,
regardless of what menu item the user selects!), and a While loop implies
forcing the loop entry condition to be set.

Quote:
>     2. In this part of a program, I'm generating a random number. It runs
> fine until I get to this procedure and when I get to this point, it puts out
> 50 sevens. Why?

>     {**************************}
>       procedure randomgen;
>            begin
>              clrscr;
>              For i := 1 to loopmax do
>                   randomize;
>                      begin
>                         num := random(max);
>                         write(num:5);
>                      end;
>                   readln;
>             end;
>      {*************************}

   Because you have the Randomize call in the loop!  This is completely
wrong, because Randomize is meant to be called only _once_ in the
program.  The call should be outside the loop - preferably at the start
of the program.
   Next, we don't see the values for LoopMax and Max.  I assume that
LoopMax = 50, and I can't tell what Max is.  If you're seeking values in
the range 1..Max, you need to use: Num := Random(Max)+1 - because Random
produces values in the range 0..Max-1.
   So, your routine should look like this:
procedure RandomGen;
var I,Num : integer;
begin
  ClrScr;
  for I := 1 to LoopMax do
    begin
      Num := Random(Max)+1; write (Num:3);
      if I Mod 24 = 0 then Writeln
    end;
  Writeln
end;

- Show quoted text -

Quote:
>     I'm determined to get this figured out sooner or later! Thanks for all
> the help.



Wed, 18 Jun 1902 08:00:00 GMT  
 Effective programming w/ loops...not getting it :(

Quote:

>    2. In this part of a program, I'm generating a random number. It runs
>fine until I get to this procedure and when I get to this point, it puts out
>50 sevens. Why?

>    {**************************}
>      procedure randomgen;

>           begin
>             clrscr;
>             For i := 1 to loopmax do
>                  randomize;
>                     begin
>                        num := random(max);
>                        write(num:5);
>                     end;
>                  readln;
>            end;

>     {*************************}

Is that really a cut and paste of your code? The loop executes only the
randomize, nothing else. Put randomize as the first line in your
program. Do not call it after that. In fact at beginning you could get
rid of randomize as it brings unpredictability, one cannot repeat
exactly same sequence. Of course you also need to test with randomize as
that bay show new errors,

Osmo



Wed, 18 Jun 1902 08:00:00 GMT  
 Effective programming w/ loops...not getting it :(

Quote:

> Hello!

>     Sorry to keep posting questions about loops. For some reason I'm just
> not getting it. From what I've posted, all of my comments have been much
> appreciated.

     There are three general types of loops (there's a minor variation on the
first two, which I'll try to remember to mention).  In one, you want to do
something as long as a particular condition holds (it may or may not hold when
you start the loop).  In another, you want to do something until something else
changes.  In the final kind of loop, you want to do something a specific (and,
for that loop, fixed) number of times.

     These situations correspond to the WHILE .. DO loop, the REPEAT .. UNTIL
loop, and the FOR .. DO loop.  Let me take the last loop first, as it is,
perhaps, conceptually the simplest.  Use for FOR loop when you want to do
something a fixed number of times, and you have a way of counting each time.
The syntax of the
FOR loop, as you know, is
   FOR <index> := <startvalue> TO <stopvalue> DO <statement>
This will execute the single (or compound, surrounded by BEGIN .. END) statement
a fixed number of times, with the variable <index> taking on the sequential
values <startvalue> to <stopvalue>.  Note that while executing <statement>, the
value of <index> is defined, and will be one of the values <startvalue> ..
<stopvalue>, but when the loop ends, the value of <index> is not defined, and
should not be used without being re-initialized.  Also note that if <startvalue>
exceeds <stopvalue>, so there are no sequence of values for <index>, the loop is
not executed at all!

     The format of the other loops is similar, except that the loops are guarded
by a boolean <condition>.

    WHILE <condition> DO <statement>
    REPEAT <statements> UNTIL <condition>

Notice that the REPEAT loop can contain multiple statements without needing
BEGIN .. END (it is something of a Pascal anomaly).  It is possible for the
WHILE loop to never execute (if <condition> is false to begin with), while a
REPEAT loop will always execute at least once.  Generally, the code within the
loop needs to contain a statement that modifies the condition so that the loop
can (eventually) exit.
It is also important to note that the loop will execute all of its <statement>,
no matter when the condition changes.

     Which brings me to the final kind of loop, one that exits immediately on
some condition, a kind of "break" statement.  Standard Pascal does not allow
this (for several good reasons), but you can "do it yourself" by adding an
additional test guarding an IF statement.  For example,

    WHILE <condition> DO
      BEGIN
        <statement1> (which does not change <condition>)
        <statement2> (which DOES change <condition>)
        IF <condition>
          THEN <statement3> (which will not be executed if <condition is false>)

       END

Notice that with this (legal Standard) Pascal construct, you will effectively
exit the loop, not executing <statement3>, the moment <condition> turns false.

     Hope this clarifies things.

Bob Schor
Pascal Enthusiast



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Speeding up interation within while not Eof loop

2. prompted read in WHILE not eof loop

3. FOR-TO-DO loop not ri

4. Loop not working

5. FOR-TO-DO loop not right?

6. Learner not getting 1st part of procedure to run

7. PDox Table not getting updated

8. Getting file *creation* date, not modified date

9. Why am I not getting the RTE 200?

10. Interbase exception not getting raised by BDE

11. I'M JUST NOT GETTING THIS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

12. OnUpdateRecord-Event gets not called - WHY??

 

 
Powered by phpBB® Forum Software