Skip to content

Commit b7309ae

Browse files
committed
progress on control flow
1 parent 80f1a40 commit b7309ae

File tree

6 files changed

+119
-67
lines changed

6 files changed

+119
-67
lines changed

.markdownlint.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"MD033": false,
3+
"MD013": false
4+
}

src/content/docs/book/part-1-instructions/3-control-flow/1-concepts/01-0-boolean-data.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The boolean [type](../../../1-sequence-and-data/1-concepts/07-type) is used to r
66

77
<a id="FigureBooleanData"></a>
88

9-
![Figure x.y: Boolean data represents truth](./images/boolean-data.png "Boolean data represents truthe")
9+
![Figure x.y: Boolean data represents truth](./images/boolean-data.png "Boolean data represents truth")
1010
<div class="caption"><span class="caption-figure-nbr">Figure x.y: </span>Boolean data represents truth</div><br/>
1111

1212
:::note
@@ -23,11 +23,11 @@ The boolean [type](../../../1-sequence-and-data/1-concepts/07-type) is used to r
2323

2424
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.
2525

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.
2727

2828
## Examples
2929

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.
3131

3232
|**Method** | **Required Arguments** |**Description** |
3333
|-----------|------------------------|----------------|
@@ -40,3 +40,11 @@ There are some useful methods in SplashKit that you can use to ask about user ac
4040
|`MouseClicked`| A mouse button | Was the indicated mouse button clicked? |
4141
|`MouseDown`| A mouse button | Is the indicated mouse button held down? |
4242
|`MouseUp`| A mouse button | Is the indicated mouse button up? (not held down) |
43+
44+
SplashKit also provides some utility methods that you can use when working with text.
45+
46+
|**Method** | **Required Arguments** |**Description** |
47+
|-----------|------------------------|----------------|
48+
|`IsDouble`| a string | Returns true when the string argument can be converted to a double (it contains only a number). |
49+
|`IsInteger`| a string | Returns true when the string argument can be converted to an integer (it contains only a whole number). |
50+
|`IsNumber`| a string | An equivalent to is double. Returns true when the string argument can be converted to a number (it contains only a number). |

src/content/docs/book/part-1-instructions/3-control-flow/1-concepts/01-2-logic-operators.md

Lines changed: 44 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,50 @@ The comparison operators allow you to compare *two* values. This is very useful,
88

99
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.
1010

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.
1214

1315
<a id="FigureLogicalOperators"></a>
1416

1517
![Figure x.y: Logical Operators combine Boolean values](./images/logical-operators.png "Logical Operators combine Boolean values")
1618
<div class="caption"><span class="caption-figure-nbr">Figure x.y: </span>Logical Operators combine Boolean values</div><br/>
1719

20+
## Logical Operators - why, when, and how
21+
22+
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
27+
- Code: `MouseClicked(MouseButton.LeftButton) && MouxeX() >= 5 && MouseX() <= 10`
28+
29+
| Clicked? | MouseX() | Clicked? | mx > 5 | mx <= 10 | MouseClicked(MouseButton.LeftButton) && MouxeX() >= 5 && MouseX() <= 10 |
30+
|---|---|---|---|---|---|
31+
| Not clicked | 7 | false | true | true | false |
32+
| Clicked | 7 | true | true | true | true |
33+
| Clicked | 11 | true | true | false | false |
34+
| Clicked | 4 | true | false | true | false |
35+
36+
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
41+
- Code: `QuitRequested() || KeyTyped(KeyCode.EscapeKey)`
42+
43+
| Quit? | Escape Key Typed? | QuitRequested() \|\| KeyTyped? |
44+
| --- | --- | --- |
45+
| false | false | false |
46+
| true | false | true |
47+
| false | true | true |
48+
| true | true | true |
49+
50+
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.
1855

1956
<a id="TableLogicalOperators"></a>
2057

@@ -27,6 +64,12 @@ There are four main *logical operators*: **and**, **or**, **xor**, and **not**.
2764

2865
<div class="caption"><span class="caption-figure-nbr">Table x.y: </span>Logical Operators</div><br/>
2966

67+
:::caution
68+
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+
3073
<a id="TableLogicalExpressionsExample"></a>
3174

3275
<table>
@@ -75,66 +118,3 @@ There are four main *logical operators*: **and**, **or**, **xor**, and **not**.
75118
</table>
76119

77120
<div class="caption"><span class="caption-figure-nbr">Table x.y: </span>Example Logical Expressions</div><br/>
78-
79-
## Logical Operators - why, when, and how
80-
81-
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.
84-
85-
| Clicked? | MouseX() | Clicked? | mx > 5 | mx <= 10 | MouseClicked(MouseButton.LeftButton) && MouxeX() >= 5 && MouseX() <= 10 |
86-
|---|---|---|---|---|---|
87-
| Not clicked | 7 | false | true | true | false |
88-
| Clicked | 7 | true | true | true | true |
89-
| Clicked | 11 | true | true | false | false |
90-
| Clicked | 4 | true | false | true | false |
91-
92-
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.
95-
96-
| Quit? | Key Typed? | QuitRequested() \|\| KeyTyped(KeyCode.EscapeKey) |
97-
| --- | --- | --- |
98-
| false | false | false |
99-
| true | false | true |
100-
| false | true | true |
101-
| true | true | true |
102-
103-
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.
135-
136-
:::
137-
138-
### Short Circuit Evaluation
139-
140-
Notice how the
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
title: And Operator
3+
sidebar:
4+
label: " - And Operator"
5+
---
6+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
title: Or Operator
3+
sidebar:
4+
label: " - Or Operator"
5+
---
6+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
title: Combining Operator
3+
sidebar:
4+
label: " - Combining Operator"
5+
---
6+
7+
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.
8+
9+
- `KeyDown(KeyCode.SpaceKey) || MouseX() > 50 && MouseClicked(MouseButton.LeftButton)`
10+
11+
This gives two options:
12+
13+
1. `(KeyDown(KeyCode.SpaceKey) || MouseX() > 50) && MouseClicked(MouseButton.LeftButton)`
14+
2. `KeyDown(KeyCode.SpaceKey) || (MouseX() > 50 && MouseClicked(MouseButton.LeftButton))`
15+
16+
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.
43+
44+
:::
45+
46+
### Short Circuit Evaluation
47+
48+
Notice how the

0 commit comments

Comments
 (0)