Skip to content

Conditionals

Like other programming languages, conditionals are supported in Notes Calculator.

The first two fundamental values you need to know are true and false. These values are used to represent the truthiness of a condition.

Notes Calculator
Calculation example
CalculationResult
truetrue
falsefalse

Comparison operators are used to compare two values and return a boolean result (true or false).

OperatorNameBehaviour
==Equal toReturns true if left value and right value are equal
!=Not equal toReturns true if left value and right value are not equal
>Greater thanReturns true if the left value is greater than the right value
<Less thanReturns true if the left value is less than the right value
>=Greater than or equal toReturns true if the left value is greater than or equal to the right value
<=Less than or equal toReturns true if the left value is less than or equal to the right value

For example:

Notes Calculator
Calculation example
CalculationResult
1 == 1true
1 != 2true
1 < 2true
2 < 1false
1 >= 1true
2 <= 2true

Logical operators are used to combine multiple conditions. The result is true if the combined condition

OperatorNameBehaviour
&& or andandReturns true if both conditions are true
|| or ororReturns true if at least one condition is true
notnotFlips a condition: true becomes false and back
xorexclusive orReturns true if exactly one of the two conditions is true
Notes Calculator
Calculation example
CalculationResult
1 == 1 && 2 == 2true
1 == 1 and 2 != 2false
1 == 1 || 2 != 2true
1 != 1 or 2 != 2false

not and xor are case-insensitive, and a plain number works as a condition too: zero is false, anything else is true.

Notes Calculator
Calculation example
CalculationResult
not falsetrue
not true and falsefalse
true xor falsetrue
true xor truefalse
5 and 3true
5 and 0false

not binds looser than xor, so put it in brackets to use it as an xor operand: true xor (not false).

The integer xor is unchanged: with whole numbers it still works bit by bit:

Notes Calculator
Calculation example
CalculationResult
5 xor 36

The if, else, and then keywords are used to control the flow of execution based on conditions.

Notes Calculator
Calculation example
CalculationResult
earnings = $25k$25,000
if earnings > $30k then tax = 15% else tax = 5%5%
earnings x tax$1,250

Conditions can be variables, comparisons, or combinations of them:

Notes Calculator
Calculation example
CalculationResult
x = 55
if x > 1 and x < 9 then 1 else 21
if 0 then 1 else 22
if false then 7false

Hours worked on a normal day:

Notes Calculator
Calculation example
CalculationResult
weekend = falsefalse
if not weekend then 8 else 08

With no else, a false condition gives false.

condition ? value if true : value if false does the same job in fewer characters:

Notes Calculator
Calculation example
CalculationResult
true ? 1 : 01
5 > 3 ? 10 : 2010
not false ? 1 : 01
true xor false ? 10 : 2010

Wrap a conditional in brackets and it behaves like any other value:

A member discount, for example:

Notes Calculator
Calculation example
CalculationResult
member = truetrue
$100 x (if member then 0.9 else 1)$90
$100 x (member ? 0.9 : 1)$90
2 * (5 > 3 ? 10 : 20)20

The branches keep their own type, so a conditional can return money or a unit amount, not just a plain number:

Notes Calculator
Calculation example
CalculationResult
express = truetrue
express ? 15 : 5€15
checked = falsefalse
if checked then 23 kg else 8 kg8 kg

If the condition itself can’t be worked out, such as an empty total or something like 0/0, the line shows no answer at all rather than quietly taking the false branch:

Notes Calculator
Calculation example
CalculationResult
total ? 1 : 2
0/0 ? 1 : 2
Notes Calculator
Calculation example
CalculationResult
income = $40k$40,000
expenses = $25k$25,000
profitable = true if income > expensestrue
insolvent = false unless expenses > incomefalse