Skip to content

Conditional

Paul Lipkowski edited this page Aug 22, 2022 · 13 revisions

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.

Currently supported conditionals

This paragraph includes current conditionals' syntaxes, that were introduced along with the premiere of RPN Calculator 0.5.0

The C-like conditional

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 }

Post-fix conditionals

Syntaxes:

  • condition function callIf
  • condition function callUnless
  • function condition callIf
  • function condition callUnless

Obsolete types of conditionals

First conditionals (versions 0.4.1-0.4.3)

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 change of the first conditionals (versions 0.5.0.0 - 0.5.2.2)

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 }

Examples of code

  • 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
Clone this wiki locally