Skip to content

Operators

Frederic Jarjour edited this page May 24, 2022 · 5 revisions

Operators

Kode is currently supporting many operators including mathematical operators like addition, substraction, multiplication, division, modulo, square roots, and power and evaluation operators like not, equal, not equal, greater than, strictly greater than, smaller than, and strictly smaller than.

Addition

Addition can be performed between any numerical data type (int and float) and between strings. Additions with the same numerical data type will result in an output with that same data type, but a float and integer addition will always result in a floating-point number output.

int + int = int

val output = 5 + 3
print(output)
print(typeOf(output))

# Output:
# 8
# int

float + float = float

val output = 5.2 + 3.3
print(output)
print(typeOf(output))

# Output:
# 8.5
# float

int + float = float

val output = 5 + 3.2
print(output)
print(typeOf(output))

# Output:
# 8.2
# float

string + string = string

val output = "Hello " + "World"
print(output)
print(typeOf(output))

# Output:
# Hello World
# string

Subtraction

Subtractions can be performed between any numerical data types.

int - int = int

val output = 5 - 2
print(output)
print(typeOf(output))

# Output:
# 3
# int

int - float = float

val output = 5 - 2.2
print(output)
print(typeOf(output))

# Output:
# 2.8
# float

Multiplication

Multiplications can be performed between any numerical data types. int * int = int

val output = 4 * 3
print(output)
print(typeOf(output))

# Output:
# 12
# int

int * float = float

val output = 4 * 3.5
print(output)
print(typeOf(output))

# Output:
# 14.0
# float

int * string = string

An integer can be multiplied by a string to duplicate that string by the integer's value.

val output = 4 * "hello "
print(output)
print(typeOf(output))

# Output:
# hello hello hello hello 
# string

Division

Divisions can be performed between any numerical data types.

int / int = int

Diving an integer by another integer provides an integer division operation where the result's value is rounded down.

val output = 10 / 3 # Notice that both 10 and 3 are integers
print(output)
print(typeOf(output))

# Output:
# 3
# int

int / float = float

val output = 10 / 3.0 # Notice that 10 is an integer and 3.0 is a floating number
print(output)
print(typeOf(output))

# Output:
# 3.3333333
# float

Modulo

Modulo can be performed with any numerical data types.

int % int = int

val output = 10 % 3
print(output)
print(typeOf(output))

# Output:
# 1
# int

float % float = float

val output = 5.5 % 2.3
print(output)
print(typeOf(output))

# Output:
# 0.9
# float

float % int = float

val output = 5.5 % 3
print(output)
print(typeOf(output))

# Output:
# 2.5
# float

Power & Square Root

Power & square root can be performed with any numerical data types.

int ^ int = int

val output = 2 ^ 5
print(output)
print(typeOf(output))

# Output:
# 32
# integer

int ^ float = float

In this manner, it is possible to perform mathematical square root operations. Alternativaly, the built-in sqrt() function can also be used for square roots.

val output = 2 ^ 0.5
print(output)
print(typeOf(output))

# Output:
# 1.414213
# float
Clone this wiki locally