Data And Variables Chapter 18. 2 Names vs. Values Michael Jordan name (the letter sequence used to refer to something) value (the thing itself)

Slides:



Advertisements
Similar presentations
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Advertisements

Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 2: Primitive Data and Definite Loops.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
JavaScript, Fourth Edition
JavaScript, Third Edition
Introduction to C Programming
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
2440: 211 Interactive Web Programming Expressions & Operators.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
1 Primitive data types, expressions, and variables.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Chapter 2 Variables.
Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals.
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
1 Building Java Programs Chapter 2: Primitive Data and Definite Loops.
1 Variables and Arithmetic Operators in JavaScript.
Chapter Two Fundamental Programming Structures in Java Adapted from MIT-AITI slides Variables and Primitive Data Types 1.
Topic 4 Expressions and variables Based on slides bu Marty Stepp and Stuart Reges from "Once a person has understood.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CS 106A, Lecture 4 Introduction to Java
Chapter 2 Variables.
Chapter 6 JavaScript: Introduction to Scripting
BASIC ELEMENTS OF A COMPUTER PROGRAM
Lecture 4: Expressions and Variables
Visual Basic Variables
Building Java Programs
Overview: Programming Concepts
Variables and Arithmetic Operators in JavaScript
Primitive Data, Variables, Loops (Maybe)
Building Java Programs Chapter 2
Topic 4 Expressions and variables
Variables ICS2O.
Lecture 3: Expressions and Variables
Building Java Programs
Building Java Programs
Chapter 2 Variables.
Topic 4 Expressions and variables
Building Java Programs
Building Java Programs Chapter 2
Lecture 5: Basic Java Syntax AP Computer Science Principles
Building Java Programs
Chapter 2: Introduction to C++.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Lecture 4: Expressions and Variables
Building Java Programs
Building Java Programs
Tutorial 10: Programming with javascript
Building Java Programs
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Chapter 2 Variables.
Building Java Programs
Building Java Programs
Topic 4 Expressions and variables
Building Java Programs
Building Java Programs
Presentation transcript:

Data And Variables Chapter 18

2 Names vs. Values Michael Jordan name (the letter sequence used to refer to something) value (the thing itself)

3 Names Have Changing Values We do not tend to distinguish between names and values, because things do not magically transform into other things. In programming, names and values are separable.  Think of names as offices or titles, like "U.S. President"  Example: Previous values of the name "U.S. President":

4 Names Have Changing Values Another way to think of names is as labeled boxes. We refer to these labeled boxes as variables. variable: a piece of your computer's memory that is given a name and can store a value U.S. President value variable

5 Identifier identifier: the letter sequence that makes up a variable's name  Must begin with a letter, underscore (_), or dollar sign ($)  Following characters can also include digits  Cannot contain spaces Identifiers are case-sensitive.  Example: Fred and fred and FrEd are NOT the same.

6 Valid XX xx  height  Time_of_day  oOoOoOo  w1o2o3h4o5o  Identifiers_can_be_long_but_typing_them_can_be_a_pain Variables should have meaningful identifiers that are descriptive of the value stored in the variable. Examples Invalid  Michael Jordan  1stValue  yay!  mininum-number space not allowed

7 Keywords The following list are keywords that have special meaning in JavaScript and thus may not be used as identifiers. abstract booleanbreakbytecase catchcharclassconstcontinue debuggerdefaultdeletedodouble elseenumexportextendsfalse finalfinallyfloatforfunction gotoifimplementsimportin instanceofintinterfacelongnative newnullpackageprivateprotected publicreturnshortstaticsuper switchsynchronizedthisthrowthrows transienttruetrytypeofvar voidvolatilewhilewith

8 Keywords As JavaScript is case-sensitive, you could technically use Class or cLaSs as identifiers, but this is very confusing and thus strongly discouraged.

9 Declaring Variables Have to tell computer what variables you want. Variable declaration syntax: var ; Examples: var x; var myGPA;

10 Syntax syntax: set of legal structures and commands that can be used Example:  Every basic statement ends with a semi-colon.

11 Declaring a variable sets aside a piece of memory in which you can store a value. var x; var y;  Inside the computer: x: ?y: ? (The memory has no value yet. It is undefined.) Declaring Variables

12 Declaring Multiple Variable At Once Can declare multiple variables at once: var,, …, ; Example: var x, y, z; x: ?y: ?z: ?

13 Exercise What is the difference between the following two sets of variable declarations? var Alpha, Beta; var beta, alpha; Both will create two variables. However, the two statements will declare different variables, because JavaScript is case-sensitive.  The order of the variables in a declaration is unimportant.

14 Expression expression: data value or a set of operations that produces a value Examples: * 3 3 (1 - 2) / 3 * "yay!" 'hello' see slide #28 on "Strings"

15 Operators Arithmetic operators we will use:  + addition  - subtraction or negation  * multiplication  / division

16 Evaluating Expressions When the computer executes (runs) a program and encounters an expression, the expression is evaluated (i.e., computed).  Example: 3 * 4 evaluates to 12

17 Precedence: Remember PEMDAS? precedence: order in which operations are computed in an expression  Operators on the same level are evaluated from left to right. Example: is 2 (not -4 )  Spacing does not affect order of evaluation. Example: 1+3 * 4-2 is 11 Parentheses () Multiplication, Division * / Addition, Subtraction + -

18 Assigning Values To Variables assignment statement: statement that stores a value into a variable Assignment statement syntax: = ; Examples: myGPA = 3.25;x: 8myGPA: 3.25 x = 2 * (1 + 3);

19 Assignment vs. Algebra The assignment statement is not an algebraic equation! = ; means:  "store the value of into " Some people read x = 3 * 4; as  " x gets the value of 3 * 4 " ERROR: 3 = 1 + 2; is an illegal statement, because 3 is not a variable.

20 Assigning Values To Variables We often know an initial value for the variables we declare. A variable can be declared and assigned an initial value in the same statement. Declaration/initialization statement syntax: var = ; Example: var taxRate = 0.088;

21 Declaring/Initializing Multiple Variables Can declare/initialize multiple variables at once: var =, …, = ; Examples: var taxRate = 0.088, balance, years = 15; taxRate: balance: ? years: 15

22 Using Variables Once a variable has been assigned a value, it can be used in expressions. var x = 2 * 4; var y = x * 5 - 1;  The above statement is equivalent to: var y = 8 * 5 - 1;

23 Assigning Variables A variable can be assigned a value more than once. Example: var x = 1.5; x = 3; x = 4 + 7;// x now has a // value of 11

24 Assignment Conundrum What happens when a variable is used on both sides of an assignment statement? var x = 3; x = x + 2; // what happens? Answer: x now has a value of 5.

25 Exercise var x; x = 3; var y; y = x; x = 5; What is in x ? What is in y ?  x has the value of 5  y has the value of 3

26 What Can We Put In Variables? For now, we will use three types of data:  number  string  Boolean

27 Writing Numbers Valid: Invalid: 8.8% $99 1,234 20kg Do not write units, percent signs, dollar signs, or commas.

28 String string: A sequence of text characters.  Start and end with single or double quotation mark characters  Starting and ending quotation marks must match (both single or both double) Examples: 'hello' "This is a string" "This, too, is a string. It can be very long!" 'Bob said: "You stink!"'

29 More On Strings A string may not span across multiple lines. "This is not a legal string." The minimum number of characters in a string is zero, which is called the empty string. Examples: "" ''

30 Boolean There are only two Boolean values  true  false Remember, JavaScript is case-sensitive, so False is not a Boolean value. Boolean values will be used when we discuss conditionals in a future lecture.