Basic constructions
Do you want to do programming? Engee uses the programming language Julia! If you are not familiar with Julia, start with Engee language basics and the course "Welcome to Engee". And examples provides different scenarios for usage of Julia. This will be useful for beginners and experienced programmers alike!
Basic constructs such as loops and conditions allow you to control the flow of program execution. They are essential for implementing complex logic and data processing. In this article, we will look at the basic constructs of the Julia language to help you get started in Engee.
Loops
Loops are constructs that allow you to repeat the execution of a particular block of code multiple times. There are two basic types of loops used in Julia: for
and while
. They help automate repetitive tasks, such as processing data, performing calculations, or searching collection items.
The for
loop
The for
loop is used to iterate over the elements of a collection (such as an array, range, or string). It is ideal when the number of iterations is known in advance.
Example: iterating over the elements of an array and displaying them on the screen.
for i in [1, 4, 0]
println(i)
end
Output
1 4 0
In this example, the variable i
takes values from the array [1, 4, 0]
and for each value, a block of code inside the loop is executed.
The for
loop can also be used to work with ranges:
for i in 1:5
println("Iteration: ", i)
end
Output
Iteration: 1 Iteration: 2 Iteration: 3 Iteration: 4 Iteration: 5
Here the loop is executed 5 times, looping through the values from 1 to 5.
Loop while
The while
loop executes a block of code as long as a certain condition is fulfilled. It is useful when the number of iterations is unknown in advance and depends on the fulfilment of the condition.
Example: incrementing the i
variable until its value is less than 5.
i = 1
while i < 5
println("Current value: ", i)
global i += 1
end
Output
Current value: 1 Current value: 2 Current value: 3 Current value: 4
In this example, the loop is executed as long as the variable i
is less than 5. At each iteration, the value of i
is incremented by 1.
When to use for
and while
-
Use the
for
loop if the number of iterations is known in advance or if you want to loop through the elements of a collection. -
Use the
while
loop if the number of iterations depends on the fulfilment of a condition that may change during the execution of the program.
Both loops can be useful in different situations, and the choice between them depends on the task at hand.
If you want to interrupt the loop before it completes, use the break keyword. To move to the next iteration without executing the remaining code in the current iteration, use continue .
|
Example with break
:
for i in 1:10
if i == 5
break
end
println(i)
end
Output
1 2 3 4
Example with continue
:
for i in 1:5
if i == 3
continue
end
println(i)
end
Output
1 2 4 5
Terms and conditions
Conditional statements allow you to execute code depending on the fulfilment of certain conditions. Julia uses the if-elseif-else
construct:
x = 4
y = 10
if x < y
println("x is less than y")
elseif x > y
println("x is greater than y")
else
println("x is equal to y")
end
Output
x is less than y