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

From den4b Wiki
Jump to navigation Jump to search
(Categorised)
m (Text replacement - "<source>" to "<syntaxhighlight lang="pascal">")
Line 7: Line 7:
 
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.
 
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>
+
<syntaxhighlight lang="pascal">
 
begin
 
begin
 
   // do some processing ...
 
   // do some processing ...
Line 19: Line 19:
 
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.
 
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>
+
<syntaxhighlight lang="pascal">
 
var
 
var
 
   I: Integer;
 
   I: Integer;
Line 36: Line 36:
 
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.
 
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>
+
<syntaxhighlight lang="pascal">
 
var
 
var
 
   TimeToExit: Boolean;
 
   TimeToExit: Boolean;

Revision as of 16:00, 8 February 2017

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.

<syntaxhighlight lang="pascal"> 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.

<syntaxhighlight lang="pascal"> var

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

<syntaxhighlight lang="pascal"> 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. </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.