CS midterm 2 Flashcards

1
Q

variable

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

using variables

A

• We don’t always know the value (or even care what the exact value is), we just refer to it with a name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

types of variables

A
•	Built-in Variables 
o	Eg. Width, mouseX
•	Constants
o	Eg. TWO_PI, BEVEL
•	User-defined Variables
o	Eg. donutSize, bgShade
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

math expressions

A
    • add
    • subtract… negative numbers
    • multiply
  • () order of operations
  • / divide… divide by zero error… truncation behaviour
  • % modulo (remainder after division)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

demos

A
  • int x = 10
  • x = x * 20 (10 x 20 = 200)
  • printIn(x);
  • x is the variable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

creating user-defined variables

A
  • int donutSize = 80; = variable declaration statement
  • int = variable data type
  • donutSize = variable name
  • = 80; = value assignment
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

variable data types

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

declaring and assigning variables

A
•	Declaring: 
o	int donutSize;
•	Assigning:
o	donutSize = 80;
•	Declaring and Assigning (aka “Initializing”):
o	int donutSize = 80;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

why user defined variables

A

• 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

(key/mouse)PressNumber

A
  • Keep track of mouse and key presses with user-defined variables
  • Also…
  • textAlign(horz, vert); (or CENTER, CENTER)
  • text(s, x, y);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

functions that return a value

A
•	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));
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

int and float conversion

A

• 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));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

custom variables

A
•	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	}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

boolean expressions

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

conditional statement

A

if a boolean expression is true, than execute a block of code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

rest stop analogy

A

• A single if statement is like deciding to stop at a rest stop on a highway

17
Q

detour analogy

A

• if else is like having to take a detour (the if) or taking the original road (the else)

18
Q

two common logic errors with if statements

A
  • 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.
19
Q

equality vs. assignment operators

A

• 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)

20
Q

Numerical Representation: Integers

A
•	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
21
Q

Numerical Representation: Float

A

• 0.0001 meters is a huge difference for a microchip designer, though all measurements will be less than about 0.1 meters

22
Q

numerical precision

A
  • 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…
23
Q

logical operators

A
  • && means “and”
  • || means “or”
  • ! means “not”
24
Q

mousePressed

A

• 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()