Tutorial by Examples

The while loop runs its body as long as the condition holds. For instance, the following code computes and prints the Collatz sequence from a given number: function collatz(n) while n ≠ 1 println(n) n = iseven(n) ? n ÷ 2 : 3n + 1 end println("1... and 4, 2, 1, ...
Sometimes, one wants to run some initialization code once before testing a condition. In certain other languages, this kind of loop has special do-while syntax. However, this syntax can be replaced with a regular while loop and break statement, so Julia does not have specialized do-while syntax. Ins...
0.5.0 (Although this example is written using syntax introduced in version v0.5, it can work with few modifications on older versions also.) This implementation of breadth-first search (BFS) on a graph represented with adjacency lists uses while loops and the return statement. The task we will sol...

Page 1 of 1