# Octave Programming Tutorial/Loops and conditions

Loops are used to repeat a block of code for a known or unknown number of times, depending on the type of loop. Using loops, you will draw some nice pictures of fractals and shapes drawn with random dots.

## The for loop

We use for loops to repeat a block of code for a list of known values. As an example, we'll calculate the mean of a list of values. The mean is calculated from
${\overline {x}}={\frac {1}{n}}\sum _{i=1}^{n}x_{i}.$
We set up a vector with some values
and calculate the mean with
Line 2: Set sum equal to 0. Line 3: For each value in x, assign it to entry. Line 4: Increment sum by entry. Line 5: Ends the for loop when there are no more members of x. Line 6: Assign the final value of sum divided by the length of x to x_mean.
TO DO: get a better example and explain the code.
In general, we write a for loop as
The ... represents the block of code that is executed exactly once for each value inside the vector.

### Example: The Sierpinski triangle

The Sierpinski triangle is a fractal that can be generated with a very simple algorithm.
* Start on a vertex of an equilateral triangle.
* Select a vertex of the triangle at random.
* Move to the point halfway between where you are now and the selected vertex.
* Repeat from step 2.
Plotting the points that you visit by following this procedure, generates the following picture.
The code that generates this fractal is shown bellow. Note that this code uses one very simple for loop to generate the fractal (for i = 1:N ; ... ; end)

#### A Word of Caution

For performance reasons, it's best to perform as few tasks as possible inside 'for' loops, and graphical operations like plot should be moved outside of loops whenever possible. By simply storing all x values in a matrix and then plotting as shown below, the run time for the code to produce this figure drops to a few seconds, even when iterating over 100,000 elements (which the code above warns not to do).
By initializing x as a matrix of the full final size ahead of time, this processing time can be reduced still further to only 1 or 2 seconds on modern hardware. In general, if a loop can be replaced with vectorization, it should be.

### Exercises

* Write a script that sums the first N integers. You can check your result with the formula ${\frac {1}{2}}N(N+1)$.
* Write a script that does the same thing as the linspace function. It should start at some value, xstart, stop at xstop and create a vector that contains N values evenly spaced from xstart to xstop. You can use the zeros function to create a zero-filled vector of the right size. Use help zeros to find out how the function works.

## The while loop

The while loop also executes a block of code more than once but stops based on a logical condition. For example
will multiply x by 2 until its value exceeds 1000. Here, x < 1000 is the condition of the loop. As long as the condition holds (is true), the loop will continue executing. As soon as it is false, the loop terminates and the first instruction after the loop is executed.
The general form of a while loop is

### Exercise

* Write a script that calculates the smallest positive integer, n, such that $a^{n}\geq b$ for some real numbers a and b. (Meaning, find the smallest power of a that is at least b.) Using the log function is considered cheating.

### Example: The Mandelbrot fractal

The Mandelbrot set is another fractal and is generated by checking how long it takes a complex number to become large. For each complex number, c,
* Start with $z_{0}=0$.
* Let $z_{i}=z_{i-1}^{2}+c\quad \forall i=1,2,\ldots$
* Find the first i such that $|z_{i}|>2$.
We record all of these i values and assign a colour to each of them. This is used to generate an image like this one.
You can download the code that generates this fractal from Mandelbrot.m. Note that there is a while loop (inside some for loops) that tests whether the complex number z has modulus less than 2:
The first condition in the while loop checks that we do not perform too many iterations. For some values of c the iteration will go on forever if we let it.
See also another version by Christopher Wellons

## The do...until statement

These loops are very similar to while loops in that they keep executing based on whether a given condition is true or false. There are however some important difference between while and do...until loops.
* while loops have their conditions at the beginning of the loop; do...until loops have theirs at the end.
* while loops repeat as long as the condition is true; do...until loops continue as long as theirs is false.
* while will execute 0 or more times (because the condition is at the beginning); do...until loops will execute 1 or more times (since the condition is at the end).
The general form of a do...until loop is

### Exercise

Write a script that calculates the greatest common divisor (GCD) of two positive integers. You can do this using Euclid's algorithm.

### Challenge

Write a script that generates random number pairs (a, b) that are distributed uniformly
* over the disc $\left\{(x,y)|x^{2}+y^{2}\leq 1\right\}$ (the first image below);
* as in the second image below

## The break and continue statements

Sometimes it is necessary to stop a loop somewhere in the middle of its execution or to move on to the next value in a for loop without executing the rest of the loop code for the current value. This is where the break and continue statements are useful.
The following code demonstrates how the break statement works.
Without the break statement, the loop would keep executing forever since the condition of the while loop is always true. The break allows you to jump past the end of the loop (to the statement after the endwhile).
The break statement can be used in any loop: for, while or do...until.
The continue statement also jumps from the inside of a loop but returns to the beginning of the loop rather than going to the end. In a
* for loop, the next value inside the vector will be assigned to the for variable (if there are any left) and the loop restarted with that value;
* while loop, the condition at the beginning of the loop will be retested and the loop continued if it is still true;
* do...until loop, the condition at the end of the loop will be tested and the loop continued from the beginning if it is still false.
As an example, the following code will fill the lower triangular part of a square matrix with 1s and the rest with 0s.
Note that the inner for skips (continues) over the code that assigns a 1 to an entry of A whenever the column index is greater than the row index.

## The if statement

The general form of the if statement is
If condition1 evaluates to true, the statements in the block immediately following the if are executed. If condition1 is false, the next condition (condition2 in the elseif) is checked and its statements executed if it is true. You can have as many elseif statements as you like. The final set of statements, after the else, is executed if all of the conditions evaluate to false. Note that the elseif and else parts of the if statement are optional.
The following are all valid if statements:

### Example: The fractal fern

This algorithm is not quite complete. Have a look at the .m file available from http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=4372&objectType=file.
The image to the right can be generated with the following algorithm:
You can download the code that generates this fractal as fracfern.m (this is disabled for now).
Return to the Octave Programming tutorial index