microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
samples/language/ComparisonOperators.qs
92lines · modecode
| 1 | /// # Sample |
| 2 | /// Comparison Operators |
| 3 | /// |
| 4 | /// # Description |
| 5 | /// Comparison operators in Q# are used to compare one value relative to |
| 6 | /// another value of the same type, producing an output Boolean value. |
| 7 | /// The comparison operators in Q# are `==`, `!=`, `<`, `<=`, `>`, and `>=`. |
| 8 | namespace MyQuantumApp { |
| 9 | |
| 10 | @EntryPoint() |
| 11 | function Main() : Unit { |
| 12 | |
| 13 | // `==` tests if the first value is equivalent to the second value. |
| 14 | |
| 15 | // The Boolean value `true`. |
| 16 | let boolean = 4 == 4; |
| 17 | Message($"Equality comparison: {boolean}"); |
| 18 | |
| 19 | // The Boolean value `false`. |
| 20 | let boolean = 4 == 6; |
| 21 | Message($"Equality comparison: {boolean}"); |
| 22 | |
| 23 | // `!=` tests if the first value is not equivalent to the second value. |
| 24 | // It is the opposite of `==`. |
| 25 | |
| 26 | // The Boolean value `false`. |
| 27 | let boolean = 4 != 4; |
| 28 | Message($"Inequality comparison: {boolean}"); |
| 29 | |
| 30 | // The Boolean value `true`. |
| 31 | let boolean = 4 != 6; |
| 32 | Message($"Inequality comparison: {boolean}"); |
| 33 | |
| 34 | // `<` tests if the first value is strictly less than the second value. |
| 35 | |
| 36 | // The Boolean value `false`. |
| 37 | let boolean = 4 < 4; |
| 38 | Message($"Less than comparison: {boolean}"); |
| 39 | |
| 40 | // The Boolean value `true`. |
| 41 | let boolean = 4 < 6; |
| 42 | Message($"Less than comparison: {boolean}"); |
| 43 | |
| 44 | // The Boolean value `false`. |
| 45 | let boolean = 6 < 4; |
| 46 | Message($"Less than comparison: {boolean}"); |
| 47 | |
| 48 | // `<=` tests if the first value is less than or equivalent to |
| 49 | // the second value. |
| 50 | |
| 51 | // The Boolean value `true`. |
| 52 | let boolean = 4 <= 4; |
| 53 | Message($"Less than or equal comparison: {boolean}"); |
| 54 | |
| 55 | // The Boolean value `true`. |
| 56 | let boolean = 4 <= 6; |
| 57 | Message($"Less than or equal comparison: {boolean}"); |
| 58 | |
| 59 | // The Boolean value `false`. |
| 60 | let boolean = 6 <= 4; |
| 61 | Message($"Less than or equal comparison: {boolean}"); |
| 62 | |
| 63 | // `>` tests if the first value is strictly greater than the second value. |
| 64 | |
| 65 | // The Boolean value `false`. |
| 66 | let boolean = 4 > 4; |
| 67 | Message($"Greater than comparison: {boolean}"); |
| 68 | |
| 69 | // The Boolean value `false`. |
| 70 | let boolean = 4 > 6; |
| 71 | Message($"Greater than comparison: {boolean}"); |
| 72 | |
| 73 | // The Boolean value `true`. |
| 74 | let boolean = 6 > 4; |
| 75 | Message($"Greater than comparison: {boolean}"); |
| 76 | |
| 77 | // `>=` tests if the first value is greater than or equivalent to |
| 78 | // the second value. |
| 79 | |
| 80 | // The Boolean value `true`. |
| 81 | let boolean = 4 >= 4; |
| 82 | Message($"Greater than or equal comparison: {boolean}"); |
| 83 | |
| 84 | // The Boolean value `false`. |
| 85 | let boolean = 4 >= 6; |
| 86 | Message($"Greater than or equal comparison: {boolean}"); |
| 87 | |
| 88 | // The Boolean value `true`. |
| 89 | let boolean = 6 >= 4; |
| 90 | Message($"Greater than or equal comparison: {boolean}"); |
| 91 | } |
| 92 | } |
| 93 | |