WHILE - DO
You can use the WHILE - DO statement to set a program to execute statements in a loop while an input is true. A typical use of the WHILE - DO statement is to set a program to define values or perform methods on a resultset of an SQL Query when a particular condition is in place.
NOTE: The WHILE - DO expression is evaluated before the statements are executed. This means that if the expression in the WHILE - DO definition is false, the program will not begin to execute the statements in the program.
You can define a WHILE - DO statement by using this format:
- WHILE <Condition expression> DO
- <Statements>;
- END_WHILE;
where:
<Condition expression> is the expression that defines the value that has to be true before the program will check the statements.
<Statements> are the values and methods that are set as a result of the <Condition expression> being true.
Example:
- TYPE
- AlarmConds : DATABASE_OBJECT (CDBPoint)
- AlarmDesc: STRING;
- Accept: METHOD;
- END_DATABASE_OBJECT;
- END_TYPE
- PROGRAM Name
- VAR
- PointList AT %S(Select Id, AlarmDesc FROM CDBPoint): RESULTSET OF AlarmConds;
- END_VAR
- WHILE PointList.Value.AlarmDesc='Unacknowledged Cleared' DO
- PointList.Value.Accept();
- PointList.Next();
- END_WHILE;
- VAR
- END_PROGRAM;
In this example, we use a DATABASE_OBJECT structure to group a type of value and a method. In this case, the DATABASE_OBJECT structure references the CDBPoint class and includes the AlarmDesc value (which is a STRING) and the Accept method (that acknowledges alarms).
In the VAR list, the variable defines the SQL Query that is used to obtain data from the CDBPoint class. The name of the SQL Query is defined as PointList and it is used to extract the Id and AlarmDesc values from the CDBPoint class (the Query returns the values for the RESULTSET of the AlarmConds DATABASE_OBJECT structure).
In the Logic of the program, the WHILE - DO statement is used to instruct the program to acknowledge any point alarms that are Unacknowledged Cleared:
- WHILE PointList.Value.AlarmDesc= 'Unacknowledged Cleared' DO
- PointList.Value.Accept();
- PointList.Next();
- END_WHILE;
This defines that as long as the current record in the SQL Query result set has an AlarmDesc value of 'Unacknowledged Cleared', the Accept method has to be used. The Accept method is applied so that the 'Unacknowledged Cleared' alarm is acknowledged for the point to which the current record relates.
The PointList.Next() instruction sets the program to proceed to the next record in the result set once the current record has had any 'Unacknowledged Cleared' alarms acknowledged.
The Logic program continues to step through each of the records in the record set, acknowledging any 'Unacknowledged Cleared' alarms until it reaches the end of the record set. When the records have been checked, the program stops executing.