© Tyl Software Foundation 2019-2021
▶ BREAK STATEMENT
Break statements are used when the execution of a
compound statement needs to
be stopped immediately.
To make a break statement, write:
To make a break statement, write:
%
Tyl Symbolizer will replace the percent code '%', with '✖', the break
symbol:
✖
Say we have a list of numbers, and we want to find the index of the first
number that is less than or equal to 3:
nums 5 6 1 0
matchedindex -1
index 4 ~
num nums index
num <= 3 ?
matchedindex index
%
^
^
print matchedindex
2
matchedindex
starts with -1. In the looping block, each time num
gets one
of nums
items, and when it satisfies the condition num <= 3
, matchedindex
gets the current index. The break line '%
', will cause the looping to
terminate, and the program will continue right after the looping, in the line
print matchedindex
, where it will print 2.
Note that though the break statement is in the yes block of a conditional statement, it causes the system to search up the statements tree and find the first enclosing looping statement. If found, it will cause that looping to terminate, otherwise the enclosing function or module will terminate.
Break statement can execute a statement prior to breaking:
nums 5 6 1 0
matchedindex -1
index 4 ~
num nums index
num <= 3 ? % print matchedindex index
^
Statement '
Here's Get Last Messages program:
% print matchedindex index
', is a break statement that contains
the statement print matchedindex index
, which will be executed before
breaking, thus enabling the folding of condition statement 'num <= 3 ?
'.
Here's Get Last Messages program:
go:
! messages list has at least 3 messages
messages 'message #7080' 'message #7081' 'message #7082' 'message #7083' 'message #7084'
lastmessages getlastmessages messages 3
print lastmessages
getlastmessages messages messagesnum:
lastmessages
messageind len messages
~
messageind --
messagesnum --
messagesnum < 0 ? % lastmessages
lastmessages <- messages messageind
^
[ 'message #7084', 'message #7083', 'message #7082' ]
In
In line
Up to now, it's all fine. But what happens when there are less than three messages in
When
The solution is:
getlastmessages
function, messageind
tracks the messages
downward, each time adding the current message to lastmessages
variable.
messagesnum
acts as the stop counter, to stop the
conditional looping
after messagesnum
times, thus lastmessages
will have
the appropriate messages.
In line
messagesnum < 0 ? % lastmessages
, when
messagesnum
goes below zero, it activates the break statement '%
'.
Now break statement executes lastmessages
, which yields the list value itself,
and since there are no other statements to be executed in the function,
returns lastmessages
to the caller.
Up to now, it's all fine. But what happens when there are less than three messages in
messages
list?
When
messagesnum
will be zero, and still in the valid stop counting range,
messageind
will be -1, and the get current message line
messages messageind
, will be invalid since messageind
is out of range!
The solution is:
go:
! messages list has less than 3 messages
messages 'message #7080' 'message #7081'
lastmessages getlastmessages messages 3
print lastmessages
getlastmessages messages messagesnum:
lastmessages
messageind len messages
stopind messageind - messagesnum
stopind < 0 ? stopind 0
~
messageind <= stopind ? % lastmessages
lastmessages <- messages ( messageind -- )
^
[ 'message #7081', 'message #7080' ]
As it becomes apparent,
Usually break statement is placed inside a conditional statement. But there are some times where it can be used also arbitrarily.
For ex., while debugging:
messageind
, can act as a stop counter for
both cases. A stopind
was added that helps messageind
act as both
list indexer and stop counter.
Usually break statement is placed inside a conditional statement. But there are some times where it can be used also arbitrarily.
For ex., while debugging:
num 0
stopctr 3
~
num ++
print num
num stopctr ? %
! limit looping to one iteration
%
^
1
The looping is breaked after one iteration, to see if the repeat block is
working correctly.