Categories: Julia

Conditional Statements in Julia

Now that we’ve covered looping, we can repeat whatever we do. Thus, today we cover conditionals, so as to be able to choose when we do it.

If construct

We all know this, so let’s jump into code:

julia> x=1;y=2;z=3;if x<y
       println("first")
       elseif y<z
       println("second")
       else
       println("default")
       end

Just a reminder, the indentation is immaterial. And again, as per usual, elseif and else blocks are optional.

Ternary operator

We can write an if loop in one-line as:

julia> x==y ? println("ok") : println("no")

Alas, that’s too tedious, because its Julia we’re in, we can do a more succinct version:

julia> println(x==y ? "equal" : "unequal")

And of course you can chain ternary operators.

julia> println(x<y ? "x" : y<z ? "y" : "z")#Finding the least valued variable

And that’s it for today! Next up, we discuss a truly distinguishing feature of Julia, the way it deals with arrays.

References

  1. Julia Docs
Editorial Staff

Recent Posts

MapReduce Algorithm

In this tutorial, we will focus on MapReduce Algorithm, its working, example, Word Count Problem,…

8 months ago

Linear Programming using Pyomo

Learn how to use Pyomo Packare to solve linear programming problems. In recent years, with…

1 year ago

Networking and Professional Development for Machine Learning Careers in the USA

In today's rapidly evolving technological landscape, machine learning has emerged as a transformative discipline, revolutionizing…

1 year ago

Predicting Employee Churn in Python

Analyze employee churn, Why employees are leaving the company, and How to predict, who will…

2 years ago

Airflow Operators

Airflow operators are core components of any workflow defined in airflow. The operator represents a…

2 years ago

MLOps Tutorial

Machine Learning Operations (MLOps) is a multi-disciplinary field that combines machine learning and software development…

2 years ago