Pascal changing (for,do) to (repeat until, while do)

Discussion in 'OT Technology' started by matt62182, May 9, 2008.

  1. matt62182

    matt62182 New Member

    Joined:
    Mar 25, 2007
    Messages:
    736
    Likes Received:
    0
    Location:
    MS
    my teacher gave us an assignment where we have to change our (for, do )statements to (repeat until, while do)....i don't quite get what's going on (i know i didn't indent, i will go back and change it)

    for example

    program sin_city;
    var
    k,m,n:integer;
    procedure makeroof;
    begin
    for m:=1 to 5 do
    begin
    for k:=5 downto m do
    write(' ');
    for k:=1 to m*2-1 do
    write ('/');
    writeln ('/');
    end;
    end; (*makeroof*)
    procedure maketop;
    begin
    for m:=1 to 3 do
    begin
    for k:=1 to 11 do
    write ('*');
    writeln;
    end;
    end; (*maketop*)
    procedure makebase;
    begin for m:=1 to 4 do
    begin
    for k:=1 to 3 do
    write ('*');
    for k:=1 to 5 do
    write (' ');
    for k:=1 to 3 do
    write ('*');
    writeln;
    end;
    end; (*makebase*)
    procedure maketower;
    begin
    for k:=1 to 23 do
    write ('_');
    writeln;
    for m:=1 to 4 do
    begin
    for k:=1 to 3 do
    write (' ':1);
    write ('***');
    for k:=1 to 19 do
    write (' ');
    for k:=1 to m do
    write ('*');
    writeln;
    end;
    end; (*maketower*)
    procedure makeyard;
    begin
    writeln;
    for m:=1 to 10 do
    begin
    for k:=1 to 15 do
    write ('*');
    writeln;
    end;
    end; (*makeyard*)
    procedure makehouse;
    begin
    makeroof;
    maketop;
    makebase;
    end;
    procedure makefed;
    var
    x:integer;
    begin
    makeroof;
    for x:=1 to 5 do
    maketop;
    makebase;
    end;
    begin (*main*)
    makefed;
    for n:=1 to 10 do
    begin
    makehouse;
    end;
    makefed;
    writeln;
    writeln;
    maketower;
    writeln;
    writeln;
    makeyard;
    readln
    end.


    what exactly should i be doing to change this to a (repeat until, while do)

    when do use (while do) and when do u use (repeat until)?
    thanks in advance for any help
     
  2. critter783

    critter783 OT Supporter

    Joined:
    Jul 15, 2005
    Messages:
    1,785
    Likes Received:
    0
    For loops are used when the number of iterations maps to an enumeration, for example, you want to operate on every item in an array. The number of iterations directly corresponds to the number of entries in the array. For loops are also used when the loop performs some operation that depends on the iteration counter.

    The two loops are equivalent. You can easily rewrite a for loop as a while loop by manually managing the value of the for loop's iteration counter variable in the body of the loop; the counter becomes the while loop's sentinel

    While loops are typically used when you want a block of code to execute until a condition is met. Typically, you would maintain a variable in the loop; this variable is known as a sentinel. When the sentinel contains a particular value, you would stop iterating.
     

Share This Page