Conditionals 1 / 条件分岐 1

条件は質問のようなものです。質問の答えが真(true)の場合はあるアクションを実行し、偽(false)の場合は別のアクションを実行するようにプログラムに指示できます。たとえば、変数 'i' がゼロに等しい場合は線を描画します。

conditional if
Control Logic

View Source Code

/*
 * @name Conditionals 1
 * @arialabel Pattern of alternating long and short lines
 * @description Conditions are like questions.
 * They allow a program to decide to take one action if
 * the answer to a question is true or to do another action
 * if the answer to the question is false.
 * The questions asked within a program are always logical
 * or relational statements. For example, if the variable 'i' is
 * equal to zero then draw a line.
 */
function setup() {
  // createCanvas(720, 360);
  createCanvas(windowWidth, windowHeight);
  background(0);

  for (let i = 10; i < width; i += 10) {
    // If 'i' divides by 20 with no remainder draw the first line
    // else draw the second line
    if (i % 20 === 0) {
      stroke(255);
      line(i, 80, i, height / 2);
    } else {
      stroke(153);
      line(i, 20, i, 180);
    }
  }
}

License

Source code is available on GitHub p5.js website legacy.

All examples are licensed under CC BY-NC-SA 4.0.