IBM Tivoli Monitoring, Version 6.3

Flow control statements

The script interpreter processes statements sequentially starting with the first statement of the program block. This sequential flow can be controlled with conditional statements that perform branching and iteration. Branching is controlled with an IF statement that directs execution along a given path. Iteration is controlled with loop statements that execute one or more statements repeatedly. Recursion is the process of a function calling itself either directly or indirectly through a second function.

Control statements are:

IF/THEN/ELSE, WHILE/BREAK/CONTINUE, RETURN

IF statement
The IF statement allows conditional execution based on the evaluation of an expression. IF statements can be nested.
Syntax:
IF (condition) true-statements [ ELSE false-statements] ;
Table 1. If Statement
Part Description
IF The keyword to begin the control structure.
Condition A numeric or string expression that evaluates true (one) or false (zero).
True-statements Statement(s) performed when the condition is true.
ELSE (Optional) Keyword to begin the False control flow.
False-statements Statement(s) performed if the condition is false.
Multiple statements are permitted as part of the true-statements or false-statements. If multiple statements are to be executed, they must appear within a statement block embedded within braces { }. Example:
If (userid != "") setField(userid,1,5); 
If (userid != "") setField (userid,1,5) 
else 
errMsg("Userid must be specified"); 
If ( op == 5 ) 
{ setField(userid,1,5);   
sendKey("Enter"); 
} 
else  
if (op == 3)  
setField(pswd,2,5); 
else 
msgBox("unknown operation:); 
if (prompt("enter Name") == "") errMsg("Name must be specified");
For statement
The For statement allows iterative execution based on the evaluation of an expression. An initializer statement can be executed to establish initial conditions. A conditional expression is used to control the number of times the loop is executed. An update statement is issued each iteration through the loop.
Syntax:
FOR (initial-statement ; condition ; update-statement ) loop-statements ;
Table 2. For Statement
Part Description
For The keyword to begin the loop control structure.
Initial-statement A statement to be executed prior to the loop.
Condition A numeric or string expression that evaluates true (one) or false (zero). A function that returns an integer result. The loop continues to execute while this condition is true.
Update-statement A statement that will be executed at the end of each iteration.
Loop-statements Statement(s) performed when the condition is true.
Multiple statements are permitted as part of the loop-statements. If multiple statements are to be executed, they must appear within a statement block embedded in braces { }. Example:
For ( i=0; i<3; i=I+1) waitforscreen(1000); 
For ( i=0; i<3; i=i+1) 
{    
sendString(userid);    
if (waitforstring ("Logon complete") > 0)    
{    
success = true;    
break;;    
} 
}
Return statement
Use the Return statement to return from a function or to end the main program block. Program execution resumes at the statement following the point at which a function was called, or exits the script if it is in the main program block. An optional value can be associated with the return statement.

A RETURN statement that is not within a function definition terminates the script.

Syntax:
RETURN [return-value];
Table 3. Return Statement
Part Description
RETURN The keyword to pass control to the calling statements.
return-value An optional value to be returned to the caller. This can be a constant value, literal, or expression.
Example:
..... 
return 0;
While statement
The While statement executes a series of statements in a loop for as long as a condition is true.
Syntax:
WHILE (expression) statements;
Table 4. While Statement
Part Description
While The keyword to begin the control structure.
Condition A numeric or string expression that evaluates true (one) or false (zero).
statements Statement(s) performed while the condition true.
Example:
cnt = 1;   
WHILE (cnt < 3)  
{   
a = a+1;  
cnt = cnt + 1; 
} 
// this will execute the loop 3 times
Break statement
The Break statement causes control to exit a While loop regardless of the condition controlling the loop.
Syntax:
BREAK;
Example:
cnt= 1; 
WHILE (cnt < 3)  
{   
userid = prompt("Enter Userid");   
if (logon(userid) == 1) break;   
cnt = cnt + 1; 
}
Continue statement
The Continue statement causes the execution of the While loop to resume at the top, without executing the rest of the statements in the block.
Syntax:
Continue;
Example:
cnt= 1;      
WHILE (cnt < 3)       
{        
userid = prompt("Enter Userid");        
if (logon(prompt("Enter Userid")) != 1)        
{        
msgBox("Invalid Logon id");        
continue;        
};        
// do other logic....        
}


Feedback