You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/book/part-1-instructions/3-control-flow/1-concepts/01-0-boolean-data.md
+11-3Lines changed: 11 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ The boolean [type](../../../1-sequence-and-data/1-concepts/07-type) is used to r
6
6
7
7
<aid="FigureBooleanData"></a>
8
8
9
-

9
+

10
10
<divclass="caption"><spanclass="caption-figure-nbr">Figure x.y: </span>Boolean data represents truth</div><br/>
11
11
12
12
:::note
@@ -23,11 +23,11 @@ The boolean [type](../../../1-sequence-and-data/1-concepts/07-type) is used to r
23
23
24
24
You will use boolean values, comparisons, and boolean logic throughout your program. Every time you want to make a decision, and boolean will be there to help you check which conditions are true and thereby help your code work out what to do next.
25
25
26
-
Create boolean variables to store, remember, and work with conditions, or evaluate these on the fly with comparisons. With the boolean you are always asking "is this true", and you can then connect these conditions to the actions you want performed through the control flow statements.
26
+
Create boolean variables to store, remember, and work with conditions, or evaluate these on the fly with comparisons. With the boolean you are always asking "Is this true?", and you can then connect these conditions to the actions you want to be performed through the control flow statements.
27
27
28
28
## Examples
29
29
30
-
There are some useful methods in SplashKit that you can use to ask about user actions, as shown in the following table. You can use these to create conditions based on things the user has done. For example, have they asked to quit the program, or have they typed the escape key?
30
+
The SplashKit library includes some useful examples of methods that return boolean data, as shown in the following table. You can use these to create conditions based on things the user has done. For example, you could create a condition that checks if the user has asked to quit the program or they have typed the escape key.
Copy file name to clipboardExpand all lines: src/content/docs/book/part-1-instructions/3-control-flow/1-concepts/01-2-logic-operators.md
+44-64Lines changed: 44 additions & 64 deletions
Original file line number
Diff line number
Diff line change
@@ -8,13 +8,50 @@ The comparison operators allow you to compare *two* values. This is very useful,
8
8
9
9
While you are limited to two values with the comparison operators, there are other operators that allow you to **combine** boolean expressions. This will enable you to combine together multiple boolean values into a single boolean expression.
10
10
11
-
There are four main *logical operators*: **and**, **or**, **xor**, and **not**. The operators **and**, **or**, and **xor** each work on two boolean values, combining them to give a new boolean value. For example, the `and` operator allows you to check if both of the expressions are true. The expression `area > 0 and area < 10` will be true only when area is **both** larger than zero **and** less then ten. The **not** operator works with a single boolean value, allowing you to check if something is not true.
11
+
There are four main *logical operators*: **and**, **or**, **xor**, and **not**. The operators **and**, **or**, and **xor** each work on two boolean values, combining them to give a new boolean value. For example, the [and operator](../01-3-and-operator) allows you to check if both of the expressions are true. The considtion `area > 0`**and**`area < 10` will be true only when area is **both** larger than zero **and** area is less then ten. The **not** operator works with a single boolean value, allowing you to check if something is not true.
12
+
13
+
Truth tables are a great way of visualising the results from these operators. [Figure x.y](#FigureLogicalOperators) shows the four truth tables for the operators we are looking at. Each table shows a single operator joining two placeholder values (`a` and `b`). For our visualisation, `a` is represented as rows and `b` as columns. There is then one row or column for true and another for false, being the only two value that these placeholders could be. Each cell in the table represents one combination of these values, gives you the four potential combinations in the case of the and, or, and xor tables. The table shows you the result of the operation in each cell, thereby showing you all of the possible results based on the different inputs.
You often want to check multiple conditions when you are making a decision. For example, to check if the user has clicked something on the screen, you need to check if they have clicked *and* the mouse cursor is over that area. This is a case where **and** is useful.
23
+
24
+
We could achieve this using something like the following. This will be true when the left button was clicked, and the mouse is 5 to 10 pixels from the left of the window. Explore this in a table as shown below. To help fit this on the page, the table header uses some shorthand names for the parts.
25
+
26
+
- Idea: The left button was clicked, **and** the X value of the mouse cursor is >= 5 and <= 10
Notice that in this case we need to have all of the part of the condition being true for the result to be true. This matches what we asked for - the mouse must be clicked and the mouse x between 5 and 10.
37
+
38
+
With or you can check if **any** of a number of conditions are true. For example, has the user asked to quit or have they typed the escape key. This can be achieved with the following condition. The following truth table shows the possible combinations for this.
39
+
40
+
- Idea: The user asked to quit, **or** they typed the escape key
In this case you can see that if any one of the values is true, then the result is true. If you ask to quit *or* you type the escape key, then this condition will be true. If you manage to do both at the same time, that will be true as well.
51
+
52
+
## In C#
53
+
54
+
This is one area where C# uses cryptic symbols rather than clear text. This is based upon the heritage of C#, which is based upon the C programming language. Rather than using keywords for *and* and *or* orperators, C# uses **&&** for and, **||** for or, **!** for not, and **^** for xor.
18
55
19
56
<aid="TableLogicalOperators"></a>
20
57
@@ -27,6 +64,12 @@ There are four main *logical operators*: **and**, **or**, **xor**, and **not**.
To make matters worse, new versions of C# have added in **and** and **or** keywords for pattern matching expressions. These differ from boolean operators, so avoid their use.
69
+
:::
70
+
71
+
## Example
72
+
30
73
<aid="TableLogicalExpressionsExample"></a>
31
74
32
75
<table>
@@ -75,66 +118,3 @@ There are four main *logical operators*: **and**, **or**, **xor**, and **not**.
You often want to check multiple conditions when you are making a decision. For example, to check if the user has clicked over a button you need to check if they have clicked, and if the mouse is over a particular area. This is a case where **and** is useful.
82
-
83
-
For example, `MouseClicked(MouseButton.LeftButton) && MouxeX() >= 5 && MouseX() <= 10` will be true when the left button was clicked, and the mouse is 5 to 10 pixels from the left of the window. We can explore this in a **truth table** as shown below. To help fit this on the page, the table header uses some shorthand names for the parts.
Notice that in this case we need to have all values being true for the result to be true. This matches what we asked for - the mouse must be clicked and the mouse x between 5 and 10.
93
-
94
-
With or you can check if **any** of a number of conditions are true. For example, has the user asked to quit or have they typed the escape key. This can be achieved with the condition `QuitRequested() || KeyTyped(KeyCode.EscapeKey)`. The following truth table shows the possible combinations for this.
In this case you can see that if any one of the values is true then the condition is true. If you ask to quit or you type the escape key, then this condition will be true. If you manage to do both at the same time, that will be true as well.
104
-
105
-
You can mix and and or, in which case you should use brackets (parenthesis) to clearly show which order to do the operations. For example `KeyDown(KeyCode.SpaceKey) || MouseX() > 50 && MouseClicked(MouseButton.LeftButton)` - we want to make it clear which parts are combined.
106
-
107
-
If this was `(KeyDown(KeyCode.SpaceKey) || MouseX() > 50) && MouseClicked(MouseButton.LeftButton)` that would require the mouse to be clicked and the space key to be down or the mouse x to be larger than 50.
108
-
109
-
| Space Key | mx > 50 | Clicked | Space Key Down or Mouse X > 50 (A) | A and clicked? |
110
-
| --- | --- | --- | --- | --- |
111
-
| false | false | false | false | false |
112
-
| true | false | false | true | false |
113
-
| false | true | false | true | false |
114
-
| true | true | false | true | false |
115
-
| true | false | true | true | true |
116
-
| false | true | true | true | true |
117
-
| true | true | true | true | true |
118
-
119
-
120
-
Where as, `KeyDown(KeyCode.SpaceKey) || (MouseX() > 50 && MouseClicked(MouseButton.LeftButton))` would be true when the space key is down, or the mouse x is larger than 50 and the left button is clicked.
121
-
122
-
| Space Key | mx > 50 | Clicked | Mouse X > 50 and clicked? (A) | space down \|\| A |
123
-
| --- | --- | --- | --- | --- |
124
-
| false | false | false | false | false |
125
-
| true | false | false | true | true |
126
-
| false | true | false | true | true |
127
-
| true | true | false | true | true |
128
-
| true | false | true | true | true |
129
-
| false | true | true | true | true |
130
-
| true | true | true | true | true |
131
-
132
-
:::tip
133
-
134
-
Always make this as clear as you can, and parenthesis help greatly here. The languages have precidence rules that determine what will happen, but you should never really need to know these. If you do, the code isn't clear enough. Add some parenthesis, then everyone can see what was intended.
You can mix *and* and *or* within a single condition. In this case you should use brackets (parenthesis) to clearly show the **order** to do the operations. For example, with the following expression do we want the *or* or the *and* to be evaluated first? The results are different depending on the order, so it is important to think this through. In this case the intention is to use this to trigger an action when the user types the space key, or they click more than 50 pixels from the left of the window.
Lets start with the first option where or is evaluated first. In this case the `(KeyDown(KeyCode.SpaceKey) || MouseX() > 50)` will be evaluated first (we can then call this `result`). The condition is then continued when the computer evaluates the `result && MouseClicked(MouseButton.LeftButton)`. Thinking about this, you may be able to see that this will require the mouse to be clicked and the space key to be down or the mouse x to be larger than 50.
17
+
18
+
| Space Key | mx > 50 | Clicked | Space Key Down or Mouse X > 50 (A) | A and clicked? |
19
+
| --- | --- | --- | --- | --- |
20
+
| false | false | false | false | false |
21
+
| true | false | false | true | false |
22
+
| false | true | false | true | false |
23
+
| true | true | false | true | false |
24
+
| true | false | true | true | true |
25
+
| false | true | true | true | true |
26
+
| true | true | true | true | true |
27
+
28
+
Where as, `KeyDown(KeyCode.SpaceKey) || (MouseX() > 50 && MouseClicked(MouseButton.LeftButton))` would be true when the space key is down, or the mouse x is larger than 50 and the left button is clicked.
29
+
30
+
| Space Key | mx > 50 | Clicked | Mouse X > 50 and clicked? (A) | space down \|\| A |
31
+
| --- | --- | --- | --- | --- |
32
+
| false | false | false | false | false |
33
+
| true | false | false | true | true |
34
+
| false | true | false | true | true |
35
+
| true | true | false | true | true |
36
+
| true | false | true | true | true |
37
+
| false | true | true | true | true |
38
+
| true | true | true | true | true |
39
+
40
+
:::tip
41
+
42
+
Always make this as clear as you can, and parenthesis help greatly here. The languages have precidence rules that determine what will happen, but you should never really need to know these. If you do, the code isn't clear enough. Add some parenthesis, then everyone can see what was intended.
0 commit comments