3 Julia Programming: Conditionals, Loops and Functions

 

This chapter covers

  • Creating compound expressions
  • Writing if-else statements and loops
  • Using short circuit evaluation for inline conditionals
  • Writing functions
  • Defining methods for functions and structs

In the previous chapter we have seen the building blocks of Julia. We have seen how to define variables as well as basic data types and operations in Julia. We have also seen the data structures to work with collections of data.

In this chapter we will take one more step and learn about the decision logic in Julia. We will also see how to define and work with functions. Multiple dispatch and constructors are important and strong features of Julia and knowing how to use them will save you significant time and effort in the future. I strongly suggest to learn and apply these methods in your future work.

A computer program completes the tasks in one or a combination of several ways.

  • Completing the tasks in the given order.
  • Choosing the tasks to be completed based on predefined conditions.
  • Executing the given tasks until certain conditions are met.

In the first one the code is executed line by line. In the second case, we use a conditional which is known as if-else statement. In the final case, we use loops. In Julia there are for and while loops.

3.1 Compound Expressions

3.2 Conditional Evaluation

3.3 Short Circuit Evaluation

3.4 Loops

3.4.1 For Loops

3.4.2 While Loops

3.4.3 Break and Continue

3.4.4 Comprehensions

3.5 Functions

3.5.1 Defining Functions

3.5.2 Variable Number of Arguments

3.5.3 Keyword Arguments

3.5.4 Broadcasting and Dot Syntax

3.5.5 Composite Functions

3.5.6 Mutating Functions

3.6 Methods

3.6.1 Multiple Dispatch

3.6.2 Parametric Methods

3.6.3 Function Like Objects