Presentation is loading. Please wait.

Presentation is loading. Please wait.

Expressions and Data Types Professor Robin Burke.

Similar presentations


Presentation on theme: "Expressions and Data Types Professor Robin Burke."— Presentation transcript:

1 Expressions and Data Types Professor Robin Burke

2 2 Outline JavaScript review Variables Data types Values Expressions Examples

3 3 JavaScript programs executed by the web browser programs embedded in a web page using the script element programs consist of statements executed sequentially

4 4 Statements End with semi-colon Cannot occupy multiple lines Assignment statement var = value; Function call document.write ("foo");

5 5 Variables (review) A variable is a named location where a value can be stored In JavaScript using a variable creates it can also declare Other languages require that variables be declared before being used Variables have "scope" locations in the program where they are valid topic for later in the class

6 6 Variable declaration Declaration is optional in Javascript Syntax var foo; Says that foo is a variable we will use later

7 7 Data types What is the difference between the following statements val1 = "53"; val2 = 53; These values are different to JavaScript stored differently manipulated differently

8 8 Data Type A characterization of a stored value Determines what kinds of values can be stored how the value is stored internally what operations can be applied Syntactic representation how the value is expressed in a program

9 9 JavaScript Types Strings Integers Floats Boolean

10 10 Strings What values lists of characters any length What operations concatenation (+ operator) output to the web page returned by prompt() function Syntax double or single quotes

11 11 Examples val1 = "53"; val2 = '53'; val3 = val1 + val2; document.write (val3);

12 12 Integers What values whole numbers between -9223372036854775808 and 9223372036854775808 What operations standard mathematical operations (+, -, *, /) special math functions Syntax unquoted integers Note book doesn't use integers in examples

13 13 Examples val1 = 53; val2 = 053; val3 = val1 + val2; Math.sqrt(val3);

14 14 Float What values decimal values from ±1.0x10 308 to ± 1.0x10 -323 17 digits of precision (past decimal point) What operations standard mathematical operations special math functions Syntax unquoted decimal values scientific notation 1.2e3 = 1.2 x 10 3 = 1200

15 15 Examples val1 = 5.3; val2 = 5.3e1; val3 = val1 + val2; Math.floor (val3);

16 16 Boolean What values true / false What operations logical operations and && or || not ! Syntax keywords (true, false)

17 17 Examples val1 = true; val2 = false; val3 = val1 && !val2;

18 18 Another kind of value var foo; document.write (foo); What is the value of foo?

19 19 undefined value This is the value of a variable when it is created if you use a variable without defining it, you get an error You can declare a variable without giving it a value not an error variable has no value unexpected results

20 20 Expressions An expression is a legal combination of Javascript values, operators, function calls and variables that evaluates to a value Examples a + b 5 / 6 3.14159 * r * r Math.sqrt (errorTerm) "foo" + "-" + "bar"

21 21 Syntax alert An expression is not a statement an expression may be part of a statement note: no semi-colon Example (a + b) / 2 expression size = a + b; statement

22 22 Evaluation An expression is evaluated when it is executed (run-time) Steps Each variable is replaced by its current value Operators are applied Until a single value remains

23 23 Example a = 5; b = 20; position = (a * 5) + (b / 10);

24 24 Complex expression Expressions can be large and complex JavaScript doesn't care Readers of your program might care A complex expression can always be simplified by using intermediate variables

25 25 Example complex expression slope = ( (y1 – y2) / (x1 – x2) ); simplified deltaY = y1 – y2; deltaX = x1 – x2; slope = deltaY / deltaX;

26 26 The "+" trap + means different things for different types "foo" + "bar"  "foobar" "5" + "6"  "56" 5 + 6  11 What about? "5" + 6 6 + "foo"

27 27 Operators + is an operator An operator takes two values (sometimes one) makes ("returns") a new value Some operators are two characters && Assignment is not an operation = is not an operator! We run out of characters APL?

28 28 Functions Like an operator takes values does some operation returns a result But has a name instead of symbol can take any number of values can be user-defined but C++

29 29 But wait What about document.write ("foo"); Where is the return value?

30 30 Predefined Functions You can define your own functions later in the class Many built-in prompt document.write

31 31 Function syntax prompt ( "Enter a number", "0") return value is a string containing the user input No parameters? Math.random () there's still a list, just an empty one function nameparameter #1 parameter #2 parameter list

32 32 Examples ftoc.html Write ctof.html mathtest.html power random

33 33 Homework #4 expressions random numbers

34 34 Next class ? Reading Ch. 6


Download ppt "Expressions and Data Types Professor Robin Burke."

Similar presentations


Ads by Google