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

From den4b Wiki
Jump to navigation Jump to search
m (Reverted edits by Waexu (Talk) to last revision by Den4b)
m (Text replacement - "</source>" to "</syntaxhighlight>")
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
{{Up|ReNamer:Pascal Script}}
 +
 
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.
 
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.
  
Line 5: 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 11: Line 13:
 
   // do more processing ...
 
   // do more processing ...
 
end.
 
end.
</source>
+
</syntaxhighlight>
  
 
== Break command ==
 
== Break command ==
Line 17: 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 28: Line 30:
 
   // do more processing ...
 
   // do more processing ...
 
end.
 
end.
</source>
+
</syntaxhighlight>
  
 
== Stop execution for next files ==
 
== Stop execution for next files ==
Line 34: 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;
Line 45: Line 47:
 
   end;
 
   end;
 
end.
 
end.
</source>
+
</syntaxhighlight>
  
 
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.
 
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.
 +
 +
[[Category:ReNamer]]
 +
[[Category:Pascal Script]]

Latest revision as of 15: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.

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.