A statement can be a declaration, an assignment, a program construct (such as a Break statement or a conditional loop), or a subroutine call.
This section discusses:
PeopleCode statements are generally terminated with a semicolon. The PeopleCode language accepts semicolons even if they are not required, such as after the last statement completed within an If statement. This functionality enables you to consistently add semicolons after each statement.
Extra spaces are ignored. They are removed by the PeopleCode Editor when you save the code.
The assignment statement is the most basic type of statement in PeopleCode. It consists of an equal sign with a variable name on the left and an expression on the right:
variableName = expression;
The expression on the right is evaluated, and the result is placed in the variable named on the left. Depending on the data types involved, the assignment is passed either by value or by reference.
Assignment by Value
In most types of assignments, the result of the right-hand expression is assigned to the variable as a newly created value, in the variable's own allocated memory area. Subsequent changes to the value of that variable have no effect on any other data.
Assignment by Reference
When both sides of an assignment statement are object variables, the result of the assignment is not to create a copy of the object in a unique memory location and assign it to the variable. Instead, the variable points to the object's memory location. Additional variables can point to the same object location.
For example, both &AN and &AN2 are arrays of type Number. Assigning &AN2 to &AN does not assign a copy of &AN2 to &AN. Both array objects point to the same information in memory.
Local array of number &AN, &AN2; Local number &NUM; &AN = CreateArray(100, 200, 300); &AN2 = &AN; &NUM = &AN;
In the code example, &AN2 and &AN point to the same object: an array of three numbers. If you were to change the value of &AN to 500 and then reference the value of &AN2, you would get 500, not 300. On the other hand, assigning &NUM to the first element in &AN (100) is not an object assignment. It is an assignment by value. If you changed &AN to 500, then &NUM remains 200.
Note: In PeopleCode, the equal sign can function as either an assignment operator or a comparison operator, depending on context.
PeopleCode language constructs include:
Branching structures: If and Evaluate.
Loops and conditional loops: For, Repeat, and While.
Break, Continue, and Exit statements loop control and terminating programs.
The Return statement for returning from functions.
Variable and function declaration statements: Global, Local, and Component for variables, and Declare Function for functions.
The Function statement for defining functions.
Class definition statements.
Try, Catch, and Throw statements for error handling.
Functions as Subroutines
PeopleCode, like C, does not have subroutines as we generally refer to them. PeopleCode subroutines are the subset of PeopleCode functions only that are defined to return no value or to return a value optionally. Calling a subroutine is the same as calling a function with no return value:
Branching statements control program flow based on evaluation of conditional expressions.
If, Then, and Else statements
The syntax of If, Then, and Else statements is:
If condition Then [statement_list_1;] [Else [statement_list_2;]] End-if;
This statement evaluates the Boolean expression condition. If condition is True, then the If statement executes the statements in statement_list_1. If condition is False, then the program executes the statements in the Else clause; if there is no Else clause, the program continues to the next statement.
Use the Evaluate statement to check multiple conditions. Its syntax is:
Evaluate left_term When [relop_1] right_term_1 [statement_list;] [When [relop_n] right_term_n [statement_list;]] [When-other [statement_list;]] End-Evaluate;
The Evaluate statement takes an expression, left_term, and compares it to compatible expressions (right_term) using the relational operators (relop) in a sequence of When clauses. If relop is omitted, then the equal sign is assumed. If the result of the comparison is True, the program executes the statements in the When clause, and then moves on to evaluate the comparison in the following When clause. The program executes the statements in all of the When clauses for which the comparison evaluates to True. If none of the When comparisons evaluates to True, the program executes the statement in the When-other clause, if one is provided. For example, the following Evaluate statement executes only the first When clause. &USE_FREQUENCY in the following example can only have one of three string values:
evaluate &USE_FREQUENCY when = "never" PROD_USE_FREQ = 0; when = "sometimes" PROD_USE_FREQ = 1; when = "frequently" PROD_USE_FREQ = 2; when-other Error "Unexpected value assigned to &USE_FREQUENCY." end-evaluate;
To end the Evaluate statement after the execution of a When clause, you can add a Break statement at the end of the clause, as in the following example:
evaluate &USE_FREQUENCY when = "never" PROD_USE_FREQ = 0; Break; when = "sometimes" PROD_USE_FREQ = 1; Break; when = "frequently" PROD_USE_FREQ = 2; Break; when-other Error "Unexpected value assigned to &USE_FREQUENCY." end-evaluate;
In rare cases, you may want to make it possible for more than one When clause to execute, as shown in the following example:
evaluate &PURCHASE_AMT when >= 100000 BASE_DISCOUNT = "Y"; when >= 250000 SPECIAL_SERVICES = "Y"; when >= 1000000 MUST_GROVEL = "Y"; end-evaluate;
The For statement repeats a sequence of statements a specified number of times. Its syntax is:
For count = expression1 to expression2 [Step i] statement_list; End-For;
The For statement initializes the value of count to expression1, and then increments count by i each time after it executes the statements in statement_list. The program continues in this loop until count is equal to expression2. If the Step clause is omitted, then i equals one. To count backwards from a higher value to a lower value, use a negative value for i. You can exit a For loop using a Break statement.
The following example demonstrates the For statement:
&MAX = 10; for &COUNT = 1 to &MAX; WinMessage("Executing statement list, count = " | &COUNT); end-for;
Conditional loops, Repeat and While, repeat a sequence of statements, evaluating a conditional expression each time through the loop. The loop terminates when the condition evaluates to True. You can exit from a conditional loop using a Break statement. If the Break statement is in a loop embedded in another loop, the break applies only to the inside loop.
The syntax of the Repeat statement is:
Repeat statement_list; Until logical_expression;
The Repeat statement executes the statements in statement_list once, and then evaluates logical_expression. If logical_expression is False, the sequence of statements is repeated until logical_expression is True.
The syntax of the While statement is:
While logical_expression statement_list; End-While;
The While statement evaluates logical_expression before executing the statements in statement_list. It continues to repeat the sequence of statements until logical_expression evaluates to False.