# For-Loop in Julia

For-loop is a type of loop, that iterates over an iterable object or simply a range of values. It executes some user-defined logic in each iteration.

## Content

1. Introduction to For-loop in Julia
2. Nested Loop
3. List comprehension in Julia
4. Break Statement in For-loop
5. Continue Statement in For-loop
6. Practice Exercise

## 1. Introduction to For-loop in Julia

For-loop is a control flow statement, used to repeat a logic multiple times while iterating over an iterable object like list or range.

In Julia, the for-loop statement follows the similar syntax to the for-loops seen in Python and R. The main difference is that, in Julia, an end statement is required in order to end the loop.

Below is the syntax :

#### Syntax of a for-loop

for i in list
# do something here for each i
end


Done with theory, Let’s see a simple example.

# Print the multiplication talbe of 9
list = 1:10
for i in list
println("9 * $i = ",9*i) end  # Output : 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81 9 * 10 = 90  It’s not mandatory to set the indexing variable beforehand. It can directly be used inside the conditional statement. # Print the multiplication talbe of 9 for i in 1:10 println("9 *$i = ",9*i)
end

# Output :
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90


You just saw very simple example of for-loop. While dealing with real projects, you would require multiple loops, loop within another loop, in order to perform operations. These kinds of multiple loops are called nested loops.

## 2. Nested loop

Nested loop means a loop statement that runs inside another loop statement. That is why nested loops are also called as “loop inside loop“

#### Syntax of a nested for-loop

for i in list1
for j in list2
# do something here for each i and j
end
end


Below is a simple example:

# Print every combination of array1 and array2
array1 = ["Machine Learning", "Artificial Intelligence", "Deep Learning"]
array2 = ["Statistics", "Julia Programming", "Domain Knowledge"]

for i in array1
for j in array2
println(i," - ",j)
end
end

# Output :
Machine Learning - Statistics
Machine Learning - Julia Programming
Machine Learning - Domain Knowledge
Artificial Intelligence - Statistics
Artificial Intelligence - Julia Programming
Artificial Intelligence - Domain Knowledge
Deep Learning - Statistics
Deep Learning - Julia Programming
Deep Learning - Domain Knowledge


Note: Since you have two loops, there should be as many end statements.

## 3. List Comprehension in Julia

You can achieve something similar using list comprehension in Julia. The syntax is similar to list comprehensions in Python.

out = ["$i -$j" for i in array1 for j in array2]
out



Output:

9-element Array{String,1}:
"Machine Learning -  Statistics"
"Machine Learning -  Julia Programming"
"Machine Learning -  Domain Knowledge"
"Artificial Intelligence -  Statistics"
"Artificial Intelligence -  Julia Programming"
"Artificial Intelligence -  Domain Knowledge"
"Deep Learning -  Statistics"
"Deep Learning -  Julia Programming"
"Deep Learning -  Domain Knowledge"



The difference here is, I am storing the result in out and not just printing it. While it is possible to run a println inside the square brackets, but if nothing is returned, it will still return a 9-element array object where each element is a ‘nothing’.

## 4. Break Statement

Break statement is a loop control statement, used to exit a for-loop before it has iterated through all the elements of list. Typically, it is used inside the if-else condition, to break out of the loop if the condition is met.

Let’s see an example.

# Print all the square numbers of 1 to 20, but make sure the squared numbers are less than 100
for i in 1:100
sq_num = i*i
if sq_num > 100
break
end
println(sq_num)
end

# Output :
1
4
9
16
25
36
49
64
81
100


Without the break keyword, the above for loop would iterate up to 100. This loop is exited early by using a break statement.

You have seen how to break a loop over some specific condition with the break statement.

What if you wish to skip only a specific iteration instead of exiting the loop altogether? Let’ see how to do that.

## 5. Continue Statement

Continue statement is a loop control statement, used to skip an iteration and move to the next one immediately.

Like the break statement, the continue is also typically placed inside an if-else condition.

When a continue statement is encountered inside a for-loop, all the statements that come after the continue is skipped and the loop proceeds to the next iteration.

Let me explain it with an example.

# Print all the multiples of 3 which are less than 10
for i in 1:10
if i % 3 != 0
continue
end
println(i)
end

# Output :
3
6
9


## 6. Practice Exercises

Q1. Take all the numbers from 0 to 100, make two lists, one containing all even numbers and other containing all odd numbers.

Show Solution
even_list = []
odd_list = []
for i in 1:100
if i % 2 == 0
push!(even_list, i)
else
push!(odd_list, i)
end
end


Q2. You are provided with a list of numbers. Iterate the for loop over the list. Print the numbers which are divisible by 7, stop the iteration if you find any number greater than 200

Show Solution
list_of_numbers = [20, 14, 7 ,12, 110, 120, 180, 140, 210, 34, 45, 60]
for i in list_of_numbers
if i > 200
break
end
if i % 7 ==0
println(i)
end
end

# Output :
14
7
140


## Conclusion

So, now you should have a fair idea of how to execute for-loops in Julia. Next, I will see you with more Data Science oriented topics in Julia.