-
Notifications
You must be signed in to change notification settings - Fork 1
Loop
Loop is a syntax structure of PapajScript, that allows executing an instruction (or a set of them) multiple times. The first loops were introduced on January 12, 2018, along with the update for RPN Calculator 0.3.0.
The first ever introduced loop is times-loop, which has 2 syntaxes
Name | Syntax | Purpose | Since | Last supported version |
---|---|---|---|---|
times-loop | Xn I1 |
Do I1 n times (n >= 1) | 0.3.0 | 0.4.1 |
times-loop | N1 times I1 |
Do I1 N1 times | 0.3.1 | supported today |
Name | Syntax | Purpose | Since |
---|---|---|---|
times-loop | N1 times I1 |
Do I1 N1 times | 0.3.1 |
while-loop | while ( B1 ) I1 |
While B1 is true, do I1 | 0.5.0 |
do-while-loop | do I1 while ( B1 ) |
Repeat doing I1 while B1 is true | 0.5.0 |
do-until-loop | do I1 until ( B1 ) |
Repeat doing I1 until B1 is true | 0.5.0 |
for-loop | for ( I1 ; B1 ; I2 ) I3 |
Start a loop with I1 and while B1 is true, do I3 and then I2 | 0.5.0 |
for-each-loop | for ( E1 : A1 ) I1 |
For each element E1 from array A1 do I1 | 0.5.1 |
for-each-indexed-loop | for ( J1 E1 : A1 ) I1 |
For each element E1 with index J1 from array A1 do I1 | 0.5.2.2 |
Name | Syntax | Purpose | Since |
---|---|---|---|
callWhile | EXP1 FUN1 function | Checks for a condition expression and repeats the procedure as long as the condition is true (equivalent of C-like while loop) |
0.5.2 |
callWhile | FUN1 EXP1 function | Repeats the procedure as long as the condition contained in the expression is true (equivalent of C-like do-while loop) |
0.5.2 |
callUntil | EXP1 FUN1 function | Checks for a condition expression and repeats the procedure as long as the condition is false | 0.5.2 |
callUntil | FUN1 EXP1 function | Repeats the procedure as long as the condition contained in the expression is false | 0.5.2 |
Notes
- A
for-each-loop
may overwrite array items' values when assigning - Prior to the 0.5.3 version,
for-each-loop
had to contain a pre-defined array, sofor ( $i : $T sortNumbers ) { }
did not work. A kind of workaround was$T sortNumbers -> $U for ( $i : $U ) { }
- A
for-each-loop
andfor-each-indexed-loop
is able to change array's values by default. Since April 17, 2021 (earliest stable version 0.5.2) it is possible to restrict the loop from editing the array by usingconst
word, e.g.for ( const $i : $T )
orfor ( const $index $item : $T )
-
10 times { 100 random }
which generates 10 numbers from a range [0..99] (note: we must know the exact amount of iterations here) -
1 >i while ( i 10 <= ) { 100 random i ++ >i }
which also generates 10 numbers from a range [0..99] -
1 >i do { 100 random i ++ >i } while ( i 10 <= )
which also generates 10 numbers from a range [0..99] -
1 >i do { 100 random i ++ >i } until ( i 10 > )
which also generates 10 numbers from a range [0..99] -
for ( 1 >i ; i 10 <= ; i ++ >i ) { 100 random }
which also generates 10 numbers from a range [0..99] -
for ( item : [ 1 1 10 seq ] ) { i println }
prints a sequence from 1 to 10
for ( 1 -> i ; i 5 <= ; i ++ -> i ) {
i i * println
} // print squares of numbers 1..5
for ( i : [ 1 1 5 seq ] ) {
i i * println
} // print squares of numbers 1..5
[ 1 1 5 seq ] -> $T
$T println // prints [ 1 2 3 4 5 ]
for ( $i : $T ) {
$i $i * println
} // print squares of numbers 1..5, i.e. 1, 4, 9, 16, 25
$T println // prints [ 1 4 9 16 25 ]
[ 1 1 5 seq ] -> $T
$T println // prints [ 1 2 3 4 5 ]
for ( const $i : $T ) {
$i $i * println
} // print squares of numbers 1..5, i.e. 1, 4, 9, 16, 25
$T println // prints [ 1 2 3 4 5 ]
[ "a" "b" "c" ] -> $T
$T println // prints [ "a" "b" "c" ]
for ( const $i $item : $T ) {
$i " - " $item ", " + + + print
} // print `0 - a, 1 - b, 2 - c`
$T println // prints [ "a" "b" "c" ]
Versions of Papaj:
Pre-builds: Bereshit (v.0.0.1), Shemot (v.0.0.2)
Aleph (v.0.1.0), Bet (v.0.2.0), Gimel (v.0.2.1), Dalet (v.0.3.0)
Hey (v.0.3.1), Vav (v.0.4.0), Zain (v.0.4.1), Chet (v.0.4.2), Tet (v.0.4.3)
Yod (v.0.5.0), Khaf (v.0.5.1), Lamed (v.0.5.2), Mem (v.0.5.3), Nun (v.0.5.4), Samech (v.0.5.5)
Development version: Leviathan
Packages of Papaj:
Vanilla, Array, Console, Date, Math, Number, String
Structures of Papaj:
Conditional, Entity, Loop, Variable
Array, Boolean, DateTime, Exception, LogicalExpression, Function, Number, Null, String
Home, PapajScript, Papaj (interpreter), Papaj REPL