CS midterm 2 Flashcards
variable
- Symbolic name used to refer to a value
- The value may change, but the symbolic name doesn’t
- Variables make code easier to change and easier to read
- Eg. MouseX is a variable
- We’re storing values “in them” and then using them like numbers
using variables
• We don’t always know the value (or even care what the exact value is), we just refer to it with a name
types of variables
• Built-in Variables o Eg. Width, mouseX • Constants o Eg. TWO_PI, BEVEL • User-defined Variables o Eg. donutSize, bgShade
math expressions
- add
- subtract… negative numbers
- multiply
- () order of operations
- / divide… divide by zero error… truncation behaviour
- % modulo (remainder after division)
demos
- int x = 10
- x = x * 20 (10 x 20 = 200)
- printIn(x);
- x is the variable
creating user-defined variables
- int donutSize = 80; = variable declaration statement
- int = variable data type
- donutSize = variable name
- = 80; = value assignment
variable data types
- int – “integer”, whole numbers like -5, 0, 12
- float – “floating point”, decimal numbers like 0.123, -10.1, 32.0, 0.0
- Boolean – true or false
- String – any literal text, eg. “hello”, “123”, “yoko ono”, etc.
- Char – one character, eg. ‘a’, ‘4’
- Color – a processing colour
- …many more types
declaring and assigning variables
• Declaring: o int donutSize; • Assigning: o donutSize = 80; • Declaring and Assigning (aka “Initializing”): o int donutSize = 80;
why user defined variables
• To reuse the same value in your program
o Eg. Draw many squares of same size
• To make it easy to change a value later
o Eg. Make squares larger
• To make your code easier to understand
o Eg. Easier to read code since “squareSize” means more than “10”
• Keep tracking of something that changes
o Eg. Click number, animation
(key/mouse)PressNumber
- Keep track of mouse and key presses with user-defined variables
- Also…
- textAlign(horz, vert); (or CENTER, CENTER)
- text(s, x, y);
functions that return a value
• Some functions return a value, like random() • You can assign the “result” of these functions to a variable o float x = random(100); • You can also use them as an argument for another function o point(random(width), random(height));
int and float conversion
• You can assign an int to a float:
o float f = 1;
• You cannot assign a float to an int
o int y = 0.5; // error! 0.5 is a float
o int r = radians(45); // error! Radians returns a float
o int hue = random(360); // error! Random returns a float
• You can convert a float to int using the int() function
o int y = int(0.5);
o int r = int(radians(45));
o int hue = int(random(360));
custom variables
• Tend to declare variables at the beginning before we do anything else • Example: o int donutSize = 80; o int bgShade = 200; // light gray • After declaring variables, they can be used anywhere throughout the program o void setup() { o size(100, 100); o background(bgShade); o }
boolean expressions
an expression that evaluates to true or false
!= number is not equal to 99
<= number is less than or equal to
> = number is greater than or equal to
== number is equal to
< number is greater than
> number is less than
conditional statement
if a boolean expression is true, than execute a block of code