Results 1 to 15 of 15

Thread: Repeat / Until probleem.

  1. #1

    Repeat / Until probleem.

    Hallo allemaal,

    Als Sw = False dan werkt deze code niet. Terwijl mijn idee is dat bij If Sw = False de daarbij behorende Repeat niet zal worden uit gevoerd maar de volgende repeat wel.
    Als Sw = False dan werkt het niet, dan wordt de volgend repeat ook niet uitgevoerd.
    Is hiet een oplossing voor?

    Met een vriendelijke groet, Frans

    Code:
    procedure TForm1.Button1Click(Sender: TObject);
    var
      I: integer;
    
    begin
      I := 0;
      if Sw = True then repeat
      repeat
        Memo1.Lines.Add(IntToStr(I));
        Inc(I);
      until I >= 20;
        Inc(K);
        Memo1.Lines.Add(IntToStr(K)+'  '+IntToStr(I));
      if Sw = True then until K >= 2;
    end;
    
    procedure TForm1.Button2Click(Sender: TObject);
    begin
      Sw := True;
    end;
    
    procedure TForm1.Button3Click(Sender: TObject);
    begin
      K := 0;
      Sw := False;
    end;

  2. #2
    Hi,
    Basically the code written is logically nonsense.
    You cant have a conditional Until. You can have an until with a condition, but not prepend it with If then.
    Lets say your first if statement (for the repeat statement) would have evaluated to false, while the 2nd if statement (for the until statement) evaluates to true... then you would have an until statement without a repeat. Or visa verse.
    That is syntactically incorrect. A repeat statement MUST ALWAYS unconditionally be paired with an until statement.
    The true problem, is the compiler ought to have thrown a syntax error on this.

  3. #3
    Hallo Dubbeld, zie dat je 2 keer repeat aanroept (vlak achter elkaar), de tweede repeat wacht op de tweede until.....
    zou de onderstaande code niet beter gaan ?

    procedure TForm1.Button1Click(Sender: TObject);
    var
    I: integer;
    begin
    Memo1.Lines.Clear;
    I := 0;
    if Sw = True then
    repeat
    Memo1.Lines.Add(IntToStr(I));
    Inc(I);
    until I >= 20;
    if Sw = False then
    repeat
    Inc(K);
    Memo1.Lines.Add(IntToStr(K)+' '+IntToStr(I));
    until K >= 2;
    end;

  4. #4
    Altijd even de formatter eroverheen gooien want die code wiebelt nu van hot naar haar.

    Delphi Code:
    1. procedure TForm1.Button1Click(Sender: TObject);
    2. var
    3.   I: integer;
    4. begin
    5.   I := 0;
    6.   if Sw = True then
    7.     repeat
    8.       repeat
    9.         Memo1.Lines.Add(IntToStr(I));
    10.         Inc(I);
    11.       until I >= 20;
    12.       Inc(K);
    13.       Memo1.Lines.Add(IntToStr(K) + '  ' + IntToStr(I));
    14.       if Sw = True then
    15.     until K >= 2;
    16. end;

    Nu zul je gelijk begrijpen wat er precies gebeurd.

    Overigens is een semicolon ; niet nodig voor een until.
    Hetzelfde als voor een end.
    Zie ook http://wiki.freepascal.org/REPEAT..UNTIL
    Vandaar dat de code prima legaal is.

  5. #5
    This part:
    if Sw = True then
    until K >= 2;

    is not valid code and ought to be throwing a syntax error. It is a condition on if the until statement will be executed or not. repeat/until is an atomic syntax. One cant exist without the other, why it imo is invalid code, despite the compiler accepting it.

  6. #6
    How about
    Code:
      if Sw = True then K := 9
    until K >= 2;
    (Without a semicolon after 9)

    Is that valid?

    The until doesn't 'belong' to the if/then. But the if/then doesn't need to be concluded by ; if it's before end or until (according to the documentation).
    https://www.freepascal.org/docs-html/ref/refsu60.html

    Note that the last statement before the until keyword does not need a terminating semicolon, but it is allowed.
    Thevonly problem I can think of is that a statement after "if x then" would be mandatory. But I don't think it is.
    If x then ; is valid too.
    So
    Code:
      If x then
    end;
    would be valid too.

  7. #7
    John Kuiper
    Join Date
    Apr 2007
    Location
    Almere
    Posts
    8,747
    if Sw = True then K := 9
    Dit staat niet in de code, rik.
    if Sw = True then until K >= 2;
    Dit wel en dan kan ik Kim begrijpen wat hij bedoeld. Je kan een repeat .... until statement niet afsluiten door middel van een if statement.
    Delphi is great. Lazarus is more powerfull

  8. #8
    Quote Originally Posted by jkuiper View Post
    Je kan een repeat .... until statement niet afsluiten door middel van een if statement.
    Ja, maar dat is dus ook niet wat er staat.
    De until staat al gewoon op zichzelf.
    Alleen de programmeur dacht dat de until bij de if/then hoorde. Maar de compiler dus niet.

    Met if x then ; mag je een lege statement hebben.
    En de ; is niet nodig voor een end of until.

    Dus
    Code:
      If x then ;
    Until y;
    Is hetzelfde als
    Code:
      If x then
    Until y;
    Omdat de ; hier niet verplicht is.
    Vandaar dat de compiler hier niet over struikelt.
    (Eigenlijk nog niet onder Delphi getest)

    Maar je mag gerust een bugfix indienen of dit aankaarten in de developers-mailinglist en kijken wat de compilers-devs hier van vinden.

    Edit:
    Nope, Delphi geeft ook gewoon aan dat het goed is.
    Delphi Code:
    1. var
    2.   x, y: Boolean;
    3. begin
    4.   x := true;
    5.   y := true;
    6.   repeat
    7.     if x then
    8.   until y;
    9. end;

    Nog een overweging is dat until (net als end) een keyword is. En voor een keyword hoef je geen semicolon te doen. De semicolon is in principe alleen voor tussen statements (wat until dus niet is). Dus grammaticaal is het ook gewoon correct. De programmeur moet natuurlijk wel weten dat until (net als end) geen statement is en dus niet gebruikt kan worden met if e.d.
    Last edited by rvk; 27-May-19 at 11:20.

  9. #9
    Senior Member
    Join Date
    Aug 2004
    Location
    Rotterdam
    Posts
    151
    Als je de code maakt zoals hieronder dan doet de hoofd repeat wat de thread-starter wil:

    Code:
        procedure TForm1.Button1Click(Sender: TObject);
        var
          I: integer;
        begin
          I := 0;
    //      if Sw = True then
            repeat
              repeat
                Memo1.Lines.Add(IntToStr(I));
                Inc(I);
              until I >= 20;
              Inc(K);
              Memo1.Lines.Add(IntToStr(K) + '  ' + IntToStr(I));
    //          if Sw = True then
            until (K >= 2) or (Sw = false);
        end;
    Nu wordt eerste repeat alleen herhaald als (Sw = true)

  10. #10
    mov rax,marcov; push rax marcov's Avatar
    Join Date
    Apr 2004
    Location
    Ehv, Nl
    Posts
    10,357
    Quote Originally Posted by rvk View Post
    How about
    Code:
      if Sw = True then K := 9
    until K >= 2;
    (Without a semicolon after 9)

    Is that valid?
    I think so. Until is a keyword and not a statement, and ; is a statement separator (contrary to curly braces languages where it is often a statement terminator)

    IOW repeat..until is like begin..end in this sense, and you don't need a ; before an end; for the same reason. A statement terminates at a keyword, an if then without ; at the end of a procedure should be similar.

  11. #11
    Quote Originally Posted by marcov View Post
    IOW repeat..until is like begin..end in this sense, and you don't need a ; before an end; for the same reason. A statement terminates at a keyword, an if then without ; at the end of a procedure should be similar.
    Yep, that's why I also think
    Code:
      If x then
    Until y;
    is perfectly valid. It just didn't behave like TS thought it should.

  12. #12
    mov rax,marcov; push rax marcov's Avatar
    Join Date
    Apr 2004
    Location
    Ehv, Nl
    Posts
    10,357
    Ok, sorry if I quoted too selectively.
    Last edited by marcov; 28-May-19 at 00:15. Reason: added an "o"

  13. #13
    Zit ik weer op ENDelphi.co.uk?
    1+1=b

  14. #14
    mov rax,marcov; push rax marcov's Avatar
    Join Date
    Apr 2004
    Location
    Ehv, Nl
    Posts
    10,357
    Het is een mengeling van Engels en Nederlands achtig. Dus .co.za ligt meer voor de hand :-)

  15. #15
    Daar is iets in. Ek dink jy is reg, Markov.
    1+1=b

Thread Information

Users Browsing this Thread

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

Bookmarks

Posting Permissions

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