variables, values, expressionsfwp/lecture02/slides2.pdf · 2019. 12. 30. · variables. values have...
TRANSCRIPT
![Page 1: Variables, values, expressionsfwp/lecture02/slides2.pdf · 2019. 12. 30. · variables. values have types 5 is a number. x holds a number “Inigo Montoya” is a string. myName holds](https://reader036.vdocuments.mx/reader036/viewer/2022081615/5fe1a604b2d9ae01ee5111b3/html5/thumbnails/1.jpg)
Variables, values, expressions
![Page 2: Variables, values, expressionsfwp/lecture02/slides2.pdf · 2019. 12. 30. · variables. values have types 5 is a number. x holds a number “Inigo Montoya” is a string. myName holds](https://reader036.vdocuments.mx/reader036/viewer/2022081615/5fe1a604b2d9ae01ee5111b3/html5/thumbnails/2.jpg)
warm-up: smile
ellipse(), rect(), fill(), stroke()http://processingjs.org/reference/
Grab a partner and do exercise: smile
(lecture 1 notes)
![Page 3: Variables, values, expressionsfwp/lecture02/slides2.pdf · 2019. 12. 30. · variables. values have types 5 is a number. x holds a number “Inigo Montoya” is a string. myName holds](https://reader036.vdocuments.mx/reader036/viewer/2022081615/5fe1a604b2d9ae01ee5111b3/html5/thumbnails/3.jpg)
style: comments
![Page 4: Variables, values, expressionsfwp/lecture02/slides2.pdf · 2019. 12. 30. · variables. values have types 5 is a number. x holds a number “Inigo Montoya” is a string. myName holds](https://reader036.vdocuments.mx/reader036/viewer/2022081615/5fe1a604b2d9ae01ee5111b3/html5/thumbnails/4.jpg)
Declaration
Assignment
Use of value
1. Declare the variable with var: reserve space in memory to store a value, and give it a name
2. Assign a value to the variable with =, copying a pattern of 0s and 1s to that location
3. Use the variable anywhere a value is needed.
variables
![Page 5: Variables, values, expressionsfwp/lecture02/slides2.pdf · 2019. 12. 30. · variables. values have types 5 is a number. x holds a number “Inigo Montoya” is a string. myName holds](https://reader036.vdocuments.mx/reader036/viewer/2022081615/5fe1a604b2d9ae01ee5111b3/html5/thumbnails/5.jpg)
values have types
5 is a number. x holds a number
“Inigo Montoya” is a string. myName holds a string.
Declaration and assignment can be combined.
![Page 6: Variables, values, expressionsfwp/lecture02/slides2.pdf · 2019. 12. 30. · variables. values have types 5 is a number. x holds a number “Inigo Montoya” is a string. myName holds](https://reader036.vdocuments.mx/reader036/viewer/2022081615/5fe1a604b2d9ae01ee5111b3/html5/thumbnails/6.jpg)
values have types
sayHello is a function
true, false, and y are boolean values
![Page 7: Variables, values, expressionsfwp/lecture02/slides2.pdf · 2019. 12. 30. · variables. values have types 5 is a number. x holds a number “Inigo Montoya” is a string. myName holds](https://reader036.vdocuments.mx/reader036/viewer/2022081615/5fe1a604b2d9ae01ee5111b3/html5/thumbnails/7.jpg)
values have types1. numbers (3.141592654, 7) 2. strings (“frog”) 3. booleans (true, false) 4. functions (function() {…})
Always know what type of value every one of your variables holds.
![Page 8: Variables, values, expressionsfwp/lecture02/slides2.pdf · 2019. 12. 30. · variables. values have types 5 is a number. x holds a number “Inigo Montoya” is a string. myName holds](https://reader036.vdocuments.mx/reader036/viewer/2022081615/5fe1a604b2d9ae01ee5111b3/html5/thumbnails/8.jpg)
variables change behaviors
![Page 9: Variables, values, expressionsfwp/lecture02/slides2.pdf · 2019. 12. 30. · variables. values have types 5 is a number. x holds a number “Inigo Montoya” is a string. myName holds](https://reader036.vdocuments.mx/reader036/viewer/2022081615/5fe1a604b2d9ae01ee5111b3/html5/thumbnails/9.jpg)
variables change behaviors
![Page 10: Variables, values, expressionsfwp/lecture02/slides2.pdf · 2019. 12. 30. · variables. values have types 5 is a number. x holds a number “Inigo Montoya” is a string. myName holds](https://reader036.vdocuments.mx/reader036/viewer/2022081615/5fe1a604b2d9ae01ee5111b3/html5/thumbnails/10.jpg)
style: good variable namesGood!
Bad!
Good!
![Page 11: Variables, values, expressionsfwp/lecture02/slides2.pdf · 2019. 12. 30. · variables. values have types 5 is a number. x holds a number “Inigo Montoya” is a string. myName holds](https://reader036.vdocuments.mx/reader036/viewer/2022081615/5fe1a604b2d9ae01ee5111b3/html5/thumbnails/11.jpg)
Expressions and operatorsExpression
Operator
Operands
You can use anywhere a value is required:
![Page 12: Variables, values, expressionsfwp/lecture02/slides2.pdf · 2019. 12. 30. · variables. values have types 5 is a number. x holds a number “Inigo Montoya” is a string. myName holds](https://reader036.vdocuments.mx/reader036/viewer/2022081615/5fe1a604b2d9ae01ee5111b3/html5/thumbnails/12.jpg)
Number operators+ addition 5 + 4 9
- subtraction 5 - 19 -14
* multiplication 3 * 6.2 18.6
/ division 19 / 5 3.8
% modulus (remainder) 19 % 5 4
Style note: always surround operators with spaces.
![Page 13: Variables, values, expressionsfwp/lecture02/slides2.pdf · 2019. 12. 30. · variables. values have types 5 is a number. x holds a number “Inigo Montoya” is a string. myName holds](https://reader036.vdocuments.mx/reader036/viewer/2022081615/5fe1a604b2d9ae01ee5111b3/html5/thumbnails/13.jpg)
Boolean operator: !
negation operator: takes one boolean operand, and flips it.
![Page 14: Variables, values, expressionsfwp/lecture02/slides2.pdf · 2019. 12. 30. · variables. values have types 5 is a number. x holds a number “Inigo Montoya” is a string. myName holds](https://reader036.vdocuments.mx/reader036/viewer/2022081615/5fe1a604b2d9ae01ee5111b3/html5/thumbnails/14.jpg)
The assignment operatorMathematics, equality sign: equation
x = 5 means “x is 5, forever”x = 6 0 = 1
Bogus!
Code, = is the assignment operator
Copy value 6 into xCopy value 5 into x
![Page 15: Variables, values, expressionsfwp/lecture02/slides2.pdf · 2019. 12. 30. · variables. values have types 5 is a number. x holds a number “Inigo Montoya” is a string. myName holds](https://reader036.vdocuments.mx/reader036/viewer/2022081615/5fe1a604b2d9ae01ee5111b3/html5/thumbnails/15.jpg)
The assignment operator
Does line 3 mean “x is the number 1 greater than itself”?
![Page 16: Variables, values, expressionsfwp/lecture02/slides2.pdf · 2019. 12. 30. · variables. values have types 5 is a number. x holds a number “Inigo Montoya” is a string. myName holds](https://reader036.vdocuments.mx/reader036/viewer/2022081615/5fe1a604b2d9ae01ee5111b3/html5/thumbnails/16.jpg)
Animation
![Page 17: Variables, values, expressionsfwp/lecture02/slides2.pdf · 2019. 12. 30. · variables. values have types 5 is a number. x holds a number “Inigo Montoya” is a string. myName holds](https://reader036.vdocuments.mx/reader036/viewer/2022081615/5fe1a604b2d9ae01ee5111b3/html5/thumbnails/17.jpg)
Animation: algorithm1. Create variables with initial values (state) 2. Clear the screen 3. Draw picture using variables 4. Change variable values 5. Go back to step two.
You know how to do steps 1… 4
![Page 18: Variables, values, expressionsfwp/lecture02/slides2.pdf · 2019. 12. 30. · variables. values have types 5 is a number. x holds a number “Inigo Montoya” is a string. myName holds](https://reader036.vdocuments.mx/reader036/viewer/2022081615/5fe1a604b2d9ae01ee5111b3/html5/thumbnails/18.jpg)
Animation: draw function
draw is a special function. The browser calls draw repeatedly. (Use it only for animation.) Go run it in lecture 2 notes.
This code should have no effect.
![Page 19: Variables, values, expressionsfwp/lecture02/slides2.pdf · 2019. 12. 30. · variables. values have types 5 is a number. x holds a number “Inigo Montoya” is a string. myName holds](https://reader036.vdocuments.mx/reader036/viewer/2022081615/5fe1a604b2d9ae01ee5111b3/html5/thumbnails/19.jpg)
Exercise: moving smile
![Page 20: Variables, values, expressionsfwp/lecture02/slides2.pdf · 2019. 12. 30. · variables. values have types 5 is a number. x holds a number “Inigo Montoya” is a string. myName holds](https://reader036.vdocuments.mx/reader036/viewer/2022081615/5fe1a604b2d9ae01ee5111b3/html5/thumbnails/20.jpg)
Function calls are expressions
![Page 21: Variables, values, expressionsfwp/lecture02/slides2.pdf · 2019. 12. 30. · variables. values have types 5 is a number. x holds a number “Inigo Montoya” is a string. myName holds](https://reader036.vdocuments.mx/reader036/viewer/2022081615/5fe1a604b2d9ae01ee5111b3/html5/thumbnails/21.jpg)
Converting between types
![Page 22: Variables, values, expressionsfwp/lecture02/slides2.pdf · 2019. 12. 30. · variables. values have types 5 is a number. x holds a number “Inigo Montoya” is a string. myName holds](https://reader036.vdocuments.mx/reader036/viewer/2022081615/5fe1a604b2d9ae01ee5111b3/html5/thumbnails/22.jpg)
Robots and code
Run it now!
![Page 23: Variables, values, expressionsfwp/lecture02/slides2.pdf · 2019. 12. 30. · variables. values have types 5 is a number. x holds a number “Inigo Montoya” is a string. myName holds](https://reader036.vdocuments.mx/reader036/viewer/2022081615/5fe1a604b2d9ae01ee5111b3/html5/thumbnails/23.jpg)
Robots and wall
![Page 24: Variables, values, expressionsfwp/lecture02/slides2.pdf · 2019. 12. 30. · variables. values have types 5 is a number. x holds a number “Inigo Montoya” is a string. myName holds](https://reader036.vdocuments.mx/reader036/viewer/2022081615/5fe1a604b2d9ae01ee5111b3/html5/thumbnails/24.jpg)
Exercise: robot sensor
![Page 25: Variables, values, expressionsfwp/lecture02/slides2.pdf · 2019. 12. 30. · variables. values have types 5 is a number. x holds a number “Inigo Montoya” is a string. myName holds](https://reader036.vdocuments.mx/reader036/viewer/2022081615/5fe1a604b2d9ae01ee5111b3/html5/thumbnails/25.jpg)
Intro to while-loops
• condition is a boolean value • if condition is true, execute
body and test condition again • if condition is false, skip body
![Page 26: Variables, values, expressionsfwp/lecture02/slides2.pdf · 2019. 12. 30. · variables. values have types 5 is a number. x holds a number “Inigo Montoya” is a string. myName holds](https://reader036.vdocuments.mx/reader036/viewer/2022081615/5fe1a604b2d9ae01ee5111b3/html5/thumbnails/26.jpg)
Exercise: robot bonk
![Page 27: Variables, values, expressionsfwp/lecture02/slides2.pdf · 2019. 12. 30. · variables. values have types 5 is a number. x holds a number “Inigo Montoya” is a string. myName holds](https://reader036.vdocuments.mx/reader036/viewer/2022081615/5fe1a604b2d9ae01ee5111b3/html5/thumbnails/27.jpg)
Exercise: robot bonk
![Page 28: Variables, values, expressionsfwp/lecture02/slides2.pdf · 2019. 12. 30. · variables. values have types 5 is a number. x holds a number “Inigo Montoya” is a string. myName holds](https://reader036.vdocuments.mx/reader036/viewer/2022081615/5fe1a604b2d9ae01ee5111b3/html5/thumbnails/28.jpg)
Exercise: robot bonk
![Page 29: Variables, values, expressionsfwp/lecture02/slides2.pdf · 2019. 12. 30. · variables. values have types 5 is a number. x holds a number “Inigo Montoya” is a string. myName holds](https://reader036.vdocuments.mx/reader036/viewer/2022081615/5fe1a604b2d9ae01ee5111b3/html5/thumbnails/29.jpg)
Exercise: driveToWall()
Factor, factor, factor!