ReNamer:Pascal Script:Break script execution

From den4b Wiki
Jump to: navigation, search

There are several ways to break the execution of a script. Using the Exit command breaks the current pass of the script, while using the Break command breaks the execution of a loop. One can also design the script in such a way that will not require usage of either of these commands, by correctly structuring the script with If..Then..Else statements and looping mechanisms. But sometimes you come across cases when it is easier to simply break the execution with one command, instead of redesigning the whole script.

Exit command

Below is a simple demo of how to use Exit command. If <Condition> is met, the script will break its execution, otherwise, script will continue execution.

begin
  // do some processing ...
  if <Condition> then Exit;
  // do more processing ...
end.

Break command

And here is a simple demo for using Break command, which will break the loop when <Condition> is met and will continue executing everything after the loop.

var
  I: Integer;
begin
  for I := 1 to 10 do
  begin
    // do some processing ...    
    if <Condition> then Break;
  end;
  // do more processing ...
end.

Stop execution for next files

As we have learned the Exit command breaks the current pass of the script, but it doesn't stop the script from being executed for the next file in the files table and so on. We encounter here a similar problem as with initialization of variables. Solution is also similar. We will use a variable to keep track if the main code of the script should be executed or not.

var
  TimeToExit: Boolean;
begin
  if not TimeToExit then
  begin
    // that's the block for the main script code
    if DialogYesNo('Would you like to stop script execution?') then
      TimeToExit := True;
  end;
end.

So the script is still executed for every file, but it's main code is executed only until TimeToExit becomes TRUE. After that script keeps starting just to see that it has nothing to do.