-
Notifications
You must be signed in to change notification settings - Fork 2
control
You can place control structures inside other control structures (such as an if-else statement inside of a for loop). These are said to be nested. For example:
list Days = { "Mon", "Tue", "Wed", "Thu",
"Fri", "Sat", "Sun" };
/* Method to find index of day in Days */
int dayIndex ( str day ) {
dayIndex = -1; /* Initialize to -1 (not found) */
for ( int i = 0; i < count( Days ); i++ ) {
if ( day == Days[i] ) {
dayIndex = i; /* found it! */
}
}
return;
}
_
Control structures in HS can be nested to any level that you want. Methods cannot be nested.
Sometimes it is necessary or more efficient to exit a loop before the termination condition is satisfied. If the termination condition is omitted, then an explicit loop termination statement is required or the loop will iterate forever. The most common way to exit a loop is with the break statement (You could also use a_goto_ statement, or a return statement if the loop is in a method, or even an exit statement.)
To skip an iteration, the continue statement can be used.
The break statement has the form:
break ;
_
It terminates the immediate enclosing loop. Control passes to the statement following the terminated loop.
In the above example, it is not necessary to continue executing the loop once the day index is found. Thus, the break statement can be used as follows:
int dayIndex ( str day ) {
dayIndex = -1;
for ( int i = 0; i < count( Days ); i++ ) {
if ( day == Days[i] ) {
dayIndex = i; /* found it! */
break; /* exit the for loop */
}
}
return;
}
_
The continue statement has the form:
continue ;
_
The continue statement immediately passes control to the end of the immediate enclosing loop.
The goto statement has the form:
identifier : statement
**goto** identifier ;
_
The goto statement transfers control unconditionally to the statement after the identifier label. Note that the label statement must precede the goto statement. This is because HS parses and executes statements serially, so the identifier of the label statement must have been encountered before the goto statement is executed.
The exception is if the goto statement appears in a method. Because methods are not executed until they are called, the target label statement for the goto statement can be defined after the method is defined, but must occur before the method is called. Thus, a goto statement could be used in the example above as follows:
int dayIndex ( str day ) {
dayIndex = -1;
for ( int i = 0; i < count( Days ); i++ ) {
if ( day == Days[i] ) {
dayIndex = i; /* found it! */
goto LABEL; /* jump to the LABEL statement below */
}
}
return;
}
LABEL: { /* do something */ }
int index = dayIndex ( "Wed" );
_
If a label is not a valid identifier, the STATUS variable is set to the error code %SYNTAX. If the label is not defined in the program, the STATUS variable is set to the error code %BRANCH. A handler declared by the on_error statement can trap these errors.
A label statement has the form:
identifier : statement
_
A label can precede any statement, statement block, or method. Labels can be used as the targets of goto and handler statements.
The usual method for exiting a method is with the return statement. The goto statement can also be used, as illustrated in the example above.
The return statement has the form:
return ;
**return** ( expression ) ;
_
The return statement causes a return from a method that was called from a program statement, or was invoked by an event or query message. An optional expression argument specifies the value returned by the method. Thus, our example above could have been written with the return statement as follows:
int dayIndex ( str day ) {
for ( int i = 0; i < count( Days ); i++ ) {
if ( day == Days[i] ) {
return ( i ); /* found it, so return the index */
}
}
return ( -1 ); /* return -1, not found */
}`
_