ブール変数には、true(真)または false(偽)の2つの可能な値しかありません。プログラムの流れを決定するために、制御文でブール値を使用するのが一般的です。この例では、ブール値「b」がtrueの場合は垂直線が描画され、「b」がfalseの場合は水平線が描画されます。
boolean conditional
Data Control
View Source Code
/*
* @name True and False
* @arialabel Black background with vertical white lines on the left half and horizontal white lines on the right half
* @description A Boolean variable has only two possible values: true or false.
* It is common to use Booleans with control statements to determine the flow
* of a program. In this example, when the boolean value "b" is true, vertical
* lines are drawn and when the boolean value "b" is false, horizontal
* lines are drawn.
*/
function setup() {
// createCanvas(720, 400);
createCanvas(windowWidth, windowHeight);
background(0);
stroke(255);
let b = false;
let d = 20;
let middle = width / 2;
for (let i = d; i <= width; i += d) {
b = i < middle;
if (b === true) {
// Vertical line
line(i, d, i, height - d);
}
if (b === false) {
// Horizontal line
line(middle, i - middle + d, width - d, i - middle + d);
}
}
}
License
Source code is available on GitHub p5.js website legacy.
All examples are licensed under CC BY-NC-SA 4.0.