160 likes | 231 Views
SECTION 10 CONDITIONAL CONSTRUCTS AND LOOPS. CONDITIONAL CONSTRUCTS AND LOOPS. What’s in this section: FOR/END Loop (Numeric Iteration) FOR/END Loop (Object-Based) WHILE/END IF/THEN/ELSE. FOR/END LOOP (NUMERIC ITERATION). Overview
E N D
SECTION 10 CONDITIONAL CONSTRUCTS AND LOOPS
CONDITIONAL CONSTRUCTS AND LOOPS • What’s in this section: • FOR/END Loop (Numeric Iteration) • FOR/END Loop (Object-Based) • WHILE/END • IF/THEN/ELSE
FOR/END LOOP (NUMERIC ITERATION) • Overview • The FOR and END commands allow you to execute a group of commands a fixed set of times. This example performs numeric iterations. ADAMS/View executes the commands bracketed by the FOR and END for each value of a variable in the specified range. • You can nest any combination of looping (FOR/END, WHILE/END) and conditional constructs (IF/ELSEIF/ELSE/END).
FOR/END LOOP (NUMERIC ITERATION) (CONT.) • Syntax 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 an ADAMS/View variable of type REAL named var. 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 to happen, you can use the EVAL function.
FOR/END LOOP (NUMERIC ITERATION) (CONT.) • 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.
FOR/END LOOP (NUMERIC ITERATION) (CONT.) • Example • 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 reference frame. 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
FOR/END LOOP (OBJECT-BASED) • Overview • The FOR and END commands allow you to execute a group of commands a fixed set of times. This example operates on a set of ADAMS/View objects, such as markers or parts. ADAMS/View executes the commands bracketed by the FOR and END on the specified set of objects. • You can nest any combination of looping (FOR/END, WHILE/END) and conditional constructs (IF/ELSEIF/ELSE/END).
FOR/END LOOP (OBJECT-BASED) (CONT.) • Syntax FOR VARIABLE_NAME=var OBJECT_NAMES=objects & TYPE=database_object_type ... END • For this type of FOR loop, ADAMS/View creates an ADAMS/View variable 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.
FOR/END LOOP (OBJECT-BASED) (CONT.) • Example • 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
WHILE/END • Overview • 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/ELSEIF/ELSE/END). • Syntax WHILE CONDITION=(expression)...END
WHILE/END (CONT.) • Example • 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 local part reference frame (LPRF). 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
IF/THEN/ELSE • Overview • Use IF/ELSE to execute a group of commands conditionally. The execution of commands bracketed by IF and END depend on the value of an expression.
IF/THEN/ELSE (CONT.) • Syntax IF CONDITION=(expression) ... END IF CONDITION=(expression) ... ELSE ... END IF CONDITION=(expression) ... ELSEIF ... ELSE ... END Helpful operators for expressions: != not equal to && logical AND || logical OR == equal to < less than For information on these and other operators, see the Expression Language Reference in the ADAMS/View Function Builder online help.
IF/THEN/ELSE (CONT.) • 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 elsemarker create marker=mar1 location=2,0,0 end