Exit from an "if" in Delphi

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;

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;

This is commonly implemented like this:

function testOne: Boolean;
begin
  ...
end;

function testTwo: Boolean;
begin
  ...
end;

procedure runTests;
var
  bOK: Boolean;

begin
  bOK = True;

  if bOK then
  begin
    // Test something
    bOK = testOne;
    // If we passed, keep going
  end;

  if bOK then
  begin
    // Test something else
    bOK = testTwo;
    // If we passed, keep going
  end;

  ...
end;

Tags:

Delphi