Conditionals

Introduction to Julia

James Fulton

Climate informatics researcher

What are conditional expressions?

  • Tell our computer to do some action if a condition is met
  • Allow us to write code which makes its own decisions
is_raining = true


# Conditional expression if is_raining
println("Better get your coat") end
Better get your coat
Introduction to Julia

What are conditional expressions?

  • Tell our computer to do some action if a condition is met
  • Allow us to write code which makes its own decisions
is_raining = true 


# Conditional expression if is_raining
println("Better get your coat") end
Better get your coat
Introduction to Julia

What are conditional expressions?

is_raining = false

# Conditional expression
if is_raining
    println("Better get your coat")
end

Introduction to Julia

Multiple lines of code under the if statement

is_raining = true

if is_raining
    # This can be many lines of code

println("The weather is awful") println("Better get your coat")
end # Code below end is always run println("Ready to go")
The weather is awful
Better get your coat
Ready to go
is_raining = false

if is_raining
    # This can be many lines of code
    println("The weather is awful")
    println("Better get your coat")
end

# Code below end is always run
println("Ready to go")
Ready to go
Introduction to Julia

Comparisons

When raining:

amount_of_rain = 1.


# Use comparison is_raining = amount_of_rain > 0
println(is_raining)
true

When dry:

amount_of_rain = 0.


# Use comparison is_raining = amount_of_rain > 0
println(is_raining)
false
Introduction to Julia

Comparisons

When raining:

amount_of_rain = 1.

# Use comparison
is_raining = amount_of_rain > 0

# Conditional expression
if is_raining
    println("Better get your coat")
end
Better get your coat

When dry:

amount_of_rain = 0.

# Use comparison
is_raining = amount_of_rain > 0

# Conditional expression
if is_raining
    println("Better get your coat")
end

Introduction to Julia

Comparisons

When raining:

amount_of_rain = 1.




# Conditional expression
if amount_of_rain>0
    println("Better get your coat")
end
Better get your coat

When dry:

amount_of_rain = 0.




# Conditional expression
if amount_of_rain>0
    println("Better get your coat")
end

Introduction to Julia

Other comparisons

  • a == b check if two values are equal
a = 1.


# Value of a is 1? println(a==1)
true
# Data type of a is Float64?
println(typeof(a)==Float64)
true
Introduction to Julia

Other comparisons

  • a == b check if two values are equal
  • a != b check if two values are not equal
a = 1.


# Value of a is not 1? println(a!=1)
false
# Data type of a is not Float64?
println(typeof(a)!=Float64)
false
Introduction to Julia

Other comparisons

  • a == b check if two values are equal
  • a != b check if two values are not equal
  • a > b check if a greater than b
  • a >= b check if greater than or equal to
a = 1.


# a is greater than 1? println(a>1)
false
# a is greater than or equal to 1?
println(a>=1)
true
Introduction to Julia

Other comparisons

  • a == b check if two values are equal
  • a != b check if two values are not equal
  • a > b check if a greater than b
  • a >= b check if greater than or equal to
  • a < b check if a less than b
  • a <= b check if less than or equal to
a = 1.


# a is less than 1? println(a<1)
false
# a is less than or equal to 1?
println(a<=1)
true
Introduction to Julia

When the condition is not met

amount_of_rain = 0.

if amount_of_rain == 0
    # Do this if conditon is met
    println("The sky looks clear")

else # Do this if not met println("Better get your coat")
end
The sky looks clear
amount_of_rain = 5.

if amount_of_rain == 0
    # Do this if conditon is met
    println("The sky looks clear")
else
    # Do this if not met
    println("Better get your coat")
end
Better get your coat
Introduction to Julia

Additional conditions

amount_of_rain = 0.

if amount_of_rain == 0
    println("There is zero rain")

elseif amount_of_rain < 1 # Add a second condition println("Better get your coat")
else println("That's a lot of rain, stay home") end
There is zero rain
Introduction to Julia

Additional conditions

amount_of_rain = 0.5

if amount_of_rain == 0
    println("There is zero rain")
elseif amount_of_rain < 1
    # Add a second condition
    println("Better get your coat")
else
    println("That's a lot of rain, stay home")
end
Better get your coat
Introduction to Julia

Additional conditions

amount_of_rain = 2

if amount_of_rain == 0
    println("There is zero rain")
elseif amount_of_rain < 1
    # Add a second condition
    println("Better get your coat")
else
    println("That's a lot of rain, stay home")
end
That's a lot of rain, stay home
Introduction to Julia

Multiple elseif's

amount_of_rain = 2

if amount_of_rain == 0
    println("There is zero rain")
elseif amount_of_rain < 1     # <--- many elseif conditions
    println("Better get your coat")
elseif amount_of_rain < 5     # <--- many elseif conditions
    println("You're going to need a bigger coat")
else
    println("That's a lot of rain, stay home")
end
You're going to need a bigger coat
Introduction to Julia

Let's practice!

Introduction to Julia

Preparing Video For Download...