Chapter 8 JavaScript/JScript: Introduction to Scripting 201

Similar documents
JavaScript: Introduction to Scripting

JavaScript: Introductionto Scripting

Copyright 1999 by Deitel & Associates, Inc. All Rights Reserved.

Chapter 10 JavaScript/JScript: Control Structures II 289

The first sample. What is JavaScript?

334 JavaScript/JScript: Functions Chapter 11. boss. worker1 worker2 worker3. Hierarchical boss function/worker function relationship.

Program Design Phase. Algorithm Design - Mathematical. Algorithm Design - Sequence. Verify Algorithm Y = MX + B

People = End Users & Programmers. The Web Browser Application. Program Design Phase. Algorithm Design -Mathematical Y = MX + B

Javascript Lesson 3: Controlled Structures ANDREY KUTASH

Introduction to Programming

C: How to Program. Week /Mar/05

Chapter 9 - JavaScript: Control Structures II

Chapter 2 - Introduction to C Programming

Programming for Engineers Introduction to C

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

JAVASCRIPT LESSON 4: FUNCTIONS

CSC Web Programming. Introduction to JavaScript

Chapter 1 Introduction to Computers and C++ Programming

Lecture 6 Part a: JavaScript

JavaScript: Control Statements I Pearson Education, Inc. All rights reserved.

Introduction to C++ Programming. Adhi Harmoko S, M.Komp

Chapter 11 - JavaScript: Arrays

CHAPTER 5: JavaScript Basics 99

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

182 Introduction to Microsoft Visual InterDev 6 Chapter 7

Full file at C How to Program, 6/e Multiple Choice Test Bank

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

Introduction to C# Applications

Chapter 1 & 2 Introduction to C Language

Course PJL. Arithmetic Operations

JavaScript: Functions Pearson Education, Inc. All rights reserved.

Exercise 1: Basic HTML and JavaScript

Flow of Control. Relational Operators. Operators Review. Order of Operations

A.A. 2008/09. Why introduce JavaScript. G. Cecchetti Internet Software Technologies

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Introduction to Python Programming

Programming Basics. INFO/CSE 100, Spring 2005 Fluency in Information Technology.

JavaScript: Control Statements I

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

IT 374 C# and Applications/ IT695 C# Data Structures

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

Mobile Site Development

IS 0020 Program Design and Software Tools

Introduction to Java Applications; Input/Output and Operators

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Introduction to C# Applications Pearson Education, Inc. All rights reserved.

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

Chapter 2, Part I Introduction to C Programming

by Pearson Education, Inc. All Rights Reserved. 2

Programming Basics. INFO/CSE 100, Spring 2006 Fluency in Information Technology.

Crayon (.cry) Language Reference Manual. Naman Agrawal (na2603) Vaidehi Dalmia (vd2302) Ganesh Ravichandran (gr2483) David Smart (ds3361)

Working with JavaScript

Learning the Language - V

CISC 110 Day 1. Hardware, Algorithms, and Programming

CS 106 Introduction to Computer Science I

Fundamentals of Programming Session 4

Web Scripting using PHP

Chapter 1 Getting Started with HTML 5 1. Chapter 2 Introduction to New Elements in HTML 5 21

Chapter 2 Working with Data Types and Operators

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

Place User-Defined Functions in the HEAD Section

Starting Excel application

JAVASCRIPT. Computer Science & Engineering LOGO

Chapter 17. Fundamental Concepts Expressed in JavaScript

Lecture 2. The variable 'x' can store integers, characters, string, float, boolean.

Data Grid Utilities - Searching Data Grids

4 WORKING WITH DATA TYPES AND OPERATIONS

Data types, variables, constants

ANSI C Programming Simple Programs

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Chapter 2: Using Data

Chapter 1 Introduction to Computers and C++ Programming

Lecture 3 Tao Wang 1

Input/Output Week 5:Lesson 16.1

Spreadsheet EXCEL. Popular Programs. Applications. Microsoft Excel. What is a Spreadsheet composed of?

A Balanced Introduction to Computer Science, 3/E

download instant at Introduction to C++

4. Inputting data or messages to a function is called passing data to the function.

3 The Building Blocks: Data Types, Literals, and Variables

COMP519 Web Programming Lecture 11: JavaScript (Part 2) Handouts

Lesson 1: Writing Your First JavaScript

CS 374 Fall 2014 Homework 2 Due Tuesday, September 16, 2014 at noon

Full file at

Let's Look Back. We talked about how to create a form in HTML. Forms are one way to interact with users

Chapter 2, Part III Arithmetic Operators and Decision Making

Fundamentals of Programming CS-110. Lecture 3

Date Issued: Subject: Editing the HTML files in the C-more panel. Revision: Original

Chapter 1 Introduction to Computers and the Internet

INTRODUCTION TO JAVASCRIPT

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Course Outline. Introduction to java

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

CMPT 125: Lecture 3 Data and Expressions

COMS 469: Interactive Media II

A First Program - Greeting.cpp

Introduction to JavaScript

Microsoft Excel 2010

Transcription:

iw3htp_08.fm Page 201 Thursday, April 13, 2000 12:30 PM Chapter 8 JavaScript/JScript: Introduction to Scripting 201 8 JavaScript/JScript: Introduction to Scripting 1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 2 <!-- Fig. 8.1: welcome.html --> 3 4 <HTML> 5 <HEAD> 6 <TITLE>A First Program in JavaScript</TITLE> 7 8 <SCRIPT LANGUAGE = "JavaScript"> 9 document.writeln( 10 "<H1>Welcome to JavaScript Programming!</H1>" ); 11 </SCRIPT> 12 13 </HEAD><BODY></BODY> 14 </HTML> Title of the HTML document Location and name of the loaded HTML document Script result Fig. 8.1 A first program in JavaScript.

iw3htp_08.fm Page 202 Thursday, April 13, 2000 12:30 PM 202 JavaScript/JScript: Introduction to Scripting Chapter 8 1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 2 <HTML> 3 <!-- Fig. 8.2: welcome.html --> 4 5 <HEAD> 6 <TITLE>Printing a Line with Multiple Statements</TITLE> 7 8 <SCRIPT LANGUAGE = "JavaScript"> 9 document.write( "<FONT COLOR='magenta'><H1>Welcome to " ); 10 document.writeln( "JavaScript Programming!</H1></FONT>" ); 11 </SCRIPT> 12 13 </HEAD><BODY></BODY> 14 </HTML> Fig. 8.2 Printing on one line with separate statements.

iw3htp_08.fm Page 203 Thursday, April 13, 2000 12:30 PM Chapter 8 JavaScript/JScript: Introduction to Scripting 203 1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 2 <HTML> 3 <!-- Fig. 8.3: welcome.html --> 4 5 <HEAD><TITLE>Printing Multiple Lines</TITLE> 6 7 <SCRIPT LANGUAGE = "JavaScript"> 8 document.writeln( 9 "<H1>Welcome to<br>javascript<br>programming!</h1>" ); 10 </SCRIPT> 11 12 </HEAD><BODY></BODY> 13 </HTML> Fig. 8.3 Printing on multiple lines with a single statement.

iw3htp_08.fm Page 204 Thursday, April 13, 2000 12:30 PM 204 JavaScript/JScript: Introduction to Scripting Chapter 8 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 2 <HTML> 3 <!-- Fig. 8.4: welcome.html --> 4 <!-- Printing multiple lines in a dialog box --> 5 6 <HEAD> 7 8 <SCRIPT LANGUAGE = "JavaScript"> 9 window.alert( "Welcome to\njavascript\nprogramming!" ); 10 </SCRIPT> 11 12 </HEAD> 13 14 <BODY> 15 <P>Click Refresh (or Reload) to run this script again.</p> 16 </BODY> 17 </HTML> Title bar The OK button allows the user to dismiss (or hide) the dialog box. The dialog box is automatically sized to accommodate the string. Mouse cursor Fig. 8.4 Displaying multiple lines in a dialog box.

iw3htp_08.fm Page 205 Thursday, April 13, 2000 12:30 PM Chapter 8 JavaScript/JScript: Introduction to Scripting 205 Escape sequence Description \n Newline. Position the screen cursor to the beginning of the next line. \t Horizontal tab. Move the screen cursor to the next tab stop. \r Carriage return. Position the screen cursor to the beginning of the current line; do not advance to the next line. Any characters output after the carriage return overwrite the previous characters output on that line. \\ Backslash. Used to represent a backslash character in a string. \" Double quote. Used to represent a double quote character in a string contained in double quotes. For example, window.alert( "\"in quotes\"" ); displays "in quotes" in an alert dialog. \ Single quote. Used to represent a single quote character in a string. For example, window.alert( \ in quotes\ ); displays in quotes in an alert dialog. Fig. 8.5 Some common escape sequences.

iw3htp_08.fm Page 206 Thursday, April 13, 2000 12:30 PM 206 JavaScript/JScript: Introduction to Scripting Chapter 8 1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 2 <HTML> 3 <!-- Fig. 8.6: Addition.html --> 4 5 <HEAD> 6 <TITLE>An Addition Program</TITLE> 7 8 <SCRIPT LANGUAGE = "JavaScript"> 9 var firstnumber, // first string entered by user 10 secondnumber, // second string entered by user 11 number1, // first number to add 12 number2, // second number to add 13 sum; // sum of number1 and number2 14 15 // read in first number from user as a string 16 firstnumber = window.prompt( "Enter first integer", "0" ); 17 18 // read in second number from user as a string 19 secondnumber = window.prompt( "Enter second integer", "0" ); 20 21 // convert numbers from strings to integers 22 number1 = parseint( firstnumber ); 23 number2 = parseint( secondnumber ); 24 25 // add the numbers 26 sum = number1 + number2; 27 28 // display the results 29 document.writeln( "<H1>The sum is " + sum + "</H1>" ); 30 </SCRIPT> 31 32 </HEAD> 33 <BODY> 34 <P>Click Refresh (or Reload) to run the script again</p> 35 </BODY> 36 </HTML> Fig. 8.6 An addition script in action (part 1 of 2).

iw3htp_08.fm Page 207 Thursday, April 13, 2000 12:30 PM Chapter 8 JavaScript/JScript: Introduction to Scripting 207 Fig. 8.6 An addition script in action (part 2 of 2).

iw3htp_08.fm Page 208 Thursday, April 13, 2000 12:30 PM 208 JavaScript/JScript: Introduction to Scripting Chapter 8 number1 45 Fig. 8.7 Memory location showing the name and value of variable number1.

iw3htp_08.fm Page 209 Thursday, April 13, 2000 12:30 PM Chapter 8 JavaScript/JScript: Introduction to Scripting 209 number1 45 number2 72 Fig. 8.8 Memory locations after values for variables number1 and number2 have been input.

iw3htp_08.fm Page 210 Thursday, April 13, 2000 12:30 PM 210 JavaScript/JScript: Introduction to Scripting Chapter 8 number1 45 number2 72 sum 117 Fig. 8.9 Memory locations after a calculation.

iw3htp_08.fm Page 211 Thursday, April 13, 2000 12:30 PM Chapter 8 JavaScript/JScript: Introduction to Scripting 211 JavaScript operation Arithmetic operator Algebraic expression JavaScript expression Addition + f + 7 f + 7 Subtraction - p c p - c Multiplication * bm b * m Division / x x / y x / y or - or x y y Modulus % r mod s r % s Fig. 8.10 Arithmetic operators.

iw3htp_08.fm Page 212 Thursday, April 13, 2000 12:30 PM 212 JavaScript/JScript: Introduction to Scripting Chapter 8 Operator(s) Operation(s) Order of evaluation (precedence) ( ) Parentheses Evaluated first. If the parentheses are nested, the expression in the innermost pair is evaluated first. If there are several pairs of parentheses on the same level (not nested), they are evaluated left to right. *, / or % Multiplication Division Modulus + or - Addition Subtraction Evaluated second. If there are several, they are evaluated left to right. Evaluated last. If there are several, they are evaluated left to right. Fig. 8.11 Precedence of arithmetic operators.

iw3htp_08.fm Page 213 Thursday, April 13, 2000 12:30 PM Chapter 8 JavaScript/JScript: Introduction to Scripting 213 Step 1. y = 2 * 5 * 5 + 3 * 5 + 7; 2 * 5 is 10 (Leftmost multiplication) Step 2. y = 10 * 5 + 3 * 5 + 7; 10 * 5 is 50 (Leftmost multiplication) Step 3. y = 50 + 3 * 5 + 7; 3 * 5 is 15 (Multiplication before addition) Step 4. y = 50 + 15 + 7; 50 + 15 is 65 (Leftmost addition) Step 5. y = 65 + 7; 65 + 7 is 72 (Last addition) Step 6. y = 72; (Last operation assignment) Fig. 8.12 Order in which a second-degree polynomial is evaluated.

iw3htp_08.fm Page 214 Thursday, April 13, 2000 12:30 PM 214 JavaScript/JScript: Introduction to Scripting Chapter 8 Standard algebraic equality operator or relational operator JavaScript equality or relational operator Sample JavaScript condition Meaning of JavaScript condition Equality operators = == x == y x is equal to y!= x!= y x is not equal to y Relational operators > > x > y x is greater than y < < x < y x is less than y >= x >= y x is greater than or equal to y <= x <= y x is less than or equal to y Fig. 8.13 Equality and relational operators.

iw3htp_08.fm Page 215 Thursday, April 13, 2000 12:30 PM Chapter 8 JavaScript/JScript: Introduction to Scripting 215 1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 2 <HTML> 3 <!-- Fig. 8.14: comparison.html --> 4 <!-- Using if statements, relational operators, --> 5 <!-- and equality operators --> 6 7 <HEAD> 8 <TITLE>Performing Comparisons</TITLE> 9 10 <SCRIPT LANGUAGE = "JavaScript"> 11 var first, // first string entered by user 12 second; // second string entered by user 13 14 // read first number from user as a string 15 first = window.prompt( "Enter first integer:", "0" ); 16 17 // read second number from user as a string 18 second = window.prompt( "Enter second integer:", "0" ); 19 20 document.writeln( "<H1>Comparison Results</H1>" ); 21 document.writeln( "<TABLE BORDER = '1' WIDTH = '100%'>" ); 22 23 if ( first == second ) 24 document.writeln( "<TR><TD>" + first + " == " + second + 25 "</TD></TR>" ); 26 27 if ( first!= second ) 28 document.writeln( "<TR><TD>" + first + "!= " + second + 29 "</TD></TR>" ); 30 31 if ( first < second ) 32 document.writeln( "<TR><TD>" + first + " < " + second + 33 "</TD></TR>" ); 34 35 if ( first > second ) 36 document.writeln( "<TR><TD>" + first + " > " + second + 37 "</TD></TR>" ); 38 39 if ( first <= second ) 40 document.writeln( "<TR><TD>" + first + " <= " + second + 41 "</TD></TR>" ); 42 43 if ( first >= second ) 44 document.writeln( "<TR><TD>" + first + " >= " + second + 45 "</TD></TR>" ); 46 47 // Display results 48 document.writeln( "</TABLE>" ); 49 </SCRIPT> 50 51 </HEAD> Fig. 8.14 Using equality and relational operators.

iw3htp_08.fm Page 216 Thursday, April 13, 2000 12:30 PM 216 JavaScript/JScript: Introduction to Scripting Chapter 8 52 <BODY> 53 <P>Click Refresh (or Reload) to run the script again</p> 54 </BODY> 55 </HTML> Fig. 8.14 Using equality and relational operators.

iw3htp_08.fm Page 217 Thursday, April 13, 2000 12:30 PM Chapter 8 JavaScript/JScript: Introduction to Scripting 217 Fig. 8.14 Using equality and relational operators.

iw3htp_08.fm Page 218 Thursday, April 13, 2000 12:30 PM 218 JavaScript/JScript: Introduction to Scripting Chapter 8 Fig. 8.14 Using equality and relational operators.

iw3htp_08.fm Page 219 Thursday, April 13, 2000 12:30 PM Chapter 8 JavaScript/JScript: Introduction to Scripting 219 Operators Associativity Type () left to right parentheses * / % left to right multiplicative + - left to right additive < <= > >= left to right relational ==!= left to right equality = right to left assignment Fig. 8.15 Precedence and associativity of the operators discussed so far.