Conditional Constructs and Loops
Learn about each of the conditional constructs in the Adams View command language. The constructs are listed in alphabetical order for reference.
BREAK
Use the BREAK command to exit the innermost FOR or WHILE loop immediately and stop execution of the loop.
When Adams View encounters a BREAK command inside a loop, it immediately exits the loop without executing the remaining commands for that iteration.
The BREAK command affects only the innermost FOR or WHILE loop.
Format
BREAK
Example
In this example, Adams View creates markers, named MAR1, MAR2, MAR3, MAR4, and MAR5, unless a marker already exists with one of those names. As soon as it encounters an existing marker, Adams View exits the loop and does not create any more.
variable create variable_name=ip integer_value=0
while condition=(ip < 5)
variable modify variable_name=ip integer_value(eval(ip+1))
if condition=(eval(DB_EXISTS ("MAR"//ip)))
break
end
marker create marker_name=(eval("MAR"//ip)) &
location=(eval(ip-1)),0,0
end
variable delete variable_name=ip
CONTINUE
Use the CONTINUE command to skip commands inside the innermost FOR or WHILE loop and continue with the next iteration of the loop.
When Adams View encounters a CONTINUE command inside of a loop, it skips over the remaining commands of the loop and goes directly to the END of the innermost loop. Adams View tests the loop condition and continues looping if the condition is still valid.
The CONTINUE command affects only the innermost FOR or WHILE loop.
Format
CONTINUE
Example
In this example Adams View creates four markers on the default part: MAR1, MAR2, MAR4, and MAR5. Adams View skips MAR3, because when ip evaluates to 3, Adams View encounters the CONTINUE command and skips to the END of the WHILE loop.
variable create variable_name=ip integer_value=0
while condition=(ip < 5)
variable modify variable_name=ip integer_value=(eval(ip+1))
if condition=(ip == 3)
continue
end
marker create marker_name=(eval("MAR"//ip)) &
location=(eval(ip-1)),0,0
end
variable delete variable_name=ip
The results of the example would be:
Results of CONTINUE Example
Name: | Loc_X: | Loc_Y: | Loc_Z: |
|---|
MAR1 | 0.0 | 0.0 | 0.0 |
MAR2 | 1.0 | 0.0 | 0.0 |
MAR4 | 3.0 | 0.0 | 0.0 |
MAR5 | 4.0 | 0.0 | 0.0 |
IF/ELSEIF/ELSE/END
Use the IF, ELSE, ELSEIF, and END commands to execute a group of commands conditionally. The execution of commands bracketed by IF and END depends on the value of an expression.
You can nest any combination of looping (FOR/END, WHILE/END) and conditional constructs (IF/ELSEIF/ELSE/END).
Note: | As with all Adams View commands, you can use the IF, ELSE, ELSEIF, and END commands on the command line, in macros, and in command files. |
Format
You can use the IF command with or without the ELSE command. A few examples of many variations are shown below.
Tip: | You can have any number of ELSEIF CONDITION commands. |
IF CONDITION=(expression)
...
END
IF CONDITION=(expression)
...
ELSE
...
END
IF CONDITION=(expression)
...
ELSEIF CONDITION=(expression)
...
ELSE

...
END
If the expression evaluates to a non-zero value, Adams View executes the commands following the IF or ELSEIF command up to the ELSE, when present, or the END if you do not use the ELSE. If the expression evaluates to zero and you used ELSE, Adams View executes the commands between the ELSE and the END commands.
The question-mark/colon (?:) operator used in a conditional expression replaces and IF/ELSE command that distinguishes one of two values. The expression consists of three parts, a condition whose truth determines which value is selected, and two expressions for the values.
condition expression ? expression a : expression b
When evaluated, the conditional takes on one of the two values. The expression that comes before the question-mark is interpreted as boolean-valued. If it is true (non-zero), then expression a is used as the value of the conditional, otherwise expression b is used as the value.
For example, consider the commands below:
if condition = (variable_a < variable_b) variable set variable = variable_min real = (EVAL(variable_a)) else variable set variable = variable_min real = (EVAL(variable_b)) end
This can be expressed more concisely by using a ?: conditional operator:
variable set variable = variable_min & real = (EVAL((variable_a < variable_b)? variable_a : variable_b))
Example
In the following example, if the marker MAR1 exists, Adams View modifies its location. If the marker does not exist, Adams View creates it and sets its location.
if condition=(DB_EXISTS ("MAR1"))
marker modify marker=mar1 location=2,0,0
else
marker create marker=mar1 location=2,0,0
end
The next example illustrates how to use ELSEIF to determine the type of object and then perform an operation on the object based on the object's type. The example assumes that an Adams View variable named .mdi.org exists and its type is database object.
! Bodies
variable create variable=object_type string=(eval(DB_TYPE(.MDI.obj)))
if condition=(object_type == "marker")
interface command_builder command="marker modify marker" initial=(.MDI.obj)
elseif condition=(object_type == "point")
interface dialog display dialog=.gui.main_objecttable parameter="Points"
elseif condition=(object_type == "flexible_body")
interface dialog display dialog=.gui.flx_dia_panel parameter=(.MDI.obj)
! Constraints - complex joints
elseif condition=(object_type == "coupler" )
interface dialog display dialog=.gui.coupler_cremod parameter=(.MDI.obj)
elseif condition=(object_type == "gear" )
interface command_builder command="constraint modify complex_joint gear" initial=(.MDI.obj)
! Constraints - Higher Pair contact
elseif condition=(object_type == "curve_curve" )
interface command_builder command="constraint modify higher_pair_contact curve_curve" init=(.MDI.obj)
elseif condition=(object_type == "point_curve" )
interface command_builder command="constraint modify higher_pair_contact point_curve" init=(.MDI.obj)
end
FOR/END
The FOR and END commands allow you to execute a group of commands a fixed set of times. You can use FOR either to perform numeric iteration or to operate on a set of Adams View objects, such as markers or parts. Adams View executes the commands bracketed by the FOR and END for each value of a variable in the specified range or upon the specified set of objects.
You can nest any combination of looping (FOR/END, WHILE/END) and conditional constructs (IF/ELSEIF/ELSE/END).
Format
Using FOR/END to Perform Numeric Iteration
To perform numeric iteration, use this form of FOR/END:
FOR VARIABLE_NAME=var START_VALUE=REAL &
INCREMENT_VALUE=REAL &
END_VALUE=REAL
...
END
Adams View executes the commands between the FOR and END for each value of var, in the range START_VALUE to END_VALUE. At the beginning of the FOR loop, Adams View creates a temporary Adams View variable named var of type REAL. Adams View deletes the variable when the loop terminates. If you use the loop variable in an expression that requires it to persist, Adams View issues a warning message and does not delete the variable at the termination of the loop. If you do not want this behavior, you can use the EVAL function as described in
Examples of Numeric Iteration for FOR/END.
START_VALUE, INCREMENT_VALUE, and END_VALUE can be any valid real expression. INCREMENT_VALUE can be either positive or negative, and defaults to 1.0 if not specified. If INCREMENT_VALUE is positive, Adams View increments the value of var by the increment for each iteration and stops looping when the value of var is greater than END_VALUE. If INCREMENT_VALUE is negative, Adams View decrements var by the increment for each iteration and continues looping until var is less than END_VALUE.
The commands inside the FOR/END loop can use var as they would any other Adams View variable of type REAL.
Examples of Numeric Iteration for FOR/END:
In this example, Adams View creates 10 markers, MAR1 through MAR10, on the default part, and locates them one unit apart on the x-axis of the part's coordinate system.
for variable_name=tempreal start_value=1 end_value=10
marker create marker_name=(eval("MAR" // RTOI(tempreal))) &
location=(eval(tempreal-1)), 0, 0
end
The example demonstrates the use of the EVAL function when you want to assign the instantaneous value of an expression rather than the expression itself. An expression's value changes whenever the value of any variable in it changes. Sometimes you want this behavior; other times you do not. Using EVAL avoids this behavior.
When Adams View applies EVAL to an expression, it obtains the current value of the expression. For example, when you use EVAL with the expression (tempreal-1), Adams View assigns 0.0 to the x component of the location for MAR1, 1.0 to MAR2, and so on. Without EVAL, Adams View assigns the expression, (tempreal-1), to the x component of the location, resulting in all the markers having locations at (9, 0, 0) at the termination of the FOR loop. Another effect is that Adams View does not delete the variable tempreal when the FOR loop terminates, because the locations of the markers still depend on it. The locations change again if you subsequently assign a different value to tempreal.
Using FOR/END To Operate on a Set of Objects
Use the following form of FOR/END to operate on a set of objects (for how to view the list of database object types, see
Database Object Type):
FOR VARIABLE_NAME=var OBJECT_NAMES=objects &
TYPE=database_object_type
...
END
For this type of FOR loop, Adams View creates a temporary Adams View variable named var of type OBJECT and successively assigns the value of each object in the set to the variable. The commands inside the FOR/END pair can use var as they would any other Adams View variable of type OBJECT. Adams View deletes the variable when the loop terminates.
In this example Adams View renumbers the Adams IDs of markers belonging to the part follower, starting at 5000, and incrementing by one for each marker in the set.
variable create variable_name=ip integer_value=5000
for variable_name=the_marker object_names=.fourbar.follower.* type=marker
marker modify marker_name=(eval(the_marker)) adams_id=(eval(ip))
variable modify variable_name=ip integer_value=(eval(ip+1))
end
variable delete variable_name=ip
As in the previous example, you can use the EVAL function to get the instantaneous value of an expression rather than assigning the expression itself.
As shown, you can use wildcards to specify the objects for the OBJECT_NAME parameter. The TYPE parameter applies a filter to the set of objects, in this case, matching only children of the part that are markers.
If you use a more general wildcard, Adams View may execute the command more slowly than if you use a more specific wildcard. For example, if you want all the markers in the model MOD1, use OBJECT_NAME=.MOD1.* type=MARKER instead of OBJECT_NAME=* type=MARKER.
For more sophisticated searching and filtering, see the database functions, such as DB_CHILDREN, in the Design-Time Functions section of the
Adams View Function Builder online help. You also may want to use the miscellaneous function SELECT_OBJECT of the same guide.
WHILE/END
Use the WHILE and END commands to execute a group of commands zero or more times. Adams View executes the commands that WHILE and END bracket repeatedly until the condition associated with the WHILE command is FALSE (zero).
You can nest any combination of looping (FOR/END, WHILE/END) and conditional constructs (IF/ELSE/ELSEIF/END).
Format
The format of the WHILE command is:
WHILE CONDITION=(expression)
...
END
Adams View evaluates the value of expression and executes the commands between the WHILE and the END command if the value is non-zero. Adams View evaluates the expression at the end of the loop and continues looping as long as the value of the expression is non-zero.
Examples
In this example, Adams View creates 10 markers, MAR1 through MAR10, on the default part, and locates them one unit apart on the x-axis of the coordinate system.
variable create variable_name=ip integer_value=0
while condition=(ip < 10)
marker create marker_name=(eval("MAR"//ip+1)) &
location=(eval(ip)),0,0
variable modify variable_name=ip integer_value=(eval(ip+1))
end
variable delete variable_name=ip
You can use the EVAL function to get the value of an expression rather than assigning the expression itself. Use of the EVAL function with loops is described in the
FOR/END.
RETURN
Use the RETURN command to exit a command file or macro and return to the command file or macro that invoked it. Its effect is similar to BREAK when you use it to exit a loop, skipping all remaining commands in the command file or macro (including any cleanup commands you may have at the end of your macro).
If a RETURN is executed within loops (nested to any depth), it exits those loops and performs all required cleanup, just as multiple BREAKs would do. You can have as many RETURN commands in your command files or macros as you want.
Format
RETURN
Example
RETURN is often used as a means for recovering from an error condition or allowing a user to cancel an operation. Below are two examples.
Example 1
In the example, the RETURN command lists information on the contents of the select list, but only if there are objects on the list. If it finds no objects, it returns and issues an error message.
if condition=(DB_COUNT(.SELECT_LIST, "objects_in_group")==0)
mdi gui_utl_alert_box_1 type="Error" text="Select List is empty. Select objects first."
return
end !if
!
info empty
!
list_info group &
group_name = .SELECT_LIST &
brief = on &
write_to_terminal = on
!
Example 2
This example macro determines if a particular file exists and asks the user if it should overwrite the existing file.
variable create variable=$_self.fileName string="file.dat"
if condition=(file_exists($_self.fileName))
if condition=(alert("warning", "Delete existing "//$_self.fileName//"?", "Ok", "Cancel", "", 2) == 2)
variable create variable=$_self.junk &
int=(alert("information", "File "//$_self.fileName//" not destroyed.", "Ok", "", "", 1))
variable delete variable=$_self.*
return
end
variable create variable=$_self.junk &
int=(alert("information", "File "//$_self.fileName//" was destroyed.", "Ok", "", "", 1))
end
! Write the new file.
file text open file=($_self.fileName) open_mode=overwrite
file text close
! Clean up.
variable delete variable=$_self.*
Ternary Conditional Operator
The ternary conditional operator used in a conditional expression replaces an IF/ELSE command that distinguishes between one of two values. The expression consists of three parts: a condition whose truth determines which value is selected, and two expressions for the values.
Format
condition expression ? expression a : expression b
When evaluated, the conditional takes on one of the two values. The expression that comes before the question mark is interpreted as Boolean-valued. If it is true (nonzero), then expression a is used as the value of the conditional, otherwise expression b is used as the value.
Example
For example, consider the commands below:
if condition = (variable_a < variable_b)
variable set variable = variable_min real = (EVAL(variable_a))
else
variable set variable = variable_min real = (EVAL(variable_b))
end
You can express this more concisely using the ternary conditional operator:
variable set variable = variable_min &
real = (EVAL((variable_a < variable_b)? variable_a : variable_b))
Notes: | The ternary conditional operator has lower precedence than all other operators. Any operations performed in sub expressions are performed before the ternary operator is evaluated: x < 10 ? x + 10 : x * 10 is the same as: (x < 10) ? (x + 10) : (x * 10) Likewise: s == "Bob" ? "Hello" : "Goodbye" // ", " // s is evaluated as: (s == "Bob") ? ("Hello") : ("Goodbye" // ", " // s) |