p5js Variables

EngagingAstrophysics avatar
EngagingAstrophysics
·
·
Download

Start Quiz

Study Flashcards

8 Questions

P5js 中有什么类型的变量?

primitive 变量和 complex 变量

哪个关键字用于声明一个不能重新分配的常量变量?

const

使用循环可以

重复执行代码块

什么类型的循环用于迭代一个序列或数组?

for 循环

什么类型的条件语句用于执行一个代码块,如果条件为 true?

if 语句

Switch 语句用于执行一个代码块基于什么?

表达式的值

If-else 语句用于执行一个代码块,如果条件为 true,否则执行另一个代码块?

什么类型的变量用于存储多个值?

complex 变量

Study Notes

Variables

  • In p5js, variables are used to store and manipulate data.
  • There are two types of variables:
    • Primitive variables: store single values, such as numbers, strings, or booleans.
      • Example: let x = 10;
    • Complex variables: store multiple values, such as arrays or objects.
      • Example: let colors = ['red', 'green', 'blue'];
  • Variables can be declared using the let, const, or var keywords.
  • The let keyword is used to declare a variable that can be reassigned.
  • The const keyword is used to declare a constant variable that cannot be reassigned.
  • The var keyword is used to declare a variable that can be reassigned, but it is not recommended to use it.

Loops

  • Loops are used to repeat a block of code multiple times.
  • There are two types of loops:
    • For loops: used to iterate over a sequence of numbers or an array.
      • Example: for (let i = 0; i < 5; i++) { console.log(i); }
    • While loops: used to repeat a block of code as long as a condition is true.
      • Example: let i = 0; while (i < 5) { console.log(i); i++; }
  • Loops can be used to:
    • Iterate over an array or an object
    • Perform a task multiple times
    • Create animations or interactive effects

Conditional Statements

  • Conditional statements are used to make decisions based on conditions or rules.
  • There are three types of conditional statements:
    • If statements: used to execute a block of code if a condition is true.
      • Example: if (x > 5) { console.log('x is greater than 5'); }
    • If-else statements: used to execute a block of code if a condition is true, and another block of code if the condition is false.
      • Example: if (x > 5) { console.log('x is greater than 5'); } else { console.log('x is less than or equal to 5'); }
    • Switch statements: used to execute a block of code based on the value of an expression.
      • Example: switch (color) { case 'red': console.log('The color is red'); break; case 'green': console.log('The color is green'); break; default: console.log('The color is unknown'); }
  • Conditional statements can be used to:
    • Make decisions based on user input or data
    • Create interactive effects or animations
    • Validate user input or data

变量

  • 在 p5js 中,变量用于存储和操作数据。
  • 有两种类型的变量:
    • 基本变量:存储单个值,如数字、字符串或布尔值。例如:let x = 10;
    • 复杂变量:存储多个值,如数组或对象。例如:let colors = ['red', 'green', 'blue'];
  • 变量可以使用 letconstvar 关键字声明。
  • let 关键字用于声明可以重新赋值的变量。
  • const 关键字用于声明不能重新赋值的常量变量。
  • var 关键字用于声明可以重新赋值的变量,但不建议使用。

循环

  • 循环用于重复一个代码块多次。
  • 有两种类型的循环:
    • for 循环:用于迭代一个数字序列或数组。例如:for (let i = 0; i < 5; i++) { console.log(i); }
    • while 循环:用于重复一个代码块只要条件为 true。例如:let i = 0; while (i < 5) { console.log(i); i++; }
  • 循环可以用于:
    • 迭代数组或对象
    • 执行多次任务
    • 创建动画或交互效果

条件语句

  • 条件语句用于根据条件或规则做出决策。
  • 有三种类型的条件语句:
    • if 语句:用于在条件为 true 时执行一个代码块。例如:if (x > 5) { console.log('x is greater than 5'); }
    • if-else 语句:用于在条件为 true 时执行一个代码块,并在条件为 false 时执行另一个代码块。例如:if (x > 5) { console.log('x is greater than 5'); } else { console.log('x is less than or equal to 5'); }
    • switch 语句:用于根据表达式的值执行一个代码块。例如:switch (color) { case 'red': console.log('The color is red'); break; case 'green': console.log('The color is green'); break; default: console.log('The color is unknown'); }
  • 条件语句可以用于:
    • 根据用户输入或数据做出决策
    • 创建交互效果或动画
    • 验证用户输入或数据

Learn about variables in p5js, including primitive and complex variables, and how to declare them using let, const, and var keywords.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free
Use Quizgecko on...
Browser
Browser