Results 1 to 6 of 6

Thread: how to make a loop?

  1. #1
    Join Date
    Jan 2009
    Posts
    28
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Unhappy how to make a loop?

    i got a question i know this might have been posted before but how do i make a loop that loops 10 times then stops etc and if u dont have an answer plz dont crtisize

  2. #2
    Join Date
    Jan 2008
    Location
    10° north of Hell
    Posts
    2,035
    Mentioned
    65 Post(s)
    Quoted
    164 Post(s)

    Default

    Quote Originally Posted by karlkarlngng View Post
    i got a question i know this might have been posted before but how do i make a loop that loops 10 times then stops etc and if u dont have an answer plz dont crtisize
    3 ways =)

    Code:
    repeat
      //stuff
      Inc(I);
    until(I >= 10);
    Code:
    for I := 0 to 10 do
    begin
      //stuff
    end;
    Code:
    while(I <= 10) do
    begin
      //stuff
      Inc(I);
    end;

    Dg's Small Procedures | IRC Quotes
    Thank Wishlah for my nice new avatar!
    Quote Originally Posted by IRC
    [22:12:05] <Dgby714> Im agnostic
    [22:12:36] <Blumblebee> :O ...you can read minds

  3. #3
    Join Date
    Jun 2006
    Posts
    75
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Just to add a bit more detail on when you should use each type of loop:

    Code:
    repeat
      //stuff
      Inc(I);
    until(I >= 10);
    Use this loop if you know the code in it needs to be run at least once

    Code:
    for I := 0 to 10 do
    begin
      //stuff
    end;
    Use this type of loop when you know how many times the code in it will be run (i.e. Your end condition is numberic rather than logical)

    Code:
    while(I <= 10) do
    begin
      //stuff
      Inc(I);
    end;
    Use this type when the code in the loop does not necessarily need to be run

  4. #4
    Join Date
    Aug 2009
    Location
    Inside the Matrix...yes it has me, and it has you too.
    Posts
    1,896
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Just want to add a bit and explain some things...

    you need to call I as an integer. very simple.

    SCAR Code:
    program New;

    var
      I: Integer;

    begin
    end.

    Now I automatically has the value of 0.
    The procedure
    SCAR Code:
    Inc(I);
    Is pretty much
    SCAR Code:
    I := I + 1;
    It is just adding one onto the value of the number. Therefor every time it would go through one of the loops above, it would add the value of one onto the integer I, and when I is equal to 10, it would break out of the loop.

    If you need anything else, or don't understand something, don't be afraid to post, no one will criticize
    NYAN NYAN NYAN NYAN NYAN NYAN NYAN NYAN NYAN NYAN NYAN NYAN NYAN

  5. #5
    Join Date
    Jan 2008
    Location
    10° north of Hell
    Posts
    2,035
    Mentioned
    65 Post(s)
    Quoted
    164 Post(s)

    Default

    Quote Originally Posted by bionicle1800 View Post
    Just want to add a bit and explain some things...

    you need to call I as an integer. very simple.

    SCAR Code:
    program New;

    var
      I: Integer;

    begin
    end.

    Now I automatically has the value of 0.
    The procedure
    SCAR Code:
    Inc(I);
    Is pretty much
    SCAR Code:
    I := I + 1;
    It is just adding one onto the value of the number. Therefor every time it would go through one of the loops above, it would add the value of one onto the integer I, and when I is equal to 10, it would break out of the loop.

    If you need anything else, or don't understand something, don't be afraid to post, no one will criticize
    Just wanna add one more thing =)

    Code:
    IncEx(I, x); //Where x is a number
    comes out as
    Code:
    I := I + x; //Where x is a number

    Dg's Small Procedures | IRC Quotes
    Thank Wishlah for my nice new avatar!
    Quote Originally Posted by IRC
    [22:12:05] <Dgby714> Im agnostic
    [22:12:36] <Blumblebee> :O ...you can read minds

  6. #6
    Join Date
    Sep 2006
    Posts
    5,219
    Mentioned
    4 Post(s)
    Quoted
    1 Post(s)

    Default

    Code:
    program New;
    var i:integer;
    begin
      i := 0;  //starts out as 0 by default, but just in case you use used i before...
      repeat
        writeln(inttostr(i));
        Inc(i);
      until i >= 10;
      writeln('');
    
      i := 1;
      repeat
        writeln(inttostr(i));
        Inc(i);
      until i >= 11;
      writeln('');
    
      i := 0;
      repeat
        writeln(inttostr(i));
        Inc(i);
      until i > 9;
      writeln('');
    
      i := 1;
      repeat
        writeln(inttostr(i));
        Inc(i);
      until i > 10;
      writeln('');
    
      for i := 0 to 9 do //this makes i start at 0 anyway
      begin
        writeln(inttostr(i));
      end;
      writeln('');
    
      for i := 1 to 10 do 
      begin
        writeln(inttostr(i));
      end;
      writeln('');
    
      i := 0;
      while i < 10 do
      begin
        writeln(inttostr(i));
        Inc(i);
      end;
      writeln('');
    
      i := 0;
      while i <= 9 do
      begin
        writeln(inttostr(i));
        Inc(i);
      end;
      writeln('');
    
      i := 1;
      while i < 11 do
      begin
        writeln(inttostr(i));
        Inc(i);
      end;
      writeln('');
    
      i := 1;
      while i <= 10 do
      begin
        writeln(inttostr(i));
        Inc(i);
      end;
      writeln('');
    
    end.
    These should all do something 10 times.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •