Control Statements

Control statements create decision-making and looping constructs in a script. OScript language contains the basic control statements found in most programming languages:

if

The if statement is used to decide whether a group of statements should be executed. The basic form of the if statement is:

if expression 1
	...block of code 1...
[ elseif expression 2
	...block of code 2... ]
[ elseif expression n
	...block of code n... ]
[ else
	...block of code-else... ]
end

In an if statement, expression 1 is evaluated. If TRUE,...block of code 1... is executed and execution resumes after the end. If FALSE, execution continues with the elseif clause, if included. The elseif clause is evaluated, and if TRUE, the associated block of code is executed; if FALSE, the next elseif clause is evaluated, and so on. If every elseif expression clause is FALSE, the ...block of code-else...is executed.

The elseif and else clauses are optional. If used, as many elseif clauses as needed can be included.

The following is an example of an OScript if statement:

List	bugList = { 'ant', 'bee', 'ladybug', 'cricket', 'beetle' }
String 	bug

if ( Length( bugList ) == 0 )
	Echo ( 'The bugList is empty.' )
elseif ( Length( bugList ) > 30 )
	Echo ( 'The bugList is has too many entries.' )
else
	for bug in buglist
		Echo ( 'The bug ' + bug + 'is in the list' )
	end
end

switch

The switch statement provides a branching mechanism. The switch statement tests whether an expression matches any of a number of constant values, and branches execution accordingly. The basic form of the switch statement is:

 
switch expression
	case value 1
		...block of code 1...
	end
	case value 2
		...block of code 2...
	end
	[ case value 3, value n
		...block of code n... 
	end ]
	[ default
		...block of code-default...
	end ]
end

In a switch statement, the expression is first evaluated. The resulting value is then compared to the case values. If the expression value matches a case value, the ...block of code... following that case value is executed. If the expression value matches none of the case values, the ...block of code-default... following the default clause, if included, is executed.

The expression value must exactly match the case value for the ...block of code... to be executed-no type coercion is performed.

If there are several possible case values for which the resulting action is the same, all of these values can be placed on the same line (as in value 3 and value n in the syntax above). A default clause is optional. If not included and no case values are matched, no action is taken. The case clauses and the default clause can be listed in any order; by convention default is often last. The values specified for each case must be different.

The following is an example of an OScript switch statement:

File	myFile = File.Open( "C:\DATAFILE", File.ReadMode )
String 	thing = File.Read( myFile )

switch thing
	case "orange", "apple", "pear"
		Echo( thing + " is a fruit." )
	end
		case "celery", "potato", "turnip"
		Echo( thing + " is a vegetable." )
	end
		default
		Echo( "Cannot tell if " + thing + " is a vegetable or fruit." )
	end
end

while

The while statement provides a looping construct that repeatedly executes the statements within the loop while the specified expression evaluates to TRUE. The basic form of the while loop is:

while expression
	...block of code...
end

In a while loop, the expression is evaluated at the top of the loop. If it is TRUE, the ...block of code... inside the loop is then executed. This is repeated until the expression evaluates to FALSE. It is important to either define an expression that will eventually become FALSE or to include a break statement within the loop to avoid the creation of an infinite loop.

The following is an example of an OScript while loop:

String oneLine
String allLines
File myFile = File.Open( "C:\DATAFILE", File.ReadMode )
while !File.EOF( myFile )
	oneLine = File.Read( myFile )
	allLines += oneLine
end

for

In OScript, there are three types of for loops: counted, indexed, and C-style.

Counted for

The counted for statement provides a looping construct that repeatedly executes the statements within the loop a specified number of times. This "number of times" need not be an Integer literal; it can be specified by any expression that evaluates to an Integer.

for variable = initialValue [down]to finalValue [by increment]
	...block of code...
end

In this for loop, the variable is assigned an initial Integer value and tested for equivalence against the finalValue. If initialValue is less than or equal to finalValue, the statements within the loop are executed. The initialValue is then incremented (if to is specified) or decremented (if downto is specified) by the specified increment value (if not included, 1 is assumed). The variable is tested again, and if not out of range, the statements within the loop are executed again. Execution continues in this manner until the variable is out of range.

Note that initialValue, finalValue, and increment are evaluated once before the loop. These values are used for the duration of the loop.

The following is an example of this type of OScript for loop:

for i = 1 to 10
	Echo( "Counting up: ", i )< > end
for i = 10 downto 1
	Echo( "Counting down:", i )
end

Indexed for

The indexed for loop is used with Lists, RecArrays, and Assocs to cycle through all elements in the structure.

for variable in structure 
	...block of code...
end

In this for loop, the variable is assigned the value of the first element in the specified structure. The statements in the loop are then executed. This is repeated until all of the elements of the >structure have been referenced.

If the values in the specified structure are not all of the same data type, you must either not declare the variable (which is not good programming practice) or declare it to be of type Dynamic.

The following is an example of this type of OScript for loop:

Integer i
List numList = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
for i in numList
	Echo( "i = ", i )
end

C-style for Loop

The C-style for loop is similar to the for loop used in the C programming language:

for ( initializer; condition; increment )
	...block of code...
end

In this for loop, the initializer is evaluated before the loop is entered. At the end of the loop, the increment and the condition are evaluated. If the value of the condition is TRUE, the loop continues; if FALSE, the loop terminates. The initializer, condition, and increment must be expressions.

The following is an example of this type of OScript for loop:

//This example will count from 1 to 10
Integer i
for( i = 1; i <= 10; i += 1 )
	Echo( "i=", i )
end

repeat

The repeat statement provides a looping construct that repeatedly executes the statements within the loop while the specified expression evaluates to FALSE. The basic form of the repeat statement is:

repeat
	...block of code...
until expression

The repeat loop differs from the while loop in an important way: structuring a loop as a repeat loop guarantees that the ...block of code... within the loop will execute at least once since the expression is evaluated at the bottom of the loop.

In a repeat loop, the ...block of code... inside the loop is executed and then the expression is evaluated. If the expression is FALSE, the ...block of code... is executed again. Once the expression evaluates to TRUE, the loop is exited. It is important to either define an expression that will eventually evaluate to TRUE or to include a break statement within the loop to avoid the creation of an infinite loop.

The following is an example of an OScript repeat loop:

String fileContents
File inputFile = File.Open( 'temp.dat', File.ReadMode )
if ( IsDefined( inputFile ) )
	repeat
		fileContents += File.Read( inputFile )
	until File.EOF( inputFile )
end

break and breakIf

The break and breakIf statements are used to leave a loop. While break unconditionally exits the loop, breakIf exits the loop if its condition is TRUE.

The following is an example of a while loop containing an OScript break statement:

// script to display first five lines of a file
File theFile = File.Open( 'MYDATA', File.ReadMode )
String line
Integer count = 1
Integer linesFound
while count <= 5
	if File.EOF()
		linesFound = count
		break
	end
	line = File.Read( theFile )
	echo( 'Line ' + Str.String( count ) + ' is: ' + line )
	count += 1
end
File.Close( theFile )

The following is an example of a while loop containing an OScript breakIf statement:

// script to display first five lines of a file
File theFile = File.Open( 'MYDATA', File.ReadMode )
String line
Integer count = 1
while count <= 5
	breakif File.EOF()
	line = File.Read( theFile )
	echo( 'Line ' + Str.String( count ) + ' is: ' + line )
	count += 1
end
File.Close( theFile )

In both examples, the first five lines of a File are read, but if the file has less than five lines (the end of the file is reached), the break statements cause the loop to be exited.

The main difference between the break and the breakIf statements is that break is commonly used if you want to perform additional steps before exiting the loop. In the previous examples, break and breakIf are used to exit the loop if the end of the file is reached before five lines are read. However, in the first example, using break instead of breakIf allows the line counter to be updated before breaking out of the loop.

continue and continueIf

The continue and continueIf statements can be used to skip to the end of the loop and start the next iteration of the loop. While continue unconditionally skips to the end of the loop, continueIf skips to the end of the loop if its condition is TRUE.

The following is an example of a while loop containing an OScript continue statement:

List things = ( "apple", "pear", "", "rock" )
String item
for item in things
	if Length( item ) == 0
		Echo( "Found an item of length zero" )
		continue
	end
	Echo ( "Non-blank item: ", item )
end

The following is an example of a while loop containing an OScript continueIf statement:

List things = ( "apple", "pear", "", "rock" )
String item
for item in things
	continueIf Length( item ) == 0
	Echo ( "Non-blank item: ", item )
end

In both examples, all items are read from the List, but if the length of item is zero, the continue statement causes execution to skip back up to the top of the loop to read the next item.

The main difference in usage of the continue and the continueIf statements is that continue is more commonly used if you want to perform additional steps before restarting the loop. In the above examples, continue and continueIf are used to skip to the top of the loop if the length of item is zero. However, in the first example, using continue instead of continueIf allows the message indicating that a Record has been skipped to be displayed.

goto

The goto statement causes script execution to branch to the specified label. The basic form of a goto statement is:

goto label
	...block of code 1...
label:
	...block of code 2...

When the goto statement is encountered, execution skips over ...block of code 1... and resumes at the label:, executing ...block of code 2....

The following is an example of an OScript goto statement:

List inputList = { "One", "Two", 3333, "Four" }
Dynamic element
for element in inputList
	if Type( element ) != StringType
	goto badElementType
	end
	Echo( "Element: ", element )
end
Echo( "inputList contains only Strings" )
return

//
// Control will pass to this label if a non-string is found
// in inputList.
//
badElementType:
	Echo( "There was a non-string element: ", element )
return

Agoto statement can be used to escape from several nested operations if a condition makes continuation impossible. The liberal use of goto statements may be a sign of poor programming practice. Only rarely can a script not be rewritten in a more structured manner, making the use of goto statements unnecessary.