CS midterm 3 Flashcards
loops
• When you do something over and over again o while (expression) { o statements o }
anatomy of a while loop
(initialization)
y = 10;
(test expression)
while (y < height) {
rect(50, y, 50, 10);
(update)
y += 20;
}
draw() frames vs. while loop iterations
- Code block in draw is repeated every frame
* Code block in while is repeated every loop iteration
common logic errors leading to infinite loops
• 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
debugging loops
- Test a single loop operation first (without a loop)
- Simplify the loop operation
- Slow down drawn frames with frameRate(1)
- Use println
gradient
• 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
assignment operator “short forms”
• 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);
more assignment operator
- x–;
- x -= 10;
- x *= 10;
- x /= 10;
anatomy of a For loop
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)
when to use For or While loop?
• 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
scope
- What parts of your code have access to a variable
* Ie. What variables can a code statement “see”
global scope
• 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
local scope
• 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
when to use local variables
• 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
couple common errors (loops)
- 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