While statements

You use the while statement to loop over a set of instructions until a certain condition is met.

You can use the while statement to repeat a set of operations until a specified condition is true. The while statement in the Impact Policy Language is the same as the one used in programming languages like C, C++, and Java.

The syntax for the while statement is the while keyword followed by a Boolean expression enclosed in parentheses. This expression is followed by a block of statements enclosed in curly braces.

while (condition) { statements }

where condition is a boolean expression and statements is a group of one or more statements. For example:

I = 10;
while(I > 0) {
    Log("The value of I is: " + I);
    I = I - 1;
}

When the while keyword is encountered in a policy, the Boolean expression is evaluated to see if it is true. If the expression is true, the statements in the following block are executed. After the statements are executed, Netcool®/Impact again tests the expression and continues executing the statement block repeatedly until the condition is false.

The most common way to use the while statement is to construct a loop that is executed a certain number of times depending on other factors in a policy. To use the while statement in this way, you use an integer variable as a counter. You set the value of the counter before the while loop begins and decrement it inside the loop. The While statement tests the value of the counter each time the loop is executed and exits when the value of the counter is zero.

The following example shows a simple use of the while statement:

Counter = 10;
					
while (Counter > 0) {
    Log("The value of Counter is " + Counter);
    Counter = Counter - 1;
}

Here, you assign the value of 10 to a variable named Counter. In the while statement, the policy tests the value of Counter to see if it is greater than zero. If Counter is greater than zero, the statements in the block that follows is executed. The final statement in the block decrements the value of Counter by one. The While loop in this example executes 10 times before exiting.

When you run this example, it prints the following message to the policy log:

The value of Counter is 10
The value of Counter is 9
The value of Counter is 8
The value of Counter is 7
The value of Counter is 6
The value of Counter is 5
The value of Counter is 4
The value of Counter is 3
The value of Counter is 2
The value of Counter is 1

The following example shows how to use the While statement to iterate through an array. You often use this technique when you handle data items retrieved from a data source.

MyArray = {"One", "Two", "Three", "Four"};
					
Counter = Length(MyArray);
					
while (Counter > 0) {
	Index = Counter - 1;
	Log(MyArray[Index]);
	Counter = Counter - 1;
}

Here, you set the value of Counter to the number of elements in the array. The While statement loops through the statement block once for each array element. You set the Index variable to the value of the Counter minus one. This is because arrays in IPL are zero-based. This means that the index value of the first element is 0, rather than 1.

When you run this example, it prints the following message to the policy log:

Four
Three
Two
One

In these examples, when you use this technique to iterate through the elements in an array, you access the elements in reverse order. To avoid doing this, you can increment the counter variable instead of decrementing it in the loop. This requires you to test whether the counter is less than the number of elements in the array inside the While statement.

The following example shows how to loop through an array while incrementing the value of the counter variable.

MyArray = {"One", "Two", "Three", "Four"};
					
ArrayLength = Length(MyArray);
Counter = 0;
					
while (Counter < ArrayLength) {
    Log(MyArray[Counter]);
    Counter = Counter + 1;
}

When you run this policy, it prints the following message to the policy log:

One
Two
Three
Four
Note: There is sometimes a risk of the boolean condition of a WHILE statement not being met; this can cause an infinitely looping policy which then can result in an out of memory problem. To prevent this, a maxloopsize property can be added to NCI_server.props. For example:

impact.policy.maxloopsize=50

If a WHILE loop in an IPL policy loops more times than specified by the maxloopsize property, the policy will break out of the WHILE loop and the following error will be generated in the impactserver.log file:

ERROR [IPLImpactActionInterpreterVisitor] The policy: myWHILELoop has looped more than 50 times!