delphiif-statementcasedelphi-2006

Delphi 2006 proper use of END


Look at this code:

if Poruka(BM_UWARN, GetMsg('Da li zaista želite da odbijete nalog?')) = mrOk then
  begin
    tmps := TRN.ss;
    TrnPom.ss := TRN.ss;
    RadnikOdbio.l := AdresniSlog.Rabotnik;
    if Zakljucaj(1011, tmps, 3) then
      begin
        FRazlogOdbNal.ShowModal;
        case FRazlogOdbNal.ModalResult of
          mrOk:              
            BeBankComment.ss := FRazlogOdbNal.beRazlogOdbijanja.ss;              
          else
            Otkljucaj(1011, tmps, 3);
            exit;
        end;
      end;
   end;

It is not important what this code does, what I need help with is did I use enough ENDs in this part of code. So the first END is of CASE statement. Second END closes BEGIN of if Zakljucaj(1011,tmps,3) and the last END is closing BEGIN of starting IF. Is this the right way to close them, does compiler now which end refers to which part of the code? And also, does compiler knows that the else part refers to case statement? If not, is there a way to explicitly do this?


Solution

  • Your code is syntactically correct. The indentation is however a little off, you should not indent the begin of a block. The default behavior of the Rad Studio auto formatter is to align them like this:

    if Poruka(BM_UWARN, GetMsg('Da li zaista želite da odbijete nalog?')) = mrOk then
    begin
      tmps := TRN.ss;
      TrnPom.ss := TRN.ss;
      RadnikOdbio.l := AdresniSlog.Rabotnik;
      if Zakljucaj(1011, tmps, 3) then
      begin
        FRazlogOdbNal.ShowModal;
        case FRazlogOdbNal.ModalResult of
          mrOk:
            BeBankComment.ss := FRazlogOdbNal.beRazlogOdbijanja.ss;
        else
          Otkljucaj(1011, tmps, 3);
          exit;
        end;
      end;
    end;
    

    The rest of what you state in your question is correct.