﻿ CONDITIONAL LOOPING
TYL PROGRAMMING LANGUAGE
▶ CONDITIONAL LOOPING
Conditional looping is the repeating of code execution while a condition is met.
Conditional looping signature:
[ON] ~

Unconditional looping signature:
~

Conditional looping line:
on ~
The `on` variable is a local boolean variable of the looping statement, that is set to TRUE, and the execution of the repeat block will continue until the value of `on` will be FALSE, or otherwise terminated.

Unconditional looping line:
~
The repeat block will continue to be executed endlessly. Termination is typically done by brake statements, or by debugging functions.

Tyl permits uncoditional looping because instead of putting a condition variable in the signature, one or more conditional statements are put inside the looping block that terminate block execution.

Unconditional looping with break:
ctr 0
max 3
~ checkstop ? % \ print ctr
checkstop:
ctr ++
ctr max ? %% \t
\f
1
2
3
At the uncoditional line, `print ctr` will be executed while `checkstop` function returns false.

Conditional looping statement:
print num 5
on ~
num > 0 ? num -- 13 \ num ++ 17
print num
not num ? on \f
^
5
-8
9
-4
13
0
`num` is drawn to zero, with jumps of up 17, and down 13, until it reaches zero. The line `not num ? on \f`, is responsible for the looping to stop.

If we try the above code, with up 4, down 2, and `num` 1, we see that the sequence will be: 1, 3, 1, -1, 3 ...., so it will never reach zero.
The astute readers will notice that the program mechanism creates a cyclic sequence anyway, thus, if after some iterations there wasn't a zero, we can safely assume that it will loop forever without reaching zero.

The solution is to use a stop counter that will stop the looping:
print num 5
stopctr 100
on ~
num > 0 ? num -- 13 \ num ++ 17
print num
not num ? on \f
not stopctr -- ? on \f
^
The line `not num ? on \f`, will check if `num` is zero, if TRUE it will set `on` to FALSE, causing the loop to stop.
Likewise, the statement condition '`not stopctr --`', will decrement `stopctr`, until it reaches zero. That will ignite the `not` function to return TRUE, and the looping will be stopped.

If we define the number of iterations it takes `num` to reach zero as the `attraction` of `num`, it constructs a mapping:
num → attraction
We can try different starting numbers to try and find a specific attraction.

Here's Zero Attraction Searcher program:
attraction 8
numctr 0
numstopctr 30
attractstopctr 30
nums
checkednum
attractstr ''
attractfound \f
numson ~
checkednum num numctr ++
nums ->->
nums <- num
stopctr attractstopctr
attracton ~
num > 0 ? num -- 13 \ num ++ 17
num ? nums <- num \ attracton \f
not stopctr -- ?
attracton \f
numson \f
^
^
numslen len nums
attractstr ++ checkednum + ': ' + numslen + newline
numslen attraction ?
attractfound \t
numson \f
^
numctr numstopctr ? numson \f
^
attractfound ?
print attractstr + newline
print 'attraction: ' + attraction
print 'start number: ' + checkednum
print 'sequence: ' + nums
\
print 'Zero was not reached!'
^
1: 7
2: 14
3: 21
4: 28
5: 5
6: 12
7: 19
8: 26
9: 3
10: 10
11: 17
12: 24
13: 1
14: 8

attraction: 8
start number: 14
sequence: [ 14, 1, -12, 5, -8, 9, -4, 13 ]
PART 3