Arithmetic logic
Learn how to use arithmetic, comparison and logical operators in ink, including conditional blocks (if / else / switch) and built-in functions like POW, INT, FLOOR, FLOAT and RANDOM.
You can read more about logic and variables in ink on the official documentation.
ink is fully-featured in terms of arithmetic and logic. Any line prefixed with ~ is treated as a logic line rather than story text, which is how you perform calculations and assignments without printing anything to the screen.
Arithmetic operators
ink supports the following arithmetic operators:
| Operator | Description |
|---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% / mod | Modulo (remainder after integer division) |
~ x = (x * x) - (y * y) + c
~ y = 2 * x * y
~ remainder = 10 mod 3Implicit numerical types
Results of operations are typed based on the type of the inputs. In particular, integer division returns an integer, while floating-point division returns a float:
~ x = 2 / 3 // x is 0 (integer)
~ y = 7 / 3 // y is 2 (integer)
~ z = 1.2 / 0.5 // z is 2.4 (float)Comparison and logical operators
The following operators are available for building conditions:
| Operator | Description |
|---|---|
== | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
&& / and | Logical AND |
|| / or | Logical OR |
! / not | Logical NOT |
Conditions are written inline inside { } braces:
{ x == 1.2 }
{ x / 2 > 4 }
{ y - 1 <= x * x }String queries
ink supports three basic string operations:
{ "Yes, please." == "Yes, please." } // equality
{ "No, thank you." != "Yes, please." } // inequality
{ "Yes, please" ? "ease" } // substring / containsConditional blocks (if / else)
{ x > 0:
~ y = x - 1
}{ x > 0:
~ y = x - 1
- else:
~ y = x + 1
}{
- x == 0:
~ y = 0
- x > 0:
~ y = x - 1
- else:
~ y = x + 1
}{ x:
- 0: zero
- 1: one
- 2: two
- else: lots
}Other features
To raise a number to a power use POW(base, exponent):
{POW(3, 2)} is 9.
{POW(16, 0.5)} is 4.When you need explicit type conversion or controlled rounding, use the built-in cast functions:
| Function | Description |
|---|---|
INT(x) | Truncates toward zero |
FLOOR(x) | Rounds down (toward negative infinity) |
FLOAT(x) | Converts to floating-point |
{INT(3.2)} // 3
{FLOOR(4.8)} // 4
{INT(-4.8)} // -4
{FLOOR(-4.8)} // -5
{FLOAT(4)} // 4.0ink can generate a random integer with RANDOM(min, max). Both bounds are inclusive (like rolling a dice):
~ temp dice_roll = RANDOM(1, 6)
~ temp score = RANDOM(30, 75)
~ temp heads = RANDOM(3, 8)Conditional blocks are not limited to pure logic — they can wrap story text and even choices:
I stared at Monsieur Fogg.
{ know_about_wager:
<> "But surely you are not serious?" I demanded.
- else:
<> "But there must be a reason for this trip," I observed.
}{ door_open:
* I strode out of the compartment[] and I fancied I heard my master quietly tutting to himself.
-> go_outside
- else:
* I asked permission to leave[] and Monsieur Fogg looked surprised.
-> open_door
* I stood and went to open the door[]. Monsieur Fogg seemed untroubled by this small rebellion.
-> open_door
}Multiple conditions can be combined with and / && and or / ||:
{
- visited_snakes && not dream_about_snakes:
~ fear++
-> dream_about_snakes
- visited_poland && not dream_about_polish_beer:
~ fear--
-> dream_about_polish_beer
- else:
-> dream_about_marmalade
}