# Introducing Julia/Controlling the flow

## Contents

- 1 Different ways to control the flow
- 2 Ternary expressions
- 3 Boolean switching expressions
- 4 If and Else
- 5 For loops and iteration
- 5.1 Iterating over an array and updating it
- 5.2 Loop variables and scope
- 5.3 Variables declared inside a loop
- 5.4 Fine tuning the loop: Continue
- 5.5 Comprehensions
- 5.6 Enumerating arrays
- 5.7 Zipping arrays
- 5.8 Iterable objects
- 5.9 Even more iterators
- 5.10 Making your own iterable objects
- 5.11 Nested loops
- 5.12 Optimizing nested loops

- 6 While loops
- 7 Exceptions
- 8 Do block

### Different ways to control the flow[edit]

Typically each line of a Julia program is evaluated in turn. There are various ways to control and modify the flow of evaluation. These correspond with the constructs used in other languages:

**ternary**and**compound**expressions**Boolean**switching expressions**if elseif else end**- conditional evaluation**for end**- iterative evaluation**while end**- iterative conditional evaluation**try catch error throw**exception handling**do**blocks

### Ternary expressions[edit]

Often you'll want to do job A (or call function A) if some condition is true, or job B (function B) if it isn't. The quickest way to write this is using the ternary operator ("?" and ":"):

```
julia> x = 1
1
julia> x > 3 ? "yes" : "no"
"no"
julia> x = 5
5
julia> x > 3 ? "yes" : "no"
"yes"
```

Here's another example:

```
julia> x = 0.3
0.3
julia> x < 0.5 ? sin(x) : cos(x)
0.29552020666133955
```

and Julia returned the value of `sin(x)`

, because x was less than 0.5. `cos(x)`

wasn't evaluated at all.

### Boolean switching expressions[edit]

Boolean operators let you evaluate an expression if a condition is true. You can combine the condition and expression using `&&`

or `||`

. `&&`

means "and", and `||`

means "or". Since Julia evaluates expressions one by one, you can easily arrange for an expression to be evaluated only if a previous condition is true or false.

The following example uses a Julia function that returns true or false depending on whether the number is odd: `isodd(n)`

.

With `&&`

, both parts have to be true, so we can write this:

```
julia> isodd(1000003) && @warn("That's odd!")
WARNING: That's odd!
julia> isodd(1000004) && @warn("That's odd!")
false
```

If the first condition (number is odd) is true, the second expression is evaluated. If the first isn't true, the expression isn't evaluated, and just the condition is returned.

With the `||`

operator, on the other hand:

```
julia> isodd(1000003) || @warn("That's odd!")
true
julia> isodd(1000004) || @warn("That's odd!")
WARNING: That's odd!
```

If the first condition is true, there's no need to evaluate the second expression, since we already have the one truth value we need for "or", and it returns the value true. If the first condition is false, the second expression is evaluated, because that one might turn out to be true.

This type of evaluation is also called "short-circuit evaluation".

### If and Else[edit]

For a more general — and traditional — approach to conditional execution, you can use `if`

, `elseif`

, and `else`

. If you're used to other languages, don't worry about white space, braces, indentation, brackets, semicolons, or anything like that, but remember to finish the conditional construction with `end`

.

```
julia> name = "Julia"
if name == "Julia"
println("I like Julia")
elseif name == "Python"
println("I like Python.")
println("But I prefer Julia.")
else
println("I don't know what I like")
end
```

The `elseif`

and `else`

parts are optional too:

```
julia> name = "Julia"
if name == "Julia"
println("I like Julia")
end
```

Just don't forget the `end`

!

How about 'switch' and 'case' statements? Well, you don't have to learn the syntax for those, because they don't exist!

#### ifelse[edit]

There's an `ifelse`

function, too. It looks like this in action:

```
julia> s = ifelse(false, "hello", "goodbye") * " world"
```

`ifelse`

is an ordinary function, which evaluates all the arguments, and returns the second or third, depending on the value of the first. With the conditional `if`

or `? ... :`

, only the expressions in the chosen route are evaluated.

### For loops and iteration[edit]

Working through a list or a set of values or from a start value to a finish value are all examples of iteration, and the `for`

... `end`

construction can let you iterate through a number of different types of object, including ranges, arrays, sets, dictionaries, and strings.

Here's the standard syntax for a simple iteration through a range of values:

```
julia> for i in 0:10:100
println(i)
end
0
10
20
30
40
50
60
70
80
90
100
```

The variable `i`

takes the value of each element in the array (which is built from a range object) in turn — here stepping from 0 to 100 in steps of 10.

```
julia> for color in ["red", "green", "blue"] # an array
print(color, " ")
end
red green blue
```

```
julia> for letter in "julia" # a string
print(letter, " ")
end
j u l i a
```

```
julia> for element in (1, 2, 4, 8, 16, 32) # a tuple
print(element, " ")
end
1 2 4 8 16 32
```

```
julia> for i in Dict("A"=>1, "B"=>2) # a dictionary
println(i)
end
"B"=>2
"A"=>1
```

```
julia> for i in Set(["a", "e", "a", "e", "i", "o", "i", "o", "u"])
println(i)
end
e
o
u
a
i
```

We haven't yet met sets and dictionaries, but iterating through them is exactly the same.

You can iterate through a 2D array, stepping "down" through column 1 from top to bottom, then through column 2, and so on:

```
julia> a = reshape(1:100, (10, 10))
10x10 Array{Int64,2}:
1 11 21 31 41 51 61 71 81 91
2 12 22 32 42 52 62 72 82 92
3 13 23 33 43 53 63 73 83 93
4 14 24 34 44 54 64 74 84 94
5 15 25 35 45 55 65 75 85 95
6 16 26 36 46 56 66 76 86 96
7 17 27 37 47 57 67 77 87 97
8 18 28 38 48 58 68 78 88 98
9 19 29 39 49 59 69 79 89 99
10 20 30 40 50 60 70 80 90 100
julia> for n in a
print(n, " ")
end
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
```

You can use `=`

instead of `in`

.

##### Iterating over an array and updating it[edit]

When you're iterating over an array, the array is checked each time through the loop, in case it's changed. A mistake you should avoid making is to use `push!`

to make an array grow in the middle of a loop. Run the following text carefully, and be ready to `Ctrl-C`

when you've seen enough (otherwise your computer will eventually crash):

```
julia> c = [1]
1-element Array{Int64,1}:
1
julia> for i in c
push!(c, i)
@show c
sleep(1)
end
c = [1,1]
c = [1,1,1]
c = [1,1,1,1]
...
```

#### Loop variables and scope[edit]

The variable that steps through each item — the 'loop variable' will disappear as soon as the loop finishes.

julia>for i in 1:10@show iendi = 1 i = 2 i = 3 i = 4 i = 5 i = 6 i = 7 i = 8 i = 9 i = 10 julia>iERROR: UndefVarError: i not defined

If you want to remember the value of the loop variable outside the loop (eg if you had to exit the loop and needed to know the value you'd reached), use the `global`

keyword to define a variable that outlasts the loop.

julia>for i in 1:10global howfarif i % 4 == 0howfar = iendend

julia>howfar8

Here, `howfar`

didn't exist before the loop, but it survived to tell its story.

#### Variables declared inside a loop[edit]

In a similar way, if you declare a new variable inside a loop, it won't exist once the loop finishes. In this example, `k`

is created inside:

julia>for i in 1:5k = i^2println("$(i) squared is $(k)")end

1 squared is 1 2 squared is 4 3 squared is 9 4 squared is 16 5 squared is 25

and doesn't exist immediately after the loop finishes:

julia>kERROR: UndefVarError: k not defined

Variables created inside a loop are forgotten at the end of each iteration. Look at the following loop:

```
for i in 1:10
if ! @isdefined z
println("z isn't defined")
end
z = i
println("z is $z")
end
```

Perhaps you expected only the first loop to produce the "z isn't defined error"? In fact, even if `z`

is created in the body of the loop, it is undefined at the start of the next iteration.

```
z isn't defined
z is 1
z isn't defined
z is 2
z isn't defined
z is 3
z isn't defined
z is 4
z isn't defined
z is 5
z isn't defined
z is 6
z isn't defined
z is 7
z isn't defined
z is 8
z isn't defined
z is 9
z isn't defined
z is 10
```

You can use the `global`

keyword to force `z`

to be available outside the loop once it's been created:

```
for i in 1:10
global z
if ! @isdefined z
println("z isn't defined")
else
println("z was $z")
end
z = i
println("z is $z")
end
```

```
z isn't defined
z is 1
z was 1
z is 2
z was 2
...
z is 9
z was 9
z is 10
```

although, if you're working in global scope, `z`

is now available everywhere, with the value 10.

Better practice is to contain variables inside functions:

```
function test()
for i in 1:10
local z = i
if ! @isdefined z
println("z isn't defined")
else
println("z was $z")
end
z = i
println("z is $z")
end
end
```

julia>test()

```
z was 1
z is 1
z was 2
z is 2
...z was 10
z is 10
```

and now `z`

will be available inside the function and the loop. This `z`

will have no connection with any similarly named variables outside the function.

#### Fine tuning the loop: Continue[edit]

Sometimes on a particular iteration you might want to skip to the next value. You can use `continue`

to skip the rest of the code inside the loop and start the loop again with the next value.

```
julia> for i in 1:10
if i % 3 == 0
continue
end
println(i) # this and subsequent lines are
# skipped if i is a multiple of 3
end
1
2
4
5
7
8
10
```

#### Comprehensions[edit]

This oddly-named concept is simply a way of generating and collecting items. In mathematical circles you would say something like this:

"Let S be the set of all elements n where n is greater than or equal to 1 and less than or equal to 10".

In Julia, you can write this as:

```
julia> S = Set([n for n in 1:10])
Set([7,4,9,10,2,3,5,8,6,1])
```

and the `[n for n in 1:10]`

construction is called **array comprehension** or **list comprehension** ('comprehension' in the sense of 'getting everything' rather than 'understanding'). The outer brackets collect together the elements generated by evaluating the expression placed before the `for`

iteration. Instead of `end`

, use a square bracket to finish.

```
julia> [i^2 for i in 1:10]
10-element Array{Int64,1}:
1
4
9
16
25
36
49
64
81
100
```

The type of elements can be specified:

```
julia> Complex[i^2 for i in 1:10]
10-element Array{Complex,1}:
1.0+0.0im
4.0+0.0im
9.0+0.0im
16.0+0.0im
25.0+0.0im
36.0+0.0im
49.0+0.0im
64.0+0.0im
81.0+0.0im
100.0+0.0im
```

But Julia can work out the types of the results you're producing:

```
julia> [(i, sqrt(i)) for i in 1:10]
10-element Array{Tuple{Int64,Float64},1}:
(1,1.0)
(2,1.41421)
(3,1.73205)
(4,2.0)
(5,2.23607)
(6,2.44949)
(7,2.64575)
(8,2.82843)
(9,3.0)
(10,3.16228)
```

Here's how to make a dictionary via comprehension:

```
julia> Dict(string(Char(i + 64)) => i for i in 1:26)
Dict{String,Int64} with 26 entries:
"Z" => 26
"Q" => 17
"W" => 23
"T" => 20
"C" => 3
"P" => 16
"V" => 22
"L" => 12
"O" => 15
"B" => 2
"M" => 13
"N" => 14
"H" => 8
"A" => 1
"X" => 24
"D" => 4
"G" => 7
"E" => 5
"Y" => 25
"I" => 9
"J" => 10
"S" => 19
"U" => 21
"K" => 11
"R" => 18
"F" => 6
```

Next, here are two iterators in a comprehension, separated with a comma, which makes generating tables very easy. Here we're making a tuple-table:

```
julia> [(r,c) for r in 1:5, c in 1:2]
5×2 Array{Tuple{Int64,Int64},2}:
(1,1) (1,2)
(2,1) (2,2)
(3,1) (3,2)
(4,1) (4,2)
(5,1) (5,2)
```

`r`

goes through five cycles, one cycle for every value of `c`

. Nested loops work in the opposite manner. Here the column-major order is respected, as shown when the array is filled with nanosecond time values:

```
julia> [Int(time_ns()) for r in 1:5, c in 1:2]
5×2 Array{Int64,2}:
1223184391741562 1223184391742642
1223184391741885 1223184391742817
1223184391742067 1223184391743009
1223184391742256 1223184391743184
1223184391742443 1223184391743372
```

You can supply a test expression as well to filter the production. For example, produce all the integers between 1 and 100 that are exactly divisible by 7:

julia> [x for x in 1:100 if x % 7 == 0] 14-element Array{Int64,1}: 7 14 21 28 35 42 49 56 63 70 77 84 91 98

##### Generator expressions[edit]

Like comprehensions, generator expressions can be used to produce values from iterating a variable, but, unlike comprehensions, the values are produced on demand.

julia> sum(x^2 for x in 1:10) 385

julia> collect(x for x in 1:100 if x % 7 == 0) 14-element Array{Int64,1}: 7 14 21 28 35 42 49 56 63 70 77 84 91 98

#### Enumerating arrays[edit]

Often you want to go through an array element by element while also keeping track of the index number of each element. The `enumerate()`

function gives you an **iterable** version of something, producing both an index number and the value at each index number:

```
julia> m = rand(0:9, 3, 3)
3×3 Array{Int64,2}:
6 5 3
4 0 7
1 7 4
julia> [i for i in enumerate(m)]
3×3 Array{Tuple{Int64,Int64},2}:
(1, 6) (4, 5) (7, 3)
(2, 4) (5, 0) (8, 7)
(3, 1) (6, 7) (9, 4)
```

The array is checked for possible changes at each iteration of the loop.

#### Zipping arrays[edit]

Sometimes you want to work through two or more arrays at the same time, taking the first element of each array first, then the second, and so on. This is possible using the well-named `zip()`

function:

```
julia> for i in zip(0:10, 100:110, 200:210)
println(i)
end
(0,100,200)
(1,101,201)
(2,102,202)
(3,103,203)
(4,104,204)
(5,105,205)
(6,106,206)
(7,107,207)
(8,108,208)
(9,109,209)
(10,110,210)
```

You'd think it would all go wrong if the arrays were different sizes. What if the third array is too big, or too small?

```
julia> for i in zip(0:10, 100:110, 200:215)
println(i)
end
(0,100,200)
(1,101,201)
(2,102,202)
(3,103,203)
(4,104,204)
(5,105,205)
(6,106,206)
(7,107,207)
(8,108,208)
(9,109,209)
(10,110,210)
```

but Julia isn't fooled — any oversupply or undersupply in any one of the arrays is handled gracefully.

```
julia> for i in zip(0:15, 100:110, 200:210)
println(i)
end
(0,100,200)
(1,101,201)
(2,102,202)
(3,103,203)
(4,104,204)
(5,105,205)
(6,106,206)
(7,107,207)
(8,108,208)
(9,109,209)
(10,110,210)
```

#### Iterable objects[edit]

The "for something in something" construction is the same for everything that you can iterate through: arrays, dictionaries, strings, sets, ranges, and so on. In Julia this is a general principle: there are a number of ways in which you can create an "iterable object", an object that is designed to be used as part of the iteration process that provides the elements one at a time.

The most obvious example we've already met is the range object. It doesn't look much when you type it into the REPL:

```
julia> ro = 0:2:100
0:2:100
```

But it yields the numbers on demand, when you start iterating:

```
julia> [i for i in ro]
51-element Array{Int64,1}:
0
2
4
6
8
10
12
14
16
18
20
22
24
26
28
⋮
74
76
78
80
82
84
86
88
90
92
94
96
98
100
```

Should you want the actual numbers from a range (or other iterable object), you can use `collect()`

:

```
julia> collect(0:25:100)
5-element Array{Int64,1}:
0
25
50
75
100
```

This might be helpful when you have iterable objects created by other Julia functions. For example, `permutations()`

creates an iterable object containing all the permutations of an array. You could use `collect()`

to grab them and make a new array:

```
julia> collect(permutations(1:4))
24-element Array{Array{Int64,1},1}:
[1,2,3,4]
[1,2,4,3]
…
[4,3,2,1]
```

but on anything large there are going to hundreds of permutations. And that's the reason why iterator objects don't produce all the values from the iteration at the same time: memory and performance. A range object doesn't take up much room, even if iterating over it might take ages, depending on how big the range is. If you generate all the numbers at once, rather than only producing them when they're needed, they all have to be stored somewhere until you need them…

Julia provides iterable objects for working with other types of data. For example, when you're working with files, you can treat an open file as an iterable object:

```
filehandle = "/Users/me/.julia/logs/repl_history.jl"
for line in eachline(filehandle)
println(length(line), line)
end
```

##### Use eachindex()[edit]

A common pattern when iterating through arrays is to perform some task for each value of `i`

, where `i`

is the index number of each element, not the element:

```
for i = 1:length(A)
# do something with i or A[i]
end
```

There is a more efficient (i.e. faster in some situations) way to do this:

```
for i in eachindex(A)
# do something with i or A[i]
end
```

#### Even more iterators[edit]

There's a Julia package called Iterators.jl that provides some advanced iterator functions.

```
julia> using Iterators
```

For example, `partition()`

groups the objects in the iterator into easily-handled chunks:

```
collect(partition(1:10, 3, 1))
8-element Array{Tuple{Int64,Int64,Int64},1}:
(1, 2, 3)
(2, 3, 4)
(3, 4, 5)
(4, 5, 6)
(5, 6, 7)
(6, 7, 8)
(7, 8, 9)
(8, 9, 10)
```

`chain()`

works through all the iterators one after the other:

```
for i in chain(1:3, ['a', 'b', 'c'])
@show i
end
i = 1
i = 2
i = 3
i = 'a'
i = 'b'
i = 'c'
```

`subsets()`

works through all subsets of an object. You can specify a size:

```
for i in subsets(collect(1:6), 3)
@show i
end
i = [1,2,3]
i = [1,2,4]
i = [1,2,5]
i = [1,2,6]
i = [1,3,4]
i = [1,3,5]
i = [1,3,6]
i = [1,4,5]
i = [1,4,6]
i = [1,5,6]
i = [2,3,4]
i = [2,3,5]
i = [2,3,6]
i = [2,4,5]
i = [2,4,6]
i = [2,5,6]
i = [3,4,5]
i = [3,4,6]
i = [3,5,6]
i = [4,5,6]
```

#### Making your own iterable objects[edit]

It's possible to design your own iterable objects. When you're defining your type, you add a couple of methods to Julia's `iterate()`

function. Then you can use something like `for`

.. `end`

loop to work through the components of your object, and these `iterate()`

methods are called automatically as necessary.

The following example shows how you can create an iterable object that iterates through prime numbers, starting at 1, going up to a certain value.

(The `isprime()`

function lives in an external package called Primes, so before starting this section, you must add it and import it.)

```
using Primes
```

First, you define a new type that will be a prime iterator:

```
struct PrimeIterator
n::Integer
end
```

You will later create an iterable object of this type using something like this:

```
primesupto20 = PrimeIterator(20)
```

The number you supply is passed to the type constructor, and stored in `n`

. The iterator will be expected to generate every prime up to this number but no further.

So to define what happens when you try to iterate this type, you must add two methods to the `iterate()`

function. This function already exists in Julia (that's why you can iterate over all the basic data objects), so the `Base`

prefix is required: you're adding a new method to the existing `iterate()`

function, one which is designed to handle these special objects.

The first method takes no arguments, except for the type, and is for starting the iteration process off.

```
function Base.iterate(::PrimeIterator)
return (1, 2)
end
```

It returns a tuple: the first value, and the next state of the iterator.

Next, you add a `iterate(type, state)`

method to Julia's `iterate()`

function. This takes two arguments, an iterable object and the current state, and returns a tuple of two values, the next item and the next state. But, if there are no more values available, the `iterate()`

function should return nothing.

```
function Base.iterate(piter::PrimeIterator, state)
if state >= piter.n
return
end
while ! isprime(state)
state += 1
end
return (state, state += 1)
end
```

Telling the iterator when it's finished is easy in this case, because the construction of the PrimeIterator object expected a number, which acts as the stop point. And, because this number was called `n`

in the type definition, it can be accessed using the "." field accessor. This is the only time you refer to the original number which was passed to the constructor.

With these two methods added for the PrimeIterator type, it's now possible to iterate through our prime iterator objects, eg from 1 to 20:

```
primesupto20 = PrimeIterator(20)
for i in primesupto20
println(i)
end
1
2
3
5
7
11
13
17
19
```

As you add more methods that handle PrimeIterator objects, you can use more and more Julia functions with it. For example, if you add a `length`

method:

```
function Base.length(piter::PrimeIterator)
return piter.n
end
```

then Julia's `length()`

function knows how long PrimeIterators are:

```
julia> length(primesupto20)
20
```

For some types (but perhaps not so easy for prime numbers), you can add a `getindex()`

method to Julia's `getindex()`

function. This allows you to make use of the array indexing notation to find the **nth** prime.

We'll cheat a bit by using Primes.jl to calculate the primes.

```
function Base.getindex(pi::PrimeIterator, i::Int)
1 <= i < length(pi) || throw(BoundsError(pi, i))
i == 1 && return 1
return primes(pi.n)[i-1]
end
```

This lets us do this:

```
julia> primesupto20[1]
1
julia> primesupto20[2]
2
julia> primesupto20[3]
3
julia> primesupto20[8]
17
julia> primesupto20[9]
19
```

(Code inspired by Carl Vogel posts at [1].)

#### Nested loops[edit]

If you want to nest one loop inside another, you don't have to duplicate the `for`

and `end`

keywords. Just use a comma:

```
julia> for x in 1:10, y in 1:10
@show (x, y)
end
(x,y) = (1,1)
(x,y) = (1,2)
(x,y) = (1,3)
(x,y) = (1,4)
(x,y) = (1,5)
(x,y) = (1,6)
(x,y) = (1,7)
(x,y) = (1,8)
(x,y) = (1,9)
(x,y) = (1,10)
(x,y) = (2,1)
(x,y) = (2,2)
(x,y) = (2,3)
(x,y) = (2,4)
(x,y) = (2,5)
(x,y) = (2,6)
(x,y) = (2,7)
(x,y) = (2,8)
(x,y) = (2,9)
(x,y) = (2,10)
(x,y) = (3,1)
(x,y) = (3,2)
...
(x,y) = (9,9)
(x,y) = (9,10)
(x,y) = (10,1)
(x,y) = (10,2)
(x,y) = (10,3)
(x,y) = (10,4)
(x,y) = (10,5)
(x,y) = (10,6)
(x,y) = (10,7)
(x,y) = (10,8)
(x,y) = (10,9)
(x,y) = (10,10)
```

(The useful `@show`

macro prints out the names of things and their values.)

One difference between the shorter and longer forms of nesting loops is the behaviour of `break`

:

```
julia> for x in 1:10
for y in 1:10
@show (x, y)
if y % 3 == 0
break
end
end
end
(x,y) = (1,1)
(x,y) = (1,2)
(x,y) = (1,3)
(x,y) = (2,1)
(x,y) = (2,2)
(x,y) = (2,3)
(x,y) = (3,1)
(x,y) = (3,2)
(x,y) = (3,3)
(x,y) = (4,1)
(x,y) = (4,2)
(x,y) = (4,3)
(x,y) = (5,1)
(x,y) = (5,2)
(x,y) = (5,3)
(x,y) = (6,1)
(x,y) = (6,2)
(x,y) = (6,3)
(x,y) = (7,1)
(x,y) = (7,2)
(x,y) = (7,3)
(x,y) = (8,1)
(x,y) = (8,2)
(x,y) = (8,3)
(x,y) = (9,1)
(x,y) = (9,2)
(x,y) = (9,3)
(x,y) = (10,1)
(x,y) = (10,2)
(x,y) = (10,3)
julia> for x in 1:10, y in 1:10
@show (x, y)
if y % 3 == 0
break
end
end
(x,y) = (1,1)
(x,y) = (1,2)
(x,y) = (1,3)
```

Notice that `break`

breaks out of both inner and outer loops in the shorter form, but only out of the inner loop in the longer form.

#### Optimizing nested loops[edit]

With Julia, the inner loop should concern rows rather than columns. This is due to how arrays are stored in memory.

The following examples consist of simple loops, but the way the rows and columns are iterated differ. The "bad" version jumps across from column to column, which means calculating the jump each time. In other words, it looks at every column of a row, then at every column of the next row, and so on:

```
function laplacian_bad(lap_x::Array{Float64,2}, x::Array{Float64,2})
nr,nc = size(x)
for ir = 2:nr-1, ic = 2:nc-1 # bad loop nesting order
lap_x[ir,ic] =
(x[ir+1,ic] + x[ir-1,ic] +
x[ir,ic+1] + x[ir,ic-1]) - 4*x[ir,ic]
end
end
```

In the "good" version, the two loops are nested properly:

```
function laplacian_good(lap_x::Array{Float64,2}, x::Array{Float64,2})
nr,nc = size(x)
for ic = 2:nc-1, ir = 2:nr-1 # good loop nesting order
lap_x[ir,ic] =
(x[ir+1,ic] + x[ir-1,ic] +
x[ir,ic+1] + x[ir,ic-1]) - 4*x[ir,ic]
end
end
```

Another way to increase the speed is to remove the array bounds checking, using the macro `@inbounds`

:

```
function laplacian_good_nocheck(lap_x::Array{Float64,2}, x::Array{Float64,2})
nr,nc = size(x)
for ic = 2:nc-1, ir = 2:nr-1 # good loop nesting order
@inbounds begin lap_x[ir,ic] = # no array bounds checking
(x[ir+1,ic] + x[ir-1,ic] +
x[ir,ic+1] + x[ir,ic-1]) - 4*x[ir,ic]
end
end
end
```

Here's the test function:

```
function main_test(nr, nc)
field = zeros(nr, nc)
for ic = 1:nc, ir = 1:nr
if ir == 1 || ic == 1 || ir == nr || ic == nc
field[ir,ic] = 1.0
end
end
lap_field = zeros(size(field))
time = @elapsed laplacian_bad(lap_field, field)
@printf "laplacian_bad: %.3f s\n" time
time = @elapsed laplacian_good(lap_field, field)
@printf "laplacian_good: %.3f s\n" time
time = @elapsed laplacian_good_nocheck(lap_field, field)
@printf "laplacian_good_nocheck: %.3f s\n" time
end
```

and the results show the difference in performance just based on the row/column scanning order. The "no check" version is even faster....

julia> main_test(10000,10000) laplace_bad: 3.209 s laplace_good: 0.436 s laplace_good_nockeck: 0.157 s

### While loops[edit]

To repeat some expressions while a condition is true, use the `while`

... `end`

construction.

```
julia> x = 0
0
julia> while x < 4
println(x)
x += 1
end
0
1
2
3
```

If you want the condition to be tested after the statements, rather than before, producing a "do .. until" form, use the following construction:

```
while true
println(x)
x += 1
x >= 4 && break
end
0
1
2
3
```

Here we're using a Boolean switch rather than an `if`

... `end`

statement.

Using Julia's macros, you can create your own control structures. See Metaprogramming.

### Exceptions[edit]

If you want to write code that checks for errors and handles them gracefully, use the `try`

... `catch`

construction.

With a `catch`

phrase, you can handle problems that occur in your code, possibly allowing the program to continue rather than grind to a halt.

In the next example, our code attempts to change the first character of a string directly (which isn't allowed, because strings in Julia can't be modified in place):

```
julia> s = "string";
julia> try
s[1] = "p"
catch e
println("caught an error $e")
println("but we can continue with execution")
end
caught an error <code>MethodError(setindex!,("string","p",1))</code> but we can continue with execution
```

The `error()`

function raises an error exception with a given message.

### Do block[edit]

Finally, let's look at a `do`

block, which is another syntax form that, like the list comprehension, looks at first sight to be a bit backwards (i.e. it can perhaps be better understood by starting at the end and working towards the beginning).

Remember the `find()`

example from earlier?

```
julia> smallprimes = [1,2,3,5,7,11,13,17,19,23];
julia> findall(x -> isequal(13, x), smallprimes)
1-element Array{Int64,1}:
7
```

The anonymous function (`x -> x isequal(13, x)`

) is the first argument of `find()`

, and operates on the second. But with a `do`

block, you can lift the function out and put it in between a `do ... end`

block construction:

```
julia> findall(smallprimes) do x
isequal(x, 13)
end
1-element Array{Int64,1}:
7
```

You just lose the arrow and change the order, putting the `find()`

function and its target argument first, then adding the anonymous function's arguments and body after the `do`

.

The idea is that it's easier to write a longer anonymous function on multiple lines at the end of the form, rather than wedged in as the first argument.