David Stotts Computer Science Department UNC Chapel Hill.

Slides:



Advertisements
Similar presentations
INFM 603: Information Technology and Organizational Context Jimmy Lin The iSchool University of Maryland Thursday, September 19, 2013 Session 3: JavaScript.
Advertisements

Python Basics: Statements Expressions Loops Strings Functions.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 63 – Manipulating Data Using Methods – Day 2.
The Web Warrior Guide to Web Design Technologies
David Stotts Computer Science Department UNC Chapel Hill.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
10 November JavaScript. Presentation Hints What do YOU think makes a good presentation Some of my suggestions Don’t write full sentences on slides Talk,
Tutorial 10 Programming with JavaScript
Guide To UNIX Using Linux Third Edition
WEB DESIGN AND PROGRAMMING Introduction to Javascript.
by Chris Brown under Prof. Susan Rodger Duke University June 2012
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Introduction to Python
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
David Stotts Computer Science Department UNC Chapel Hill.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
What is Java Script? An extension to HTML. An extension to HTML. Allows authors to incorporate some functionality in their web pages. (without using CGI.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Tutorial 10 Programming with JavaScript. XP Objectives Learn the history of JavaScript Create a script element Understand basic JavaScript syntax Write.
Tutorial 10 Programming with JavaScript
Done by: Hanadi Muhsen1 Tutorial 1.  Learn the history of JavaScript  Create a script element  Write text to a Web page with JavaScript  Understand.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
DHTML AND JAVASCRIPT Genetic Computer School LESSON 5 INTRODUCTION JAVASCRIPT G H E F.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
ITBP 119 Algorithms and Problem Solving Section 2.1 Installing software Section 2.2 First Programs Section 2.3 Variables.
David Stotts Computer Science Department UNC Chapel Hill.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
David Stotts Computer Science Department UNC Chapel Hill.
CPS120: Introduction to Computer Science Decision Making in Programs.
Introduction to Programming with RAPTOR
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (there is an audio component to this eLesson) © Dr.
David Stotts Computer Science Department UNC Chapel Hill.
David Stotts Computer Science Department UNC Chapel Hill.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
JavaScript, Fourth Edition
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 2 (Tuesday)
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
JavaScript Events Java 4 Understanding Events Events add interactivity between the web page and the user You can think of an event as a trigger that.
David Stotts Computer Science Department UNC Chapel Hill.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
JavaScript and AJAX 2nd Edition Tutorial 1 Programming with JavaScript.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
David Stotts Computer Science Department UNC Chapel Hill.
© 2010 Robert K. Moniot1 Chapter 6 Introduction to JavaScript.
JavaScript Part 1 Introduction to scripting The ‘alert’ function.
Chapter 6 JavaScript: Introduction to Scripting
Tutorial 10 Programming with JavaScript
Learning to Program D is for Digital.
Introduction to Computer Science / Procedural – 67130
Variables, Expressions, and IO
Building Java Programs Chapter 2
T. Jumana Abu Shmais – AOU - Riyadh
Programming in JavaScript
Programming in JavaScript
JavaScript: Introduction to Scripting
Javascript Chapter 19 and 20 5/3/2019.
Presentation transcript:

David Stotts Computer Science Department UNC Chapel Hill

0. data (types, simple information) 1. data storage (variables, assignment) 2. data retrieval (expressions, evaluation) 3. repetition (loops) 4. decision making (conditionals) 5. procedure abstraction (functions) 6. data abstraction (arrays) 7. objects: all-the-above, wrapped up

“You are sad,” the Knight said in an anxious tone: “Let me sing you a song to comfort you.” The name of the song is called ‘Haddocks' Eyes.’” “Oh, that's the name of the song, is it?" Alice said, trying to feel interested. “No, you don't understand,” the Knight said, looking a little vexed. “That's what the name is called. The name really is ‘The Aged Aged Man.’ ” “Then I ought to have said ‘That's what the song is called’?” Alice corrected herself. “No, you oughtn't: that's quite another thing! The song is called ‘Ways And Means’: but that's only what it's called, you know!” “Well, what is the song, then?” said Alice, by this time completely bewildered. “I was coming to that,” the Knight said. “The song really is ‘A-sitting On A Gate’: and the tune's my own invention.” Lewis Carroll, Through The Looking Glass

 The White Knight gives a confusing pile of nomenclature for this song. ◦ The song is A-sitting On a Gate ◦ The song is called Ways and Means ◦ The song's name is The Aged Aged Man ◦ The song's name is called Haddocks' Eyes  The complicated terminology distinguishing between the song, the name of the song, and what the name is called' epistemologists and linguists call the use–mention distinction.use–mention distinction  In programming we see these issues in using symbolic names for data values and memory locations in the computer: values, variables, references, bindings, scope

data storage (variables, assignment)  Data values must be stored in the computer memory for a program to operate on them  Data storage is done by the assignment statement  Assignment is putting a data value into a storage location in memory  Such a storage location used by your program is called a variable

 “Variable” means the stored value can vary different values can be stored in it at different times  A variable has a text name  Example valid names x speed age trip_3 _price total$ $amount $_$ totalYearsWorked 1b2c3d4e5nnnnnnnn1j

You can think of a variable as a name for a data container or box… a storage area in memory age speed title “The Island of Dr. Moreau”

How the assignment statement works age = 47 variable (LHS) assignment operator data value (RHS) read this way: variable “age” gets value 47 age 47

Example assignment statements age = 47 ; speed = ; title = “The Island of Dr. Moreau” ; gearRatio_3 = 0.27 ; done = false ; Note the “;” at the end… put one at the end of every JavaScript statement

put a semi-colon at the end of every JavaScript statement Cool, can do… oh, um… what is a “statement” ? A statement is the basic syntax unit from which programs are composed A program executes by going from one statement to the next and doing what each statement calls for So far all we know about are ◦ assignment statement ◦ alert statement ( a function call )

0. data (types, simple information ) 1. data storage (variables, assignment ) 2. data retrieval (expressions, evaluation ) 3. repetition (loops ) 4. decision making (conditionals ) 5. procedure abstraction (functions ) 6. data abstraction (arrays ) 7. objects: all-the-above, wrapped up

 We are looking at a lot of detail here It will seem simpler soon when we put it all together  Here at the beginning we want to emphasize the principles at work  In assignment we have these all glommed up data data storage data retrieval

data retrieval ( expressions, evaluation )  Once data values are stored in the computer memory we need a way to get them back for use in the program  Data retrieval is done using expressions and evaluation  Expressions are formulae built from variables, values, operations, expressions, ( )  Examples: x 36 speed age / 2.0 x – age + 3 !done “go”+“tarheels” 12.1 * (43 % 7) ((x * y) + (3 * temperature)) / ( dx ) done || found

 Evaluating an expression means turning the formula into a data value  Rules define how this is systematically done  A constant evaluates to itself 5 stands for the integer value 5  A variable evaluates to the data value stored in its memory location x x now stands for the number value

 A complex expression is evaluated by first evaluating the simple parts, then combining the values using the operations dx dy  dx + dy evaluates to then  total = dx + dy causes this memory state:  total

2011, big Internet argument over this What is 48/2(9+3) ? Answer depends on order of evaluation Also on how we handle the implied multiplication Is it 48/(2*(9+3)) … or 48/24 … or 2 Is it 48/2*(9+3) and we use P E MD AS ? 48/2*12 … or 24*12 … or 288

let’s try Google and then JavaScript

 JavaScript has no implied multiplication  No implied anything in expressions  Must put in all operators  Put parentheses in liberally just to be sure you get the program to do what you intend  Lots of parentheses don’t cause “extra work” but they do cause extra clarity

 Now we can go back to our first program and make some changes… our second programsecond program var years; // variable declaration var months; // variable declaration years = 42; // assignment months = years*12; // expression evaluation // and assignment alert(“If a person is age " + years); alert("In months, this is " + months); alert(years + " years is " + months + " months");

 Your author laments all the prior programming instructors who insisted on lots of rules  Says they limit the natural awe and wonder of students who are having their captive minds freed by programming...  While I am overall sympathetic to the point, we do have some rules here  They are for your own comfort and safety… like “keep head and hands inside the vehicle while the train is in motion”… and...

 Declare your variables ! no, srsly … declare your variables

JavaScript allows undeclared variables, but they can cause (quietly) executions errors error console Open this up to see compile and run messages Firefox: find it in the “web developer” menu “use strict”; put this as the first line in your program put it exactly, quotes and all gives error message if variable is undeclared

Some operations convert values of one type into value of a different type alert( “message” )  a popup box Number(“25.4”)  25.4 negative(-73)  true Some operations convert values of one type into different values of the same type floor(5.2)  5

We call these functions A name, some arguments inside parentheses Number(“25.4”) length(“abc”) negative(73) myFunc( -12, ”no”, ) When you see a function name with argument values you expect it to run and produce a result value

Sometimes we will see a function invoked via “dot notation” var line = “12.75” ; line.length  5 line.parseInt()  (a number, not a string) This will be more prominent later in the course

A function name with argument values is referred to as a function call A function call produces a value (so does an expression) So a function call can appear as a part of an expression months = 12 * Number(“47”); miles = Math.floor(time*speed) + init;

 You began the “big six” concepts with 0. Data numbers, booleans, strings operators that work on each type  You saw 1. Data Storage variables and assignment  You saw 2. Data Retrieval expressions and evaluation  You saw function calls as expressions  You wrote another program  and… Rule 1: declare your variables!

 Finish reading chapter 1 in the text  Practice with the programs on the web site… add stuff to them, try things and see what happens  Take a look at the HTML tags document under “Resources” on the class web page  Maybe try to spice up the web page part of your practice program(s)

 One way to understand how your programs work is to “play computer”  To do this we will draw a diagram to represent the computer memory, with variable names on the memory slots.  Go through your program, line by line, in the order the computer will execute them  Mark on the memory diagram each change the program makes to stored values (assignments)

Demo the technique on the whiteboard or document camera Remember that a variable starts out with no value in it until some assignment happens years months * In the CPU: line “expression evaluation” assignment 

 Our third program: getting input from the userthird program var years; var months; years = prompt("Age of the person (in years)?"); months = years * 12 ; alert("The person is age " + years); alert("In months, this is " + months); alert(years+" years is "+months+" months")

 There are several ways to do I/O in a Javascript program. One input way is the prompt operation syntax : someVar = prompt ( string );  The string is printed in a popup box and the user is given a text field into which to type a value  The value the user types is assigned to someVar  Warning: prompt gets text from the user (a string), since text is what keyboard typing generates  If you want a number from the user, you must make sure the text gets converted properly

 To get number input from a user someVar = Number( prompt ( string ) );  The Number function makes a numeric data item out of the text the user types (think Haddock’s Eyes here)  “127” is text, just like “UNC” is text, and it makes no sense to add, divide, etc. on it  127 is an integer number, and it makes sense to add, subtract, divide, etc. on it  Difference is in the internal storage formats

 You know what a variable is, and how to establish one in a JavaScript program  You know how to store data into a variable with the assignment statement  You know how to retrieve data stored in a variable using expressions and evaluation  You know how to simulate execution of your program using a memory diagram  You know how to do I/O in Javascript using prompt (for input) and alert (for output) and how to convert text into numbers