CS midterm 2 Flashcards
(24 cards)
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
rest stop analogy
• A single if statement is like deciding to stop at a rest stop on a highway
detour analogy
• if else is like having to take a detour (the if) or taking the original road (the else)
two common logic errors with if statements
- Adding semicolon after the Boolean expression means no code block. A code block with no “if” means always execute.
- Missing “else” makes two if statements.
equality vs. assignment operators
• Equality Operator ==
o Is the left value equal to the right value?
o Eg. State == 1 means “is state equal to 1?”
o Left and right can be anything that reduces to a single value (a variable, a function that returns a value, a number, an expression)
• Assignment Operator =
o Assign the right value to the left variable
o Eg. State = 1 means “assign 1 to state”
o Left must be a variable, right can be anything that reduces to a single value (a variable, a function that returns a value, a number, an expression)
Numerical Representation: Integers
• Binary Numbers: 1 and 0 • Bits and Bytes • Integer Representation: o Fixed size o How to handle negative numbers? • Use println statements to find errors
Numerical Representation: Float
• 0.0001 meters is a huge difference for a microchip designer, though all measurements will be less than about 0.1 meters
numerical precision
- How many numbers are there between 0 and 1?
- How many decimal points in one-third(1/3)?
- Computers can not always do exact math
- This has implications for equality testing…
logical operators
- && means “and”
- || means “or”
- ! means “not”
mousePressed
• An event function
o Called once when the mouse button is pressed
• mousePressed is a built-in Boolean variable
o true when the mouse button is pressed, false otherwise
• Same for keyPressed and keyPressed()