One of the simplest first programming exercises to try in a new language
is the FizzBuzz problem.
It’s a simple toy problem that is less trivial than “Hello, World”
because it also involves using basic control-flow structures.
This blog post assumes that you’ve already installed the Julia programming language (hint: see GitHub)
and figured out how to open a REPL (hint: run julia
).
Now that you’ve got a Julia REPL open, let’s go through the very basic syntax of the language:
its control flow structures.
After we have those under control, you’ll write FizzBuzz in Julia.
If Statements
Julia uses the keyword end
to end blocks rather than using whitespace or curly braces ({}
) as delimeters.
Julia is not whitespace sensitive.
A simple if statement:
julia> if 5 == 4
print("hello world")
end
==
is the equality operator; it returns either true
or false
.
It is both an infix operator and a function.
The simplest possible if
-statements are:
if false
end
and
if true end
Julia is not whitespace sensitive;
you can sometimes put things all on one line without changing the meaning.
This is the case with these very simple if
-statements.
The tricky part to guess in the if
-statement syntax is the keyword elseif
,
since there’s so much variation in that keyword between languages.
julia> if 5 == 6 # this is a comment
print("five and six are equal")
elseif mod(5,6) == 4 # you will need the mod function later
print("five mod six is four")
else
print("Stefan is awesome")
end
Stefan is awesome
For Loops
The most straight-forward ways to write FizzBuzz involve for
-looping over a range of numbers,
so here’s an example of that style of loop and its output:
julia> for x=1:5
println(x)
end
1
2
3
4
5
Note that the range 1:5
is inclusive; both 1
and 5
appear in the output.
As you would expect, Julia also has while
loops:
julia> while false
print("something is very wrong")
end
Ranges
The 1:5
in the for
loop is a range.
Ranges are of the form start:end
.
As you can see, both the start and the end are inclusive. (both 1 and 5 are values in the range 1:5
)
By default, the range increments by 1.
julia> for x=1:2:10
println(x)
end
1
3
5
7
9
In this more complex form of range, you have start:step:end
.
The end value of the range does not have to be included; it is only included if incrementing by step
lands on it.
Exercise: FizzBuzz in REPL
Now that you have if
statements, for
loops, and the println
, mod
, and ==
functions
at your disposal, you can write FizzBuzz in the REPL.
It should take the form of a for
loop with an if
statement inside.
For those unfamiliar with it, FizzBuzz is a common coding problem that involves printing something
for each of the first 100 positive integers.
You should have 100 lines of output: one for each integer.
What you print depends on the number, as follows:
- If the number is divisible by 3, print ‘Fizz’.
- If the number is divisible by 5, print ‘Buzz’.
- If the number is divisible by both 3 and 5, print ‘FizzBuzz’.
- If none of the above apply, just print the number.
In case the above explanation is unclear, the correct output for 1:20
is:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Solution
Don’t peek before trying it yourself, but if you feel the need to see a correct solution,
you can find one here on RosettaCode.