Assignment 3

Ryerson University

HL Loops

There are 3 types of loops in HL, grouped into 2 categories:

Condition Loops

These loops continue executing depending on the Boolean value of a condition that is evaluated at each loop iteration. These loops include while loops, repeat-until loops, do-while loops. HL will only provide one such loop, the while loop.

While Loops

The syntax of a while loop is: WHILE condition DO body OD;

In this type of loop, the condition is always evaluated before the body, and the evaluation of the loop stops when the condition evaluates to false.

Therefore it is possible for the body to never be evaluated if the first evaluation of the condition yields a false value.

Enumeration Loops

These loops are executed for a fixed number of times described either explicitly with a counter or implicitly with a polynomial.

Explicit for loops

The syntax of an explicit loop is: FOR IDENTIFIER FROM expression1 TO expression2 (BY expression3)0 DO body OD

Its semantics are:

Implicit for loops

The syntax of an implicit loop is: FOREACH IDENTIFIER IN expression DO body OD

This loop enumerates through a polynomial as follows:

For example,
    polyn p;
    foreach p in 3 + 5x^3 - 2x do print(p,"\n"); od;
would print:

Enumeration modifiers

HL programmers can alter the default execution of enumeration loops with the stop statement which is meaningless outside of loop bodies. The syntax of this statement is: STOP IDENTIFIER

Here are the details of how this statement works:


// guess what this loop calculates
int i, j;
for i from 1000 to 10000 do
    for j from 2 to i-1 do
        if j*j == i then stop i; fi;	

This page is maintained by Sophie Quigley (cps710@scs.ryerson.ca)
Last modified Monday, 05-Sep-2016 09:05:02 EDT