Flow ControlΒΆ

Users can control the execution of a G7 script by using its flow control capabilities. These commands allow the user to write compact scripts with less repetition and to perform complex tasks with greater ease.

Examples:

function TEST{
   if( %NARGS == 0 ){
      ic Zero arguments were passed to the function TEST.
      }
   else if( %NARGS == 1 ){
      ic The argument %1 was passed to TEST.
      }
   else{
      ic Two arguments, %1 and %2, were passed to TEST.
      }
   }

TEST
TEST ONE_ARGUMENT
TEST FIRST_ARGUMENT SECOND_ARGUMENT

This simple example demonstrates the use of user-build functions in G7. Within the function called TEST, if-else commands are used to determine how many arguments were passed to TEST.

if( %1 <= B ){
   ic A or B
   }
else if( %1 == C ){
   ic C
   }
else{
   ic D
   }

This example demonstrates the ability of the if-else command to compare strings and then to carry out a set of tasks based on the lexicographical order of the strings.

The following text documents the if-else routines and supporting commands.

break

This command halts execution of a do loop or an add file, but processing of the script continues after breaking out of the do loop or out of the child script.

Examples:

do{
  if( %1 == 2 ){ break }
  else{ ic This is iteration %1 }
  }(1-3)
continue

This command causes the current iteration of a do loop to end and the next iteration to begin.

Examples:

do{
  if( %1 == 2 ){ continue }
  else{ ic This is iteration %1 }
  }(1-3)
if( [comparisons] ){ [...] }
[ else if( [comparisons] ){ [...] } ]
[ else{ [...] } ]
The if command evaluates a series of logical expressions. If the arguments are true, then the following block of code, contained in a set of brackets, is executed. If the argument is false, then else if statements are processed in the same way. If all if and else if arguments are false, then the code following the optional final else command is processed.

Three types of comparisons are legal. First, a number may be compared to a second number. Second, a single number may be given and implicitly compared to zero. Finally, a string may be compared to a second string.

Valid comparisons between two strings, with the comparison based on the lexicographical ordering, or between two numbers are

<:is LESS THAN
<=:is LESS THAN OR EQUAL TO
==:is EQUALS
>=:is GREATER THAN OR EQUAL TO
>:is GREATER THAN
!=:is NOT EQUALS
!:is the logical negation operator. If given before a number, then the result is FALSE if the number is nonzero and the result is TRUE if the number is zero. If given before a set of parentheses, then the result is FALSE if the expression within the parentheses is TRUE, and the result is TRUE if the expression within the parentheses is FALSE.

Additional string comparison tools include:

%strcmpi(<string1>,<string2>)
Compares string1 and string2, without case sensitivity. Value is FALSE if strings match, following the standards of the C libraries..
%strncmp(<string1>,<string2>,<N>)
Compares the first N characters of string1 and string2, with case sensitivity. Value is FALSE if the first N characters of the strings match.
%strncmpi(<string1>,<string2>,<N>)
Compares the first N characters of string1 and string2, without case sensitivity. Value is FALSE if the first N characters of strings match.

Multiple comparisons may be performed, with the following operators joining them

||:Logical OR, which returns TRUE if either the left hand side or the right hand side is TRUE.
&&:Logical AND, which returns TRUE if both the left hand side and the right hand sides are TRUE, and returns FALSE otherwise.

The following keywords and functions particularly are helpful, though many additional features also are useful.

%NARGS:The number of arguments passed to a function, to an add file, or to an fadd file.
%{+ - / *}:simple arithmetic calculations are posible.
%mod(x, y):returns the remainder of ‘x’ divided by ‘y’.
%exists( <series> ):
 returns true if variable x is found in bank a.
%getval(<series>, <date>):
 The value of series in period date is retrieved, either from the workspace or from the specified bank. The value may be compared to a scalar or to a second eval result.

Comparisons may be evaluated as a group by surrounding them with parentheses. Blocks of code following the arguments must be surrounded by brackets {}.

Examples:

if( 1 - 2 < -0.5 )  { ic Subtraction }
if( 2 < 3 && 3 <= 4){ ic Multiple evaluations }
if( 2 < 3 || 3 <= 4){ ic Multiple evaluations with OR }
if( 1 == 1 )        { ic Equality }
else if( !0 )       { ic Else if and negation }
else                { ic Else }
if( D != N ){ ic String inequality comparisons }
if( D <= N ){ ic String less than or equal to }
if( %mod(7,2) == 1 ) { ic Modulo arithmetic }
do{
   if( %1 == 3){
      ic Continuing on Iteration %1
      continue
      }
   if( %1 == 5 ){
      ic Breaking on Iteration 5
      break
      }
   if( %1 > 5 ){ return ERR }
   }( 1-10 )
return [arg]

This command interupts execution of the G7 script. If G7 is in the midst of processing a do loop or an add file, then the arguments determine the next actions of G7. Arguments may be given in upper or lower case.

Arguments: :OK: The default argument. Exits a do loop or add file, but processing continues. :ESC: Processing halts entirely, but no error messages are shown. :ERR: Processing halts entirely, with error messages displayed.

Examples:

return ERR

Previous topic

Command Files, Groups, and Do Loops

Next topic

User-specified Error Handling

This Page