[English/日本語]

|Next Lesson>

Julia programming

Lesson 0: Getting Familiar with Julia

Here, we familiarize ourselves with Julia programming.


Lesson 0-1: Execution

We will first learn the basic steps for programming in Julia:

  1. Write the code in the Julia language with the extension .jl.
  2. Run the file prepared in the previous step using the julia command in your terminal.

To learn this procedure, we will write and execute an example code for a well-known program, commonly known as "Hello World." First, write the following Julia code and save it as a file named lesson0_1.jl.

println("Hello world!!")

Here, println is a function used to output variables. In this case, the print function is used to output the string Hello world!!. In Julia, by enclosing characters in double-quotation marks (""), the string between them can be treated as a character variable.

Once the above code is ready, enter and execute the following command on your terminal to run the Julia program.

julia lesson0_1.jl

As a result, the following message is output on your display:

Hello world!!

Here, we learned how to write and execute Julia code:

  1. Save the Julia code in a file with the .jl extension.
  2. Execute the above code in the terminal using the julia command.

In the following sections, we will explore the basics of Julia programming.


Lesson 0-2: Basic Computing Operations

In many cases, numerical computations involve combining fundamental mathematical operations such as addition, subtraction, multiplication, division, and exponentiation. In this section, we will learn how to execute these basic mathematical operations using Julia. Let's start by examining the following sample program.

(lesson0_2.jl)
a = 3.0
b = 2.0

println("a=",a)
println("b=",b)

c = a + b
d = a - b
e = a * b
f = a / b
g = e^b

println("a+b=",c)
println("a-b=",d)
println("a*b=",e)
println("a/b=",f)
println("a^b=",g)

In this program, the first and second lines assign the real numbers 3.0 and 2.0 to the variables a and b, respectively. In Julia, the equality sign (=) does not signify that the left and right sides are equal; rather, it serves as an instruction to substitute the right side for the left side.

The fourth and fifth lines that follow are instructions to output the real numbers assigned above using the println statement.

In Julia, addition is represented by the symbol +, subtraction by the symbol -, multiplication by the symbol *, division by the symbol /, and exponentiation by the symbol ^.

In the provided code, the last lines (lines 13 to 17) instruct the program to output the results of the four arithmetic operations and the exponentiation operation using the print statement, respectively.

Save the above code in a file with the .jl extension and execute it in your terminal using the julia command. The instructions in the code will be executed sequentially from the top. Verify whether the obtained results align with the expected outcomes.



Lesson 0-3: Arrays

In the above source code, a single value was stored for each variable. These variables are referred to as scalar variables. While scalar variables are fundamental in programming, there are many situations where it is beneficial to store multiple values in a single variable. In this context, we will explore Julia's array function, designed for this purpose. Let's examine the following program.

(lesson0_3.jl)
array_a = ["April", "May", "June"]

println(array_a)

println(array_a[1])
println(array_a[2])
println(array_a[3])

array_b = [0.6, 1.2, 1.8]

println(array_b)

println(array_b[1])
println(array_b[2])
println(array_b[3])

In the first line of the above source code, an array is created with the three character variables 'April', 'May', 'June' as elements, enclosed in [], and assigned to the variable array_a. In Julia, creating an array can be accomplished by enclosing its components within [].

In the third line of the source code, the entire array is output using the println function. Conversely, lines 5 through 7 output the array elements one by one. This allows reading only specific elements of the array. For example, array_a[2] represents the second element of the array. It's important to note that element numbering in Julia starts from the 1st element.

The above description pertains to an array whose elements are the strings "April", "May", "June", but the elements of the array can be variables of other types. For instance, lines 10 through 16 of the above source code instruct to store three real numbers in the array array_b and output each element. Execute the Julia program above to observe the behavior of the array.


Lesson 0-4: Arrays and Iteration

As observed thus far, when the source code is written and the program is executed, the instructions are carried out sequentially from the top of the code. Consequently, when crafting a program involving numerous procedures, the code tends to become lengthy and time-consuming to write. Julia offers a function to simplify and enhance the readability of code by iterating through instructions in a loop. Let's examine the following source code.

(lesson0_4.jl)
array_c = ["April", "May", "June"]

for a in array_c
    println(a)
end

println("End")

The first line of the above source code creates an array with three strings, "April", "May", "June", as elements, and stores them in the variable array_c.

The third line of code initiates the definition of the iteration section with the for a in array_c: statement. Here, the variable a serves as an iterator, changing with each iteration. It takes on the value of each element in the array variable array_c during each loop iteration. In the provided code, the variable a is assigned the three strings "April", "May", "June" in sequence, and the processing within the iteration section is executed in each iteration.

In Julia, the iteration section in the for statement is defined using for and end. Thus, in the case of the above source code, only the println(a) part is iterated.

In the given source code example, following the iterative processing of the for loop, the "End" log is output once by println("End"), marking the termination of the program.

In summary, the above program initially creates an array variable array_c with three elements, then utilizes a for loop to output each element of the array via println(a), and finally, outputs an exit log End to conclude the program.

Execute the above program to observe the behavior of the iterative process using the array and the for statement.


Lesson 0-5: For Loop and Iteration

In the preceding section, we explored iterative processing using the array. In that context, we gained insights into how to iterate over each element in an array. Beyond utilizing arrays, it is also possible to explicitly define the length of the for loop. Let's examine the following source code to understand the behavior of the for loop.

(lesson0_5.jl)
println("loop a")

for i = 1:10
    println(i)
end

println("loop b")

for j = 1:2:10
    println(j)
end

Here is a brief explanation of the provided code:



|Next Lesson>

[Back to Julia]