Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exit from an "if" in Delphi

Tags:

delphi

you can exit a while loop with break.

How to exit from an if .

Is there a kind of GOTO in Delphi ?

procedure ...
begin

  if .... then
    begin

      here the code to execute

      if (I want to exit = TRUE) then
        break or GOTO

      here the code not to execute if has exited

    end;

  here the code to execute

end;
like image 896
user382591 Avatar asked Jul 27 '12 10:07

user382591


2 Answers

Like Piskvor mentioned, use nested if statement:

procedure Something;
begin    
  if IWantToEnterHere then
  begin
    // here the code to execute    
    if not IWantToExit then
      // here the code not to execute if has exited
  end;    
  // here the code to execute
end;
like image 72
TLama Avatar answered Sep 23 '22 05:09

TLama


I am not in favour of using Exit's this way, but you asked for it...

Like @mrabat suggested in a comment to @Arioch 'The answer, you could use the fact that a finally block is always executed, regardless of Exit's and exceptions, to your advantage here:

procedure ...
begin

  if Cond1 then
  try
    // Code to execute

    if Cond2 then
      Exit;

    // Code NOT to execute if Cond2 is true
  finally
    // Code to execute even when Exit was called
  end;
end;
like image 26
Marjan Venema Avatar answered Sep 22 '22 05:09

Marjan Venema