Operations#

Learning Objectives#

  • Perform basic arithmetic operations in Julia

  • Use logical operators to perform logical operations

  • Apply comparison operators to compare values

  • Understand the syntax and usage of different operators in Julia

  • Implement basic mathematical and logical operations in Julia scripts

Arithmetic Operations#

x = 10
y = 3

println("X: ", x)
println("Y: ", y)

# Addition 
println("Addition, x + y: ", x + y)

# Subtraction 
println("Subtraction, x - y: ", x - y)

# Multiplication 
println("Multiplication, x * y: ", x * y)

# Division 
println("Divison, x / y: ", x / y)

# Floor Division 
println("Floor Division, x ÷ y: ", x ÷ y)

# Modulus 
println("Modulus, x % y: ", x % y)

#Exponentiation
println("Exponentiation, x ^ y: ", x^y)
X: 10
Y: 3
Addition, x + y: 13
Subtraction, x - y: 7
Multiplication, x * y: 30
Divison, x / y: 3.3333333333333335
Floor Division, x ÷ y: 3
Modulus, x % y: 1
Exponentiation, x ^ y: 1000

Logical Operations#

Within Julia AND is denoted with &&, OR with || and NOT with !.

a = true
b = false

println("a: ", a)
println("b: ", b)


println("a AND B: ", a && b)  # false
println("a OR B: ", a || b)  # true
println("NOT a: ", !a)      # false
a: true
b: false
a AND B: false
a OR B: true
NOT a: false

Comparison Operations#

Comparison operations in Julia work on numeric and other comparable types.

println("X: ", x)
println("Y: ", y)

# Equality
println("Is x equal to y: ", x == y)

# Inequality
println(" Is x not equal to y: ", x != y)  # true

# Less than, Greater than
println("Is x less than y: ", x < y)   # false
println("Is x greater than y: ", x > y)   # true

# Less than or equal to, Greater than or equal to
println("Is x less than or equal to y: ", x <= y)  # false
println("Is x greater than or equal to y: ", x >= y)  # true
UndefVarError: `x` not defined

Stacktrace:
 [1] top-level scope
   @ In[1]:2