Sporkl Loops and Conditionals


Two constructs in programming languages that are often used to control program execution flow are loops and conditionals. Loops repeatedly execute blocks of code in an iterative fashion. Conditionals branch code execution depending on "conditions", those being boolean ("true/false") results of expressions.

Below are the details of how to implement these in Sporkl.

Loops

Sporkl supports two types of loops: for loops and while loops. Both execute a block of code repeatedly until a particular condition is no longer met, after which the loop exits and the program resumes execution after the end of the loop's block of code. Loops will also exit from within the block of code when a break statement is encountered.

Loops may be nested, meaning that loops can be contained within other loops.

When a break statement is encountered, the program will exit from the nearest containing loop. When a continue statement is encountered, the program will skip the remainder of the code in the nearest containing loop body and resume execution at the top of the loop, which in the for loop case is the iterate ops, and in the while loop case is the condition evaluation.

for loops

for loops have the following general form:

for ( <list of initialization ops> ; <condition>; <list of iterate ops> )
{
   <code body>
}

<list of initialization ops> is a comma-separated list of zero or more statements that are to be executed before the loop begins. This would typically be where the variables that are iterated in the loop get initialized to a particular value. <condition> is an expression that at the beginning of a loop iteration must evaluate to a non-zero value (true), or be empty (where "true" is implied in this context), for the loop to proceed. When <condition> evaluates to zero (false), the loop exits. <list of iterate ops> is a comma-separated list of zero or more statements that are to be executed at the end of each loop iteration.

Below are some examples of for loops:

/* This loop runs for 10 iterations, then stops when i=10 */
for ( i=0; i<10; ++i )
{
   print("i= " + str(i) + "\n");
}

/* This loop runs for 5 iterations */
for ( i=0, j=0; i<10 && j<10; ++i, j+=2 )
{
   print("i= " + str(i) + "  j=" + str(j) + "\n");
}

/* This loop does the same thing as the above loop... */
i=0;
j=0;
for ( ; i<10 && j<10; )
{
   print("i= " + str(i) + "  j=" + str(j) + "\n");
   
   ++i;
   j += 2;
}

/* ...as does this. */
i=0;
j=0;
for ( ; ; )
{
   if ( !(i<10 && j<10) )
   {
      break;
   }
   
   print("i= " + str(i) + "  j=" + str(j) + "\n");
   
   ++i;
   j += 2;
}

/* This loop runs forever. */
for ( ; ; )
{
   print("Bueller..?\n");
}

while loops

while loops have the following general form:

while ( <condition> )
{
   <code body>
}

<condition> is an expression that must evaluate to a non-zero value (true), or be empty (where "true" is implied in this context), at the beginning of a loop iteration for the loop to proceed. When "condition" evaluates to false, or to zero, the loop exits.

Below are some examples of while loops:

/* This loop runs for 10 iterations, then stops when i=10 */      
i = 0;
while ( i<10 )
{
   print("i= " + str(i) + "\n");
   ++i;
}

/* This loop only runs for 1 iteration */
i = 0;
while ( i==0 )
{
   print("i= " + str(i) + "\n");
   ++i;
}

/* This loop runs forever. */
i = 0;
while ( true )
{
   print("Bueller..?\n");
   ++i;
}

Conditionals

Conditionals control program flow based on the outcome of expressions which return numerical values that are interpreted as boolean (true or false) values. A numerical value is interpreted as true if it is non-zero, and false if it is zero. Also, Sporkl includes the keywords true and false, which represent the numerical values 1 and 0, respectively.

There are two basic types of conditionals in Sporkl: inline conditionals and if statements.

Inline Conditionals

An inline conditional is an expression that returns one of two values, based on whether a condition evaluates to true or false. It has the following form:

<condition> ? <result A> : <result B>

When an inline conditional expression is executed, <condition> is evaluated, and if the result is true, <result A> is returned, otherwise <result B> is returned.

Since the Sporkl compiler must know the returned type at compile time, <result A> and <result B> must have consistent types, otherwise a compiler error will be generated.

if Statements

if statements execute different blocks of code depending on the evaluation of one or more conditional expressions. These have the following general form:

if ( <condition 1> )
{
   [Code block 1]
}
elseif ( <condition 2> )
{
   [Code block 2]
}
....
elseif ( <condition n> )
{
   [Code block n]
}
else
{
   [Code block n+1]
}

First note the "...." in the above - this is notation to indicate that there can be any number of elseif conditions in an if statement. Also, the final else block is optional.

When an if statement is executed, the first condition is evaluated, and if true, the associated code block is executed, then program execution resumes after the end of the entire if statement. If false, the program execution skips to the next condition to be evaluated, and repeates the above process. This continues until a condition evaluates to true, or an else block is present and is reached. If all previous conditions evaluated to false, then the else code block is executed.

No more than one code block will be ever be executed in an if statement. If the if statement has an else block, then exactly one code block will be executed.




( <-- Back to Sporkl documentation home )




sporkforge