variables, values, expressionsfwp/lecture02/slides2.pdf · 2019. 12. 30. · variables. values have...

29
Variables, values, expressions

Upload: others

Post on 30-Aug-2020

3 views

Category:

Documents


0 download

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Exercise: driveToWall()

Factor, factor, factor!