CS midterm 3 Flashcards

1
Q

loops

A
•	When you do something over and over again
o	while (expression) {
o	statements
o	}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

anatomy of a while loop

A

(initialization)

y = 10;

(test expression)
while (y < height) {
rect(50, y, 50, 10);

(update)
y += 20;
}

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

draw() frames vs. while loop iterations

A
  • Code block in draw is repeated every frame

* Code block in while is repeated every loop iteration

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

common logic errors leading to infinite loops

A

• Adding a semicolon after the Boolean expression means no code block
• Without a code block, the loop variable won’t update
o Fix this by removing the semicolon after the Boolean expression
• Loop variable is updated such that the loop condition will always be true
o Fix this by ensuring that your code eventually makes the loop repetition expression false

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

debugging loops

A
  • Test a single loop operation first (without a loop)
  • Simplify the loop operation
  • Slow down drawn frames with frameRate(1)
  • Use println
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

gradient

A
•	Made up of lines + increasing values
o	while (y < height) {
o	stroke(shade);
o	line(0, y, width, y);
o	y++;
o	shade += 1; }
•	Refer to gradient demo
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

assignment operator “short forms”

A
•	These all add 1 to x (they are all equivalent):
o	x = x + 1;
o	x += 1;
o	x++;
•	These both add 5 to x (they are both equivalent):
o	x = x + 5;
o	X += 5;
•	Other examples
o	// same as x = x + 10 * y;
o	x += 10 * y;
o	// same as x = x + random(-2, 2);
o	x += random(-2, 2);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

more assignment operator

A
  • x–;
  • x -= 10;
  • x *= 10;
  • x /= 10;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

anatomy of a For loop

A

for(int y = 10; y < height; y += 20) {
rect(50, y, 50, 10);\
}

1) initialization - internal variable declaration
2) test expression - y < height
3) update (keep adding 1 or whatever)

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

when to use For or While loop?

A

• for is a short form for while, and they’re interchangeable
• For most situations, you can use either
• But sometimes, one is easier to use in certain cases:
• Use for to loop an exact number of repetitions:
o I want 10 pacmen
o I want 3 lines
o I want random(3, 11) lines
• Use for to update by same amount:
o I want count by 10 from 0 to 100
o I want lines spaced 10 pixels apart the width of a canvas
• Use while to update by different amounts when you don’t need to loop an exact number of repetitions:
o I want lines spaced 2-10 pixels apart the width of the canvas

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

scope

A
  • What parts of your code have access to a variable

* Ie. What variables can a code statement “see”

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

global scope

A

• We have be declaring variables “outside” of functions
o These are called global variables
• Global variables can be used anywhere in a program after they are declared
o They have global scope
o They “remember” their value between calls to setup(), draw(), etc.
o Built-in Processing variables are global

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

local scope

A

• You can declare variables “inside” any code block
o These are called local variables
• Local Variables can be used anywhere in that code block after they’re declared
• Recall, a code block is anything in between { and }
o Functions, loops and conditionals have code blocks
• Local variables cease to exist after the code block is exited
o They have local scope

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

when to use local variables

A

• When you don’t need to “remember” the value after the code block completes
o A loop variable
o An intermediate calculation
o To re-use a common value in a function call

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

couple common errors (loops)

A
  • Slides 42-45 for in-depth explanation
  • Variable must be declared above (called “out of scope”)
  • Logic errors i.e int x; and x += 2;
  • Local variable “shadow” the global variable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

creating an infinite loop

A

• Typical Causes:
o Forgetting to change the counter variable
o Changing the counter variable incorrectly
o Using Wrong Test
• An infinite loop is a logical error

17
Q

when to use INT? or FLOAT?

A
  • Use int to count things (eg. Loop 10 times)
  • Use int to “index” things (eg. Array positions)
  • Use int if you want to check for equality (eg. i == 10)
  • Use int if you don’t care about decimals (eg. pixel position)
  • Otherwise use float…