Tyl Programming Language
▶ BREAK STATEMENT
Break statements are used when the execution of looping statement needs to be stopped immediately.

To make a break statement, write:
%
Tyl symbolizer will replace the percent code: '%', with: '✖', the break symbol:

Break statement must be written in its own line (except for one-line statements)!

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
matched_index -1

index 4 ~
 num nums index

 num <= 3 ?
  matched_index index
  



print matched_index
2
matched_index starts with: -1. In the looping, when the current item of nums satisfies the condition: 'num <= 3', matched_index 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 matched_index', where it will print: 2.
Note that even though the break statement is in the repeat 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.

In Variable Assignment - Part 4, there is a pseudo-code in get_last_messages function.

Let's try and implement it:
main »
 messages 'message #7080' 'message #7081' 'message #7082' 'message #7083' 'message #7084'

 get_last_messages messages 3 last_messages
print last_messages

get_last_messages messages num last_messages »
 message_ind len messages
 messages_ctr num

 on ~
  message_ind --
  messages_ctr --

  messages_ctr < 0 ?

  message messages message_ind
  last_messages message

[ 'message #7084', 'message #7083', 'message #7082' ]
In get_last_messages function, message_ind tracks the messages downward, each time adding the current message to last_messages parameter. messages_ctr acts as the stop counter, to stop the conditional looping after num times, thus last_messages will have the appropriate messages.
In line: 'messages_ctr < 0 ? ✖', when messages_ctr goes below zero, it activates the break statement: '', and the program continues after the looping (effectively, out of the function).

Up to now, it's all fine. But what happens when there are less than three messages in messages list?

When messages_ctr will be zero, and still in the valid stop counting range, message_ind will be: -1, and the get current message line: 'message messages message_ind', will be invalid since message_ind is out-of-range!

The solution is:
main »
 messages 'message #7080' 'message #7081'

get_last_messages messages 3 last_messages
print last_messages

get_last_messages messages num last_messages »
 message_ind len messages
 messages_ctr num

 on ~
  message_ind --
  messages_ctr --

  message_ind < 0 ?
  messages_ctr < 0 ?

  message messages message_ind
  last_messages message

[ 'message #7080', 'message #7081' ]
As it becomes apparent, message_ind, can act also as a stop counter for such case, and the stop line: 'message_ind < 0 ? ✖', is added.

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

on ~
 num ++
print num




1
The looping is breaked after one iteration, to see if the repeat block is working correctly.