MCL Scripting: Control Structures

Introduction

Control structures in the MCL programming language are used to make decisions and control the flow of a program’s execution. The MCL language features a handful of useful control structures, so of which may be familiar to users of other programming language. For each control structure the format is listed, explained and an example provided to illustrate it’s use.

Control Structures

Do…While

Format:

do

statements

while condition

The do while loop repeats a block of statements while a user-defined condition is true. The block of code is run and then the condition is evaluated.
count VAR byte
byte = 0
do

puts 0,[count,13]
count = count + 1

while(count < 10)

While…Wend

Format:

while condition

statements

wend

The while..wend loop functions like the do…while loop in that it repeats a block of code while a condition is true, however in this form of looping the condition is evaluated first and then the code is executed.
index VAR byte
index = 0

while index < 10

puts 0,[index, 13]
index = index + 1

wend

Repeat…Until

Format:

repeat

statements

until condition

The repeat…until control structure repeats a block of code while the condition is false. This is the opposite of the do…while and while..wend structures which repeat a block of code while the condition is true.
count VAR byte
count = 0

repeat

puts 0, [count, 13]
count = count + 1

until count > 10

Ff…Then…Elseif…Else…Endif

Format:

single if

if expression then

statements

endif

if…else

if expression then

statements

else

statements

endif

if…elseif

if expression then

statements

elseif expression

statements

endif

if…elseif…else

if expression then

statements

elseif expression

statements

else

statements

endif

If statements allows for decisions to be made about the flow and execution of code. The if is paired with an expression, if the expression evaluates to true the code following it is executed, if the expression evaluates to false the code is not executed. The else statement can follow a block of code paired with an if. If the if’s conditional evaluated to false the code following the else will be executed. Elseif acts as a follow on to a failed if evaluation and allows another expression to be evaluated. The endif keyword ends a block using the if keyword.
count VAR byte
count = 5

if count < 10 then

puts 0,[“The count variable is less than ten”, 13] ; the string is printed because count is less than ten

endif

if count > 10

puts 0,[“The count variable is greater than 10”, 13] ; this string is not printed because count is not greater than 10

endif

count VAR byte
count = 5

if count > 10 then

puts 0,[“The count variable is greater than 10”, 13] ; the string is not printed because count is not greater than 10

else

puts 0,[“The count variable is less than 10”, 13] ; the string is printed because the the previous evaluation failed

endif

count VAR bytes
count = 5

if count > 10 then

puts 0,[“Count is greater than 10”, 13] ; evaluates to false, string not printed

elseif count = 5

puts 0,[“Count is equal to 5”, 13] ; evaluates to true, string is printed

endif

count VAR bytes
count = 5

if count > 10 then

puts 0,[“Count greater than 10”, 13] ; evaluates to false, string not printed

ifelse count > 8

puts 0,[“Count greater than 10”, 13] ; evaluates to false, string not printed

else

puts 0,[“Count less than 10 and less than 8”, 13] ; string prints because previous evaluations were false

endif

For…Next

Format:

for countValue = startValue to finishValue {step increment}

statements

next

The for…next loop is a counter-based loop. It uses a variable, labeled countValue in the format section above, to hold a value that increments or decrements from the startValue to the finishValue. For each iteration of the loop the code enclosed by the for and next keywords is executed. The loop exits when the countValue is no longer in the range specified by the startValue and the finishValue.

count VAR byte
for count = 0 to 9

puts 0,[count,13] ; on each iteration of the loop the value of count is printed to the terminal

next

count VAR byte
for count = 1to 20 step 5

puts 0,[count, 13] ; print the values from 1 to 20 in increments of 5

next

Label

Format: label_name

Labels are named locations in the code that can be jumped to with a call to goto. They are placed anywhere in the code where a different path needs to be taken than the default sequential execution order.
c
main

pause 100
puts 0,[“Hello, world!”, 13]
end

Goto

Format:
goto label_name

Goto is used to jump from one place in code to another. Goto is called with one parameter: the name of the label to jump to.
main

pause 100
puts 0,[“Hello, world!”, 13]
goto main

Gosub

Format:
gosub label[argument1,…,argumentN], result

The gosub routine jumps to a named label but also passes arguments to the subroutine and can accept a return value from the subroutine. The return statement is used to exit the subroutine as well as pass back a return value to the calling gosub command.
result var long

Main

gosub myadd[10,100], result
puts 0,[“Result =”,dec result]

End

arg1 var long
arg2 var long

myadd [arg1,arg2]

return arg1+arg2