PCL and Customization > The PATRAN Command Language (PCL) Introduction > Control Statements
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX''">XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX''">   
Control Statements
Control statements are used to transfer control to another section of the program. The following syntax conventions are observed in the following examples:
REQUIRED KEYWORDS	All required keywords are identified in bold Courier font.
entry	Items in plain Courier font are descriptions of the expected entry.
[label]	Items enclosed in brackets are optional.
Branching
PCL is a block structured language. Language elements that control branching and skipping are the IF THEN ELSE, SWITCH CASE, BREAK and CONTINUE statements.
Break and Continue
The BREAK and CONTINUE statements are only allowed within the body of FOR, WHILE, REPEAT, SWITCH and LIST statements. In addition, if the optional label field is given, the label must match one of the above block structures the statement is within. The CONTINUE statement causes transfer to the END statement processing for the block structure indicated by the label field or the most current block structure if no label is provided. This essentially causes the loop to repeat. The BREAK statement is similar except that it transfers control past the END statement, thereby terminating execution of the loop. The format of the two statements is as follows:
BREAK [ label ]
CONTINUE [ label ]
Examples:
CONTINUE active_set_loop
 
BREAK
Simple If Then
The simple IF statement evaluates the logical expression in the clause. If the result is TRUE, the single statement immediately following the THEN keyword is executed. If the result is FALSE, then nothing happens. The IF statement structure is as follows:
IF( logical_expression ) THEN statement
If Then Else
The IF statement evaluates the expression in the clause. If the result is TRUE, the group of statements immediately following is executed and then control skips to the matching END IF clause. If the result is FALSE, then if there is an ELSE IF clause, it is evaluated and the preceding logic is repeated. If all results evaluate to FALSE, then the statements following the ELSE are executed. Multiple ELSE IF clauses are allowed. The IF statement structure is as follows:
IF( logical_expression ) THEN
		statements...
ELSE IF( logical_expression ) THEN
		statements ...
ELSE
		statements ...
END IF
The program statements within a conditional structure are normally indented to make it easier to read. PCL and Patran ignore all leading blanks in a line.
Examples:
In the following example of the IF THEN statement, a patch model is being adaptively meshed based on the previously evaluated strain energy density for the region defined by the patches in patch_list:
	IF ( strain_energy > max_threshold ) THEN
 
		el_length = el_length / 2.0
		do_my_mesh( patch_list, “QUAD”, I,el_length )
 
	ELSE IF ( strain_energy < min_threshold) THEN
	
		el_length = 2.0 * el_length
		do_my_mesh( patch_list, “QUAD”, I,el_length )
 
	ELSE
 
		BREAK adapting /* Break out of loop called adapting */
 
	END IF
Switch and Case
The SWITCH statement starts by evaluating the expression in the clause. It then scans for each CASE statement in turn. Upon reaching the CASE statement, each expression in the CASE is evaluated. If there is an equality match of the CASE expression result and the SWITCH expression result, the statements up to the next CASE or DEFAULT are executed, and then control passes to the statement after the END SWITCH. If the DEFAULT is reached with no CASE expressions matching, then the statements following the DEFAULT will be executed. The DEFAULT clause is optional. See the Break and Continue, 21 statement for a description of the SWITCH label. The SWITCH statement structure is as follows:
SWITCH(expression) [ label ]
	CASE(expression1,expression2,...)
		statements ...
	CASE(expression1,expression2,...)
		statements ...
	DEFAULT
		statements ...
END SWITCH
As an example of using the SWITCH statement, a PCL function is used to interactively construct the element property cards based on the element configuration code. The function UI_READ_REAL prompts the user with the argument string and returns the real value input from the keyboard.
	SWITCH (el_config) 
		CASE (2)	
			midm = UI_READ_REAL(“ENTER MEMBRANE MATERIAL ID:”)
		CASE (3)
			mids = UI_READ_REAL(“ENTER SHELL MATERIAL ID:”)
		CASE (mconfig)
			mass = UI_READ_REAL(“ENTER TOTAL MASS:”)
		CASE (sconfig)
			spring = UI_READ_REAL(“ENTER SPRING CONSTANT:”)
		DEFAULT
			WRITE_LINE(“WARNING: ELEMENT CONFIG”, el_config,“UNDEFINED”)
	END SWITCH
Looping
A loop is a repeated execution of a particular segment or group of statements. Loops include initialization, incrementation, execution and test. Loops may be nested within one another. Block structures used for looping in PCL are WHILE, REPEAT, LIST and FOR statements.
For
The FOR statement begins by evaluating the first numeric expression and assigning it to the variable. Next, the second expression is evaluated and saved as the TO result. The third expression is evaluated and saved as the BY result. If the BY result is zero, an error occurs. If the BY result is positive and the variable value is greater than the TO result, control passes to the statement following the matching END FOR. If the BY result is negative and the variable value is less than the TO result, control also passes to the statement following the matching END FOR. Otherwise the statements in the body are executed. When the END FOR is reached, the variable is incremented by the BY result. The preceding logic is then repeated starting at the point after the expressions were evaluated. Also see the description of the Break and Continue, 21 statements. The FOR statement structure is as follows:
FOR(variable=numeric_expr.TO numeric_expr. [ BY numeric_expr. ]) [ label ]
	statements...
END FOR
While
The WHILE statement evaluates the expression in the clause. If the result is FALSE, control passes to the point after the matching END WHILE. Otherwise, the statements are executed and the preceding logic is repeated. Also see the description of the Break and Continue (p. 2‑21) statements. The WHILE statement structure is as follows:
WHILE( logical_expression ) [ label ]
	statements...
END WHILE
The program statements within a loop structure are normally indented to make it easier to read.
The following is an example of the use of WHILE and FOR statements. A text file containing node displacements in FORTRAN format 6E16.9 is read and stored in array “node_disp” with library utility function TEXT_READ. The file “fid” has previously been opened with a call to the library function TEXT_OPEN. The integer function TEXT_READ returns the value 0 for a successful read and non-zero otherwise.
		count = 0
		WHILE ( TEXT_READ ( fid, “%OF%%6E16.9%”, O, node_dis ( count +1, 1:6)) == 0)
			count += 1
			IF ( count > 10000) THEN
				WRITE_LINE(“* DISCONTINUED READING FILE AT” // @
					“10,000 RECORDS”)
				BREAK  
			END IF
		END WHILE
Repeat
The REPEAT statement starts by executing the statements up to the matching UNTIL clause. Then the expression in the clause is evaluated. If the result is FALSE, the preceding logic is repeated. Otherwise, execution continues with the statement after the UNTIL. Also see the description of the Break and Continue, 21 statements. The REPEAT statement structure is as follows:
REPEAT [ label ]
	statements ...
UNTIL( logical_expression )
As an example of the REPEAT structure, the user is requested to input a grid ID. If an invalid ID is entered, the user is prompted until a valid ID is entered.
	REPEAT
		grid_id = UI_READ_INTEGER( “ INPUT GRID ID ”)
	UNTIL ( VALID_GID(grid_id) )
List
The LIST statement executes the statements in the body for each expression in the expression list. Each expression is evaluated in turn and assigned to the variable. For each assignment done, the statements in the body are executed. Also see the description of the Break and Continue, 21 statements. The LIST statement structure is as follows:
LIST( variable=expression1 [,expression2, ...] ) [ label ]
	statements ...
END LIST
In the following example of a LIST statement, the variable “diameter” is used to create both the inside and outside surface of a sphere. The variables x0, y0 and z0 are the global coordinates of the sphere’s origin.
LIST ( diameter = inside_diameter, inside_diameter + thickness )
!GR,#,,`x0`,`y0 - diameter`,`z0`
!LIN,2#,ARC,`x0`/`y0`/`z0`/`x0`/`y0`/`z0 + 1`/180,#
!PA,8#,ARC,`x0`/`y0`/`z0`/`x0`/`y0 + 1`/`z0`/360,2#
	END LIST
!	HP,8#,2P,,`maxpid + 1`T`maxpid + 8`,`maxpid + 9`T`maxpid + 16`