|
| 1 | +--- |
| 2 | +title: 'Logic' |
| 3 | +teaching: 15 |
| 4 | +exercises: 15 |
| 5 | +--- |
| 6 | + |
| 7 | +::::::::::::::::::::::::::::::::::::: questions |
| 8 | + |
| 9 | +- How do we declare and assign values to variables? |
| 10 | + |
| 11 | +:::::::::::::::::::::::::::::::::::::::::::::::: |
| 12 | + |
| 13 | +::::::::::::::::::::::::::::::::::::: objectives |
| 14 | + |
| 15 | +- Understand the different intrinsic data types. |
| 16 | +- Declare and assign variables and parameters. |
| 17 | + |
| 18 | +:::::::::::::::::::::::::::::::::::::::::::::::: |
| 19 | + |
| 20 | +Fortran's `logical` type has two values: |
| 21 | + |
| 22 | +```fortran |
| 23 | + logical :: is_below_20_metres = .false. |
| 24 | + logical :: use_stochastic_physics = .true. |
| 25 | +``` |
| 26 | + |
| 27 | +### Logical operators |
| 28 | + |
| 29 | +Values can be tested logical operators `.or.`, `.and.` and `.not.` are available, and |
| 30 | +these can be used to set the value of logical variables. |
| 31 | + |
| 32 | +The precedence is illustrated by, e.g., |
| 33 | +```fortran |
| 34 | + q = i .or. j .and. .not. k ! evaluated as i .or. (j .and. (.not. k)) |
| 35 | +``` |
| 36 | +where q, i, j, and k are all logical variables. |
| 37 | + |
| 38 | +Use brackets to avoid confusion over operator precedence. |
| 39 | + |
| 40 | +### Relational operators |
| 41 | + |
| 42 | +To form logical expressions from numeric or other expressions, we require |
| 43 | +relational operators. The are two forms in Fortran, illustrated in the table |
| 44 | +below. It is recommended that you avoid the older form. |
| 45 | + |
| 46 | +| Relation | Operator | Older form | For | |
| 47 | +|--------------------------|----------|------------|------------------| |
| 48 | +| Less than | `< ` | `.lt.` | `integer` `real` | |
| 49 | +| Less than or equal to | `<=` | `.le.` | `integer` `real` | |
| 50 | +| Greater than | `> ` | `.gt.` | `integer` `real` | |
| 51 | +| Greater than or equal to | `>=` | `.ge.` | `integer` `real` | |
| 52 | +| Equal to | `==` | `.eq.` | `integer` `real` `complex`| |
| 53 | +| Not equal to | `/=` | `.neq.` | `integer` `real` `complex`| |
| 54 | + |
| 55 | +### Logical equivalence |
| 56 | + |
| 57 | +Equivalence between two logical expressions or variables is established |
| 58 | +via the logical operators `.eqv.` and `.neqv.`. |
| 59 | + |
| 60 | +While some some compilers may allow the use of `==`, this should be avoided. |
| 61 | + |
| 62 | +### Using logical operators |
| 63 | + |
| 64 | +These operators can be used to check and set the values of logical variables, dependent on other variables, e.g. |
| 65 | +```fortran |
| 66 | +program example4 |
| 67 | + implicit none |
| 68 | +
|
| 69 | + real, parameter :: pi = 3.14159265 |
| 70 | + logical, parameter :: switch1 = .true. |
| 71 | +
|
| 72 | + real :: a=3.0 |
| 73 | + logical :: test1, test2, test3 |
| 74 | +
|
| 75 | + test1 = a >= pi ! True if a is greater than or equal to pi |
| 76 | +
|
| 77 | + test2 = (.not. test1) ! True if test1 is False, False if test1 is True |
| 78 | +
|
| 79 | + test3 = (test2 .eqv. switch1) ! True if test2 is True, False if test2 is False |
| 80 | +
|
| 81 | + print *, test1 |
| 82 | +
|
| 83 | + print *, test2 |
| 84 | +
|
| 85 | + print *, test3 |
| 86 | +
|
| 87 | +end program example4 |
| 88 | +``` |
| 89 | +Compiling and running this code will give the following output |
| 90 | +``` |
| 91 | +$ ./a.out |
| 92 | + F |
| 93 | + T |
| 94 | + T |
| 95 | +``` |
| 96 | + |
| 97 | +:::::::::::::::::::::::::::::::::::::::: keypoints |
| 98 | + |
| 99 | +- Th |
| 100 | + |
| 101 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
0 commit comments