# Control Flow

Figures provides a number of ways to control which calculations are executed and how often. These statements will be familiar to those who've used a programming language before, but they can also be very useful to anyone that wants to customize the app to their needs. If these concepts are new to you, don't worry. This document should act as a gentle introduction.

## If Statements

Sometimes you need different calculations to be run depending on the situation. As an example, let's say you're working on your budget. You'd like to donate to a charity, but only if your account is above a minimum amount. You can do this using an `if` statement:

``````account = \$200 - \$75 rent - \$20 utilities - \$40 food
if account > \$100 {
account -= \$10 for charity
}``````
``````\$65

``````

You'll see the word `if`, a condition, and then a block surrounded by `{` and `}`. When the condition is `true`, the calculations in the block are run. Otherwise they're ignored. In the example above, notice that there's no result for the charity donation. This is because the if statement was `false`, so the change to `account` inside the block is never run.

The last example had one condition, but you can use as many as you need. Just include each one separated by a comma. Let's say that you have a maximum amount you want to donate this year. You can add another condition to only donate if you haven't given that much yet.

``````account = \$280 - \$75 rent - \$20 utilities
total donation = \$50

if account > \$100, total donation < \$80 {
account -= \$10
total donation += \$10
}``````
``````\$185
\$50

\$175
\$60
``````

This time, both conditions were true so the calculations in the block were run. The results show the adjustment made for your donation.

NOTE

Each condition must return either `true` or `false`. Some programming languages allow the number one to mean true and zero to mean false. In Figures, this will generate an error.

What if you want to have one calculation if a condition is true and a different one if it's false? This is done using an `else` block. Maybe you always want to donate to a charity, but the amount depends on how much you have in your account:

``````account = \$200 - \$125 for monthly expenses

if account > \$100 {
account -= \$10
} else {
account -= \$5
}``````
``````\$75

\$70
``````

In the example, the condition is false so the else block was run. Notice that there is only a result next to the calculation inside the else block.

You can also chain together multiple if statements to consider different conditions. Perhaps you want even more fine-grained control over your monthly donation:

``````account = \$220 - \$50 for monthly expenses

if account > \$200 {
account -= \$20
} else if account > \$100 {
account -= \$10
} else {
account -= \$5
}``````
``````\$170

\$160

``````

Here, you'd donate \$20 if your account is flush with cash. If things are tighter you'd donate \$10, but you'll never donate less than \$5. Notice the result beside the calculation that matched the conditions.

## While Loops

A while loop is similar to an if statement. It begins by checking a condition. If the condition returns `true`, the calculations in its block are run. What's different is that once the block is complete, it checks the condition again and keeps going until the condition returns `false`.

Let's say you want to find the greatest common divisor of two integers (whole numbers). One easy way is known as Euclid's Algorithm:

``````a = 27; b = 6

while b > 0 {
r = a mod b
a = b
b = r
}
a``````
``````6

0
3
0

3``````

The details of the algorithm aren't important here. Just note that the value of `b` is changed each time the loop is executed. Only when `b` is reduced to zero does the while loop end.

NOTE

The results for calculations inside a loop show the result of the last time the loop executed. In the app, the result will also show the number of times that line was run.

Just like an if statement, a while loop can have more than one condition. Let's say you want to repeatedly apply a power to a number up to a certain limit:

``````base = 2
exponent = 2

while base < 20, exponent > 1 {
base = base ^ exponent
}``````
``````2
2

256
``````

Here the `while` loop will continue as long as `base` is less than twenty. If `exponent` was set to one, the `while` loop would continue forever so there's also a condition that `exponent` is greater than one.

NOTE

If a while loop's conditions always return `true`, you have created an infinite loop. Figures will continue running the loop over and over until you tell it to stop (or you change the calculation). To stop an infinite loop, click the square button in the bottom right of the results pane.

## For Loops

A for loop lets you repeat a set of calculations for each element in a sequence. A sequence is a series of values like a list or a range. Let's say you have a list of the current values of your investment accounts, and you'd like to estimate the total returns for all accounts.

``````investments = [\$8.5k, \$10k, \$3.3k]
total returns = \$0

for i in investments {
total returns += 5% of i
}``````
``````[\$8,500, \$10,000, \$3,300]
\$0

\$1,090
``````

Using the example, let's look at how to construct a for loop. First you assign a name to represent an element, placing it after the `for` keyword. In the example, the element is named `i`. Whenever `i` is referenced in the block, it's referring to the current investment being evaluated.

Next you give the sequence to be iterated (`investments` in the example). The for loop will run its block once for each element of the sequence.

The first time the block is executed, the value of `i` was \$8,500. The second time it was \$10,000, and so on. So the result of `total returns` is the sum of 5% of each investment.

Here's another example using a range as the sequence. In this example, we just want to add up all the elements of the range.

``````total = 0
for number in 3..8 {
total += number
}``````
``````0

33
``````

Here the sequence is the range of numbers from three to eight. The name of the element is `number`. The loop is run six times, once for each number in the range. During each loop the value of `number` is added to `total`. The final result is 3 + 4 + 5 + 6 + 7 + 8 which is 33.

## Breaking Out of a Loop

If you want to end the loop early, you can use a `break` statement. It immediately exits the innermost loop.

In this example, a range of numbers is subtracted from `value` as long as it doesn't go below zero. The loop will continue until `i` reaches five and `value` is zero. The `if` statement ensures that `value` never goes below zero.

``````value = 10
for i in 1..10 {
if i > value {
break
}
value -= i
}``````
``````10

0
``````

If you have one loop nested inside another, the `break` statement only exits the innermost one. Here's a nested loop that looks at each row in a grid, doubling the first value that's at least five. Notice that the `break` statement exits the column loop when it finds a match, but then continues with the next row.

``````grid = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]

for row in 1..3 {
for column in 1..3 {
if grid[row, column] >= 5 {
grid[row, column] ×= 2
break
}
}
}
grid
grid
grid``````
``````[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

14

[1, 2, 3]
[4, 10, 6]
[14, 8, 9]``````

## Restarting a Loop

When you need to go back to the start of a loop without running the rest of the block, use a `continue` statement. It will reset the innermost loop to the next iteration.

As an example, here's a loop that only adds even numbers in a range.

``````result = 0
for number in 1..10 {
if is odd(number) { continue }
result += number
}``````
``````0

30
``````

If you have one loop nested inside another, `continue` only restarts the innermost one. Here's a nested loop that doubles odd numbers in a grid. The `continue` statement restarts the column loop if the number is even.

``````grid = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
for row in 1..3 {
for column in 1..3 {
if is even(grid[row, column]) { continue }
grid[row, column] ×= 2
}
}
grid
grid
grid``````
``````[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

18

[2, 2, 6]
[4, 10, 6]
[14, 8, 18]``````

Didn't find what you were looking for?