Table of contents

## Perquisites

So far we have looked at Conditional Logic and using it to implement code in either an if, elif (else if) and else statement a single instance. We have also looked at for loops where one runs code for a specified number of times. It is also possible to set up a while loop, which will run code if and only if a certain condition has been met.

## Conditional Logic

The simplest conditional statement we can put together is False and True:

We can create a loop using these conditions.

Because the condition is False, the code in the loop will be skipped and will never be ran.

## Infinite Loop

On the other hand, while the condition is True, the loop will be ran and in this case as the condition is always going to be True the loop will run indefinitely, this is so called an infinite loop. This can be useful for instance if one is doing automated testing on a mechanical component for an indeterminate number of cycles to test its durability. We will demonstrate an infinite loop by writing a script which will spam your console, repeating the same text an infinite number of times.

In order to break out of an infinite loop, we will need to press [Ctrl] + [ c ].

## For Loop vs While Loop Example: Filling a Bath

A while loop can be used in place of a for loop in many cases let's give a visual example. Assume we have a bath that is 80 litres in size. Starting off with an empty bath, we want to make sure there is at least 60 litres of water in the bath. Assume we have a tap that is either on or off and has a flow rate of 3 litres per minute.

For a for loop, we could calculate the number of iterations or minutes required to fill the bath merely by dividing 60 litres by 3 litres per minute which gives 20 iterations:

time (minutes) = 1

water level (litres) = 3

time (minutes) = 2

water level (litres) = 6

time (minutes) = 3

water level (litres) = 9

time (minutes) = 4

water level (litres) = 12

time (minutes) = 5

water level (litres) = 15

time (minutes) = 6

water level (litres) = 18

time (minutes) = 7

water level (litres) = 21

time (minutes) = 8

water level (litres) = 24

time (minutes) = 9

water level (litres) = 27

time (minutes) = 10

water level (litres) = 30

time (minutes) = 11

water level (litres) = 33

time (minutes) = 12

water level (litres) = 36

time (minutes) = 13

water level (litres) = 39

time (minutes) = 14

water level (litres) = 42

time (minutes) = 15

water level (litres) = 45

time (minutes) = 16

water level (litres) = 48

time (minutes) = 17

water level (litres) = 51

time (minutes) = 18

water level (litres) = 54

time (minutes) = 19

water level (litres) = 57

time (minutes) = 20

water level (litres) = 60

Alternatively for a while loop we can loop until the condition of the bath having less than 60 litres of water is met.

water level (litres) = 3

time (minutes) = 1

water level (litres) = 6

time (minutes) = 2

water level (litres) = 9

time (minutes) = 3

water level (litres) = 12

time (minutes) = 4

water level (litres) = 15

time (minutes) = 5

water level (litres) = 18

time (minutes) = 6

water level (litres) = 21

time (minutes) = 7

water level (litres) = 24

time (minutes) = 8

water level (litres) = 27

time (minutes) = 9

water level (litres) = 30

time (minutes) = 10

water level (litres) = 33

time (minutes) = 11

water level (litres) = 36

time (minutes) = 12

water level (litres) = 39

time (minutes) = 13

water level (litres) = 42

time (minutes) = 14

water level (litres) = 45

time (minutes) = 15

water level (litres) = 48

time (minutes) = 16

water level (litres) = 51

time (minutes) = 17

water level (litres) = 54

time (minutes) = 18

water level (litres) = 57

time (minutes) = 19

water level (litres) = 60

time (minutes) = 20

As we can see from above, both results are identical, this is because before implementing the for loop, we calculated the condition the while loop would have ended at.

## While Loop containing if and else if: Maintaining Homeostasis

A while loop can be setup with an if and else if statement to maintain a stable, relatively constant internal environment known as homeostasis. We will continue with the bath analogy however let us assume that we want to have the bath always be full of fresh water and this bath hence has both a tap and a drain. First of all we start of with an empty bath. Once again the tap can either be on or off and the tap_rate is either 3 litres per minute (on) or 0 litres per minute (off). For convenience lets assume the drain can either be open or closed and water loss is either 1 litres per minute (open) or 0 litres per minute (closed).

The code will execute in a similar manner to the loop above however once the bath is filled, instead of only switching the tap off, the drain will be opened allowing water to escape. Once water has escaped and the water level is below a certain height, the drain will be closed and the tap will be opened until the water level is filled above a certain height. This will loop round forever maintaining a continuous water level in the bath.

time (minutes) = 1

water level (litres) = 3

time (minutes) = 2

water level (litres) = 6

time (minutes) = 3

water level (litres) = 9

time (minutes) = 4

water level (litres) = 12

time (minutes) = 5

water level (litres) = 15

time (minutes) = 6

water level (litres) = 18

time (minutes) = 7

water level (litres) = 21

time (minutes) = 8

water level (litres) = 24

time (minutes) = 9

water level (litres) = 27

time (minutes) = 10

water level (litres) = 30

time (minutes) = 11

water level (litres) = 33

time (minutes) = 12

water level (litres) = 36

time (minutes) = 13

water level (litres) = 39

time (minutes) = 14

water level (litres) = 42

time (minutes) = 15

water level (litres) = 45

time (minutes) = 16

water level (litres) = 48

time (minutes) = 17

water level (litres) = 51

time (minutes) = 18

water level (litres) = 54

time (minutes) = 19

water level (litres) = 57

time (minutes) = 20

water level (litres) = 60

time (minutes) = 21

water level (litres) = 63

time (minutes) = 22

water level (litres) = 66

time (minutes) = 23

water level (litres) = 69

time (minutes) = 24

water level (litres) = 72

time (minutes) = 25

water level (litres) = 71

time (minutes) = 26

water level (litres) = 70

time (minutes) = 27

water level (litres) = 69

time (minutes) = 28

water level (litres) = 68

time (minutes) = 29

water level (litres) = 67

time (minutes) = 30

water level (litres) = 66

time (minutes) = 31

water level (litres) = 65

time (minutes) = 32

water level (litres) = 64

time (minutes) = 33

water level (litres) = 63

time (minutes) = 34

water level (litres) = 62

time (minutes) = 35

water level (litres) = 61

time (minutes) = 36

water level (litres) = 60

time (minutes) = 37

water level (litres) = 59

time (minutes) = 38

water level (litres) = 62

time (minutes) = 39

water level (litres) = 65

time (minutes) = 40

water level (litres) = 68

.

.

.

Once again in order to break out of an infinite loop, we will need to press [Ctrl] + [ c ].

A large number of systems will use such a continuous feedback loop, for example the combination of a thermocouple and a thermometer may be used to maintain a temperature within a strict tolerance to a fish tank using a similar while loop to the above.