Difference between revisions of "ReNamer:Pascal Script:Break script execution"

From den4b Wiki
Jump to navigation Jump to search
(cleanup and more examples for exit and break)
Line 1: Line 1:
Using the exit command breaks the current pass of the script. However it doesn’t stop the script from being executed for the next file in the files table and so on. We encounteer here similar problem as with initialization of variables. Solution is also similar. We will use a boolean to keep track if the main code of the script should be executed or not.
+
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.
 +
 
 +
<source>
 +
begin
 +
  // do some processing ...
 +
  if <Condition> then Exit;
 +
  // do more processing ...
 +
end.
 +
</source>
 +
 
 +
== 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.
  
 
<source>
 
<source>
 
var
 
var
TimeToExit : boolean;
+
  I: Integer;
 +
begin
 +
  for I := 1 to 10 do
 +
  begin
 +
    // do some processing ...   
 +
    if <Condition> then Break;
 +
  end;
 +
  // do more processing ...
 +
end.
 +
</source>
  
 +
== 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 [[ReNamer:Pascal Script:Initialization of variables|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.
 +
 +
<source>
 +
var
 +
  TimeToExit: Boolean;
 
begin
 
begin
 
   if not TimeToExit then
 
   if not TimeToExit then
 
   begin
 
   begin
     //that’s the block for the main script code
+
     // that's the block for the main script code
     if DialogYesNo('Would you like to exit script?') then TimeToExit := true;
+
     if DialogYesNo('Would you to stop script execution?') then
    exit;
+
      TimeToExit := True;
 
   end;
 
   end;
 
end.
 
end.
 
</source>
 
</source>
  
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.<br><br>
+
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.

Revision as of 13:23, 13 August 2009

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 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.