|
| 1 | +--- |
| 2 | +title: Boolean |
| 3 | +module: Foundation |
| 4 | +submodule: Foundation |
| 5 | +file: src/core/reference.js |
| 6 | +description: > |
| 7 | + <p>A value that's either <code>true</code> or <code>false</code>.</p> |
| 8 | +
|
| 9 | + <p><code>Boolean</code> values help to make decisions in code. They appear any |
| 10 | + time a |
| 11 | +
|
| 12 | + logical condition is checked. For example, the condition |
| 13 | +
|
| 14 | + "Is a mouse button being pressed?" must be either <code>true</code> or |
| 15 | +
|
| 16 | + <code>false</code>:</p> |
| 17 | +
|
| 18 | + <pre><code class="language-js">// If the user presses the mouse, draw a circle |
| 19 | + at |
| 20 | +
|
| 21 | + // the mouse's location. |
| 22 | +
|
| 23 | + if (mouseIsPressed === true) { |
| 24 | + circle(mouseX, mouseY, 20); |
| 25 | + } |
| 26 | +
|
| 27 | + </code></pre> |
| 28 | +
|
| 29 | + <p>The <code>if</code> statement checks whether |
| 30 | +
|
| 31 | + <a href="/reference/p5/mouseIsPressed/">mouseIsPressed</a> is |
| 32 | + <code>true</code> and draws a |
| 33 | +
|
| 34 | + circle if it is. <code>Boolean</code> expressions such as <code>mouseIsPressed |
| 35 | + === true</code> |
| 36 | +
|
| 37 | + evaluate to one of the two possible <code>Boolean</code> values: |
| 38 | + <code>true</code> or <code>false</code>.</p> |
| 39 | +
|
| 40 | + <p>The <code>===</code> operator (EQUAL) checks whether two values are equal. |
| 41 | + If they |
| 42 | +
|
| 43 | + are, the expression evaluates to <code>true</code>. Otherwise, it evaluates to |
| 44 | +
|
| 45 | + <code>false</code>.</p> |
| 46 | +
|
| 47 | + <p>Note: There's also a <code>==</code> operator with two <code>=</code> |
| 48 | + instead of three. Don't use |
| 49 | +
|
| 50 | + it.</p> |
| 51 | +
|
| 52 | + <p>The <a href="/reference/p5/mouseIsPressed/">mouseIsPressed</a> system |
| 53 | + variable is |
| 54 | +
|
| 55 | + always <code>true</code> or <code>false</code>, so the code snippet above |
| 56 | + could also be written |
| 57 | +
|
| 58 | + as follows:</p> |
| 59 | +
|
| 60 | + <pre><code class="language-js">if (mouseIsPressed) { |
| 61 | + circle(mouseX, mouseY, 20); |
| 62 | + } |
| 63 | +
|
| 64 | + </code></pre> |
| 65 | +
|
| 66 | + <p>The <code>!==</code> operator (NOT EQUAL) checks whether two values are not |
| 67 | + equal, as |
| 68 | +
|
| 69 | + in the following example:</p> |
| 70 | +
|
| 71 | + <pre><code class="language-js">if (2 + 2 !== 4) { |
| 72 | + text('War is peace.', 50, 50); |
| 73 | + } |
| 74 | +
|
| 75 | + </code></pre> |
| 76 | +
|
| 77 | + <p>Starting from the left, the arithmetic expression <code>2 + 2</code> |
| 78 | + produces the |
| 79 | +
|
| 80 | + value <code>4</code>. The <code>Boolean</code> expression <code>4 !== 4</code> |
| 81 | + evaluates to <code>false</code> because |
| 82 | +
|
| 83 | + <code>4</code> is equal to itself. As a result, the <code>if</code> |
| 84 | + statement's body is skipped.</p> |
| 85 | +
|
| 86 | + <p>Note: There's also a <code>!=</code> operator with one <code>=</code> |
| 87 | + instead of two. Don't use |
| 88 | +
|
| 89 | + it.</p> |
| 90 | +
|
| 91 | + <p>The <code>Boolean</code> operator <code>&&</code> (AND) checks whether two |
| 92 | + expressions are both |
| 93 | +
|
| 94 | + <code>true</code>:</p> |
| 95 | +
|
| 96 | + <pre><code class="language-js">if (keyIsPressed === true && key === 'p') { |
| 97 | + text('You pressed the "p" key!', 50, 50); |
| 98 | + } |
| 99 | +
|
| 100 | + </code></pre> |
| 101 | +
|
| 102 | + <p>If the user is pressing a key AND that key is <code>'p'</code>, then a |
| 103 | + message will |
| 104 | +
|
| 105 | + display.</p> |
| 106 | +
|
| 107 | + <p>The <code>Boolean</code> operator <code>||</code> (OR) checks whether at |
| 108 | + least one of two |
| 109 | +
|
| 110 | + expressions is <code>true</code>:</p> |
| 111 | +
|
| 112 | + <pre><code class="language-js">if (keyIsPressed === true || mouseIsPressed === |
| 113 | + true) { |
| 114 | + text('You did something!', 50, 50); |
| 115 | + } |
| 116 | +
|
| 117 | + </code></pre> |
| 118 | +
|
| 119 | + <p>If the user presses a key, or presses a mouse button, or both, then a |
| 120 | +
|
| 121 | + message will display.</p> |
| 122 | +
|
| 123 | + <p>The following truth table summarizes a few common scenarios with |
| 124 | + <code>&&</code> and |
| 125 | +
|
| 126 | + <code>||</code>:</p> |
| 127 | +
|
| 128 | + <pre><code class="language-js">true && true // true |
| 129 | +
|
| 130 | + true && false // false |
| 131 | +
|
| 132 | + false && false // false |
| 133 | +
|
| 134 | + true || true // true |
| 135 | +
|
| 136 | + true || false // true |
| 137 | +
|
| 138 | + false || false // false |
| 139 | +
|
| 140 | + </code></pre> |
| 141 | +
|
| 142 | + <p>The relational operators <code>></code>, <code><< code="">, |
| 143 | + <code>>=</code>, and <code><=< code=""> also produce <code>Boolean</code> |
| 144 | +
|
| 145 | + values:</=<></code></<></code></p> |
| 146 | +
|
| 147 | + <pre><code class="language-js">2 > 1 // true |
| 148 | +
|
| 149 | + 2 < 1 // false |
| 150 | +
|
| 151 | + 2 >= 2 // true |
| 152 | +
|
| 153 | + 2 <= 2="" true="" <="" code=""/></code></pre> |
| 154 | +
|
| 155 | + <p>See <a href="/reference/p5/if/">if</a> for more information about |
| 156 | + <code>if</code> statements and |
| 157 | +
|
| 158 | + <a href="/reference/p5/Number/">Number</a> for more information about |
| 159 | + <code>Number</code>s.</p> |
| 160 | +line: 502 |
| 161 | +isConstructor: false |
| 162 | +itemtype: property |
| 163 | +example: |
| 164 | + - |- |
| 165 | +
|
| 166 | + <div> |
| 167 | + <code> |
| 168 | + function setup() { |
| 169 | + createCanvas(100, 100); |
| 170 | +
|
| 171 | + describe('A gray square. When the user presses the mouse, a circle appears at that location.'); |
| 172 | + } |
| 173 | +
|
| 174 | + function draw() { |
| 175 | + background(200); |
| 176 | +
|
| 177 | + // If the user presses the mouse, draw a circle at that location. |
| 178 | + if (mouseIsPressed) { |
| 179 | + circle(mouseX, mouseY, 20); |
| 180 | + } |
| 181 | + } |
| 182 | + </code> |
| 183 | + </div> |
| 184 | +
|
| 185 | + <div> |
| 186 | + <code> |
| 187 | + function setup() { |
| 188 | + createCanvas(100, 100); |
| 189 | +
|
| 190 | + describe('A gray square. When the user presses the mouse, a circle appears at that location.'); |
| 191 | + } |
| 192 | +
|
| 193 | + function draw() { |
| 194 | + background(200); |
| 195 | +
|
| 196 | + // If the user presses the mouse, draw a circle at that location. |
| 197 | + if (mouseIsPressed === true) { |
| 198 | + circle(mouseX, mouseY, 20); |
| 199 | + } |
| 200 | + } |
| 201 | + </code> |
| 202 | + </div> |
| 203 | +
|
| 204 | + <div> |
| 205 | + <code> |
| 206 | + // Click on the canvas to begin detecting key presses. |
| 207 | +
|
| 208 | + function setup() { |
| 209 | + createCanvas(100, 100); |
| 210 | +
|
| 211 | + describe('A gray square that turns pink when the user presses the mouse or a key.'); |
| 212 | + } |
| 213 | +
|
| 214 | + function draw() { |
| 215 | + background(200); |
| 216 | +
|
| 217 | + // If the user presses the mouse, change the background color. |
| 218 | + if (mouseIsPressed === true || keyIsPressed === true) { |
| 219 | + background('deeppink'); |
| 220 | + } |
| 221 | + } |
| 222 | + </code> |
| 223 | + </div> |
| 224 | +
|
| 225 | + <div> |
| 226 | + <code> |
| 227 | + // Click the canvas to begin detecting key presses. |
| 228 | +
|
| 229 | + // Create a Boolean variable. |
| 230 | + let isPlaying = false; |
| 231 | +
|
| 232 | + function setup() { |
| 233 | + createCanvas(100, 100); |
| 234 | +
|
| 235 | + describe( |
| 236 | + 'The message "Begin?\nY or N" written in green on a black background. The message "Good luck!" appears when they press the "y" key.' |
| 237 | + ); |
| 238 | + } |
| 239 | +
|
| 240 | + function draw() { |
| 241 | + background(0); |
| 242 | +
|
| 243 | + // Style the text. |
| 244 | + textAlign(CENTER, CENTER); |
| 245 | + textFont('Courier New'); |
| 246 | + textSize(16); |
| 247 | + fill(0, 255, 0); |
| 248 | +
|
| 249 | + // Display a different message when the user begins playing. |
| 250 | + if (isPlaying === false) { |
| 251 | + text('Begin?', 50, 40); |
| 252 | + text('Y or N', 50, 60); |
| 253 | + } else { |
| 254 | + text('Good luck!', 50, 50); |
| 255 | + } |
| 256 | + } |
| 257 | +
|
| 258 | + // Start playing when the user presses the 'y' key. |
| 259 | + function keyPressed() { |
| 260 | + if (key === 'y') { |
| 261 | + isPlaying = true; |
| 262 | + } |
| 263 | + } |
| 264 | + </code> |
| 265 | + </div> |
| 266 | +class: p5 |
| 267 | +--- |
| 268 | + |
| 269 | + |
| 270 | +# Boolean |
0 commit comments