-
Notifications
You must be signed in to change notification settings - Fork 1
Conditional
Conditional is a syntax structure of PapajScript, that allows executing various options depending on a condition. The first conditionals were introduced on August 3, 2018 and the first stable version of RPN Calculator that had contidionals was 0.4.1.
This paragraph includes current conditionals' syntaxes, that were introduced along with the premiere of RPN Calculator 0.5.0
The if
checks the conditional expression and launches the next instruction only when the check is successful.
The else
launches the next instruction only when the recent condition check was unsuccesful.
The elif
checks the conditional expression (just like if
) if a check of the previous one was not successful.
Syntaxes:
if ( B1 ) { I1 }
if ( B1 ) { I1 } else { I2 }
if ( B1 ) { I1 } elif ( B2 ) { I2 }
if ( B1 ) { I1 } elif ( B2 ) { I2 } else I3
if ( B1 ) { I1 } elif ( B2 ) { I2 } elif ( B3 ) { I3 } ... else { IN }
if ( B1 ) { I1 } elif ( B2 ) { I2 } elif ( B3 ) { I3 } ... elif ( BN ) { IN }
Syntaxes:
condition function callIf
condition function callUnless
function condition callIf
function condition callUnless
Syntaxes:
condition ? if { block_if_condition_true }
condition ? else { block_if_condition_false }
condition ? if { block_if_condition_true } else { block_if_condition_false }
condition ? unless { block_if_condition_false }
condition ? if { block_if_condition_true } unless { block_if_condition_false }
This type of conditionals was introduced on August 3, 2018 (first stable RPN Calculator version – 0.4.1), prior to the introduction of variables. The last version that supported these conditionals was 0.4.3, as this type of conditional became rebuilt with a slightly different syntax.
Examples:
$x 0 >= ? if { "this number is non-negative" println } else { "this number is negative" println }
Syntax: B1 ? if: I1 else: I2
The question mark checks if an expression B1 is true or its numerical value is equal to 0.
The if:
launches the next instruction only when the recent ?-check was successful.
The else:
launches the next instruction only when the recent ?-check was unsuccessful.
Since August 22, 2022 this type of conditional has become obsolete. The last stable version that supported it was 0.5.2.2.
Examples:
scan toNumber 2 mod ? if: { "This number is even." println } else: { "This number is odd." println }
scan toNumber 5 > ? if: { "This number is greater than 5" println }
scan toNumber 5 <= ? else: { "This number is greater than 5" println }
if ( scan toNumber 2 mod ) { "This number is even." println } else { "This number is odd." println }
if ( scan toNumber 5 > ) { "This number is greater than 5" println }
if ( scan toNumber 5 <= not ) { "This number is greater than 5" println }
if ( scan toNumber 10 mod ) 1 else 0
if ( x 1 = ) {
"X is equal to 1" println
} elif ( x 2 = ) {
"X is equal to 2" println
} else {
"X is neither equal to 1 nor equal to 2" println
}
Old-style conditional (versions 0.5.0.0 - 0.5.2.2)
function {
>n
n 0 = ?
if: 1
else: {
n n -- factorial *
}
} -> factorial
scan toNumber factorial
Post-fix conditional
function {
>n
n 0 = -> cond
$cond fun{ 1 } callIf
$cond fun{
n n -- factorial *
} callUnless
} -> factorial
scan toNumber factorial
C-like conditional
function {
>n
if ( n 0 = ) {
1
} else {
n n -- factorial *
}
} -> factorial
scan toNumber factorial
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