Tutorial - Over Statement

The Over Statement

The form of the Over statement is as follows :-

        over_statement   ::= Over array_expr As identifier Do
                                     statement_block 
                             EndOver;

The array_expr is any expression that yields an array. The identifier is any valid identifier that has not been declared within the current routine. It will act as a reference for the current element. The value will be constant if the array expression yields a constant array.

The statement will go through all the elements of the array and assign them to the identifier. The statement list will then be executed on that value.

Simple Example

Type YieldRec = { Number year, Number yield };
Type YieldArray = YieldRec[];

Program()
	YieldArray results = [ {1970, 14}, {1971, 18} , {1972, 19}, {1973, 16} ];
	Number margin = 30, width = 20, scale = 5;
    Begin

	Over results As entry Do
		Rectangle(width, -entry.yield * scale) => { (entry.year - 1970) * width + margin, 180 };
	EndOver;

    End;
stmt_over_1.grs Output of stmt_over_1.grs stmt_over_1.png

The OBreak statement

As with the other loop constructs there is a break statement that will terminate the loop early.

	Over results As entry Do
		If entry.yield > 100 Then
			Output "Yield too high - aborting processing";
			OBreak;
		EndIf;
		Rectangle(width, -entry.yield * scale) => { (entry.year - 1970) * width + margin, 180 };
	EndOver;

Exercise

In a previous tutorial Project - Tic-Tac-Toe you wrote a simple program to display the result of a game.

Now write a new version of the program using the new constructs you have learned since then. You should use a structure to contain all the information that specifies a move. Create an array type base on the structure for a complete game, declare an array variable initialised with the game from the previous example. Finally draw the entire game with an 'Over' loop.

You can copy the low level drawing routines from the original.

I would also suggest replacing all the individual parameters for the Token shape with a single structure parameter.

My solution in here: Samples/tictactoe_2.grs ( I have not created a link so as not to temp you to peek ).