Programming language components



Similar documents
JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

JavaScript and Dreamweaver Examples

what operations can it perform? how does it perform them? on what kind of data? where are instructions and data stored?

Technical University of Sofia Faculty of Computer Systems and Control. Web Programming. Lecture 4 JavaScript

«W3Schools Home Next Chapter» JavaScript is THE scripting language of the Web.

Lab 5 Introduction to Java Scripts

JavaScript: Control Statements I

Like any function, the UDF can be as simple or as complex as you want. Let's start with an easy one...

Introduction to Java Applets (Deitel chapter 3)

Lab 4.4 Secret Messages: Indexing, Arrays, and Iteration

Web Programming Step by Step

JavaScript Basics & HTML DOM. Sang Shin Java Technology Architect Sun Microsystems, Inc. sang.shin@sun.com

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Programming Languages

Sources: On the Web: Slides will be available on:

First Bytes Programming Lab 2

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

PHP Tutorial From beginner to master

6. Control Structures

1 Description of The Simpletron

Working with forms in PHP

The programming language C. sws1 1

PROG0101 Fundamentals of Programming PROG0101 FUNDAMENTALS OF PROGRAMMING. Chapter 3 Algorithms

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

LAB MANUAL CS (22): Web Technology

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

A PRACTICAL GUIDE TO db CALCULATIONS

c. Write a JavaScript statement to print out as an alert box the value of the third Radio button (whether or not selected) in the second form.

How To Program In Javascript With Deitel And Deitle

Internet Technologies

Real SQL Programming 1

MASTERTAG DEVELOPER GUIDE

C# and Other Languages

JavaScript By: A. Mousavi & P. Broomhead SERG, School of Engineering Design, Brunel University, UK

Hands-on Exercise 1: VBA Coding Basics

Hypercosm. Studio.

Complete this module and Complete the JavaScript module at

Lecture 2 Mathcad Basics

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Introduction to Java

Lab Experience 17. Programming Language Translation

VB.NET Programming Fundamentals

Government Girls Polytechnic, Bilaspur

Visual Logic Instructions and Assignments

VHDL Test Bench Tutorial

Programming in Access VBA

Variables, Constants, and Data Types

Computers. An Introduction to Programming with Python. Programming Languages. Programs and Programming. CCHSG Visit June Dr.-Ing.

if and if-else: Part 1

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

Introduction to Data Structures

JavaScript Introduction

Web Hosting Prep Lab Homework #2 This week: Setup free web hosting for your project Pick domain name and check whether it is available Lots of free

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

I PUC - Computer Science. Practical s Syllabus. Contents

Visual Basic 6 Error Handling

Fireworks 3 Animation and Rollovers

1 Abstract Data Types Information Hiding

Passing 1D arrays to functions.

In mathematics, it is often important to get a handle on the error term of an approximation. For instance, people will write

Example of a Java program

KS3 Computing Group 1 Programme of Study hours per week

Exercise 4 Learning Python language fundamentals

Dialog planning in VoiceXML

Creating Basic Excel Formulas

Independent samples t-test. Dr. Tom Pierce Radford University

>print "hello" [a command in the Python programming language]

McGraw-Hill The McGraw-Hill Companies, Inc.,

Java Basics: Data Types, Variables, and Loops

CSC309 Winter 2016 Lecture 3. Larry Zhang

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Finding XSS in Real World

Chapter 8 Selection 8-1

Using Casio Graphics Calculators

JavaScript For Cats. An introduction for new programmers. Table of contents. Don't be a scaredy-cat. So easy your human companion could do it too!

Exercise 1: Python Language Basics

Microsoft Access 3: Understanding and Creating Queries

PL / SQL Basics. Chapter 3

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Chapter 12 Programming Concepts and Languages

Udacity cs101: Building a Search Engine. Extracting a Link

Web Programming with PHP 5. The right tool for the right job.

arrays C Programming Language - Arrays

INFORMATION BROCHURE Certificate Course in Web Design Using PHP/MySQL

Visual Basic Programming. An Introduction

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Instructions for Embedding a Kudos Display within Your Website

ACADEMIC TECHNOLOGY SUPPORT

Appendix K Introduction to Microsoft Visual C++ 6.0

PYTHON Basics

Custom Javascript In Planning

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

The Little Man Computer

Lexical Analysis and Scanning. Honors Compilers Feb 5 th 2001 Robert Dewar

Intro to Web Programming. using PHP, HTTP, CSS, and Javascript Layton Smith CSE 4000

Introduction to Python

Binary Number System. 16. Binary Numbers. Base 10 digits: Base 2 digits: 0 1

C Compiler Targeting the Java Virtual Machine

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Transcription:

Programming language components syntax: grammar rules for defining legal statements what's grammatically legal? how are things built up from smaller things? semantics: what things mean what do they compute? statements: instructions that say what to do compute values, make decisions, repeat sequences of operations variables: places to hold data in memory while program is running numbers, text,... most languages are higher-level and more expressive than the assembly language for the toy machine statements are much richer, more varied, more expressive variables are much richer, more varied grammar rules are more complicated semantics are more complicated but it's basically the same idea

Why study / use Javascript? all browsers process Javascript many web services rely on Javascript in browser can use it in your own web pages can understand what other web pages are doing (and steal from them) easy to start with easy to do useful things with it programming ideas carry over into other languages Javascript has limitations: very little use outside of web pages many irregularities and surprising behaviors no browser matches ostensible standards exactly doesn't illustrate much about how big programs are built

Javascript components Javascript language statements that tell the computer what to do get user input, display output, set values, do arithmetic, test conditions, repeat groups of statements, libraries, built-in functions pre-fabricated pieces that you don't have to create yourself alert, prompt, math functions, text manipulation,... access to browser and web pages buttons, text areas, images, page contents,... you are not expected to remember syntax or other details you are not expected to write code in exams (though a bit in problem sets and labs) you are expected to understand the ideas how programming and programs work figure out what a tiny program does or why it's broken

Basic example #1: join 2 names (name2.html) Javascript code appears in HTML file between <script> tags <script language=javascript>... </script> shows variables, dialog boxes, an operator <html> <body> <P> name2.html: joins 2 names <script> var firstname, secondname, result; firstname = prompt("enter first name"); secondname = prompt("enter last name"); result = firstname + secondname; // + means "join" here alert("hello, " + result); // and here </script>

Basic example #2: add 2 numbers (add2.html) dialog boxes, variables, arithmetic, conversion <html> <body> <P> add2.html: adds 2 numbers <script> var num1, num2, sum; num1 = prompt("enter first number"); num2 = prompt("enter second number"); sum = parseint(num1) + parseint(num2); // "+" means "add" alert(sum); </script> parseint(...) converts a sequence of characters into its integer value there's also a parsefloat( ) for floating point numbers

Adding up lots of numbers: addup.html variables, operators, expressions, assignment statements while loop, relational operator (!= "not equal to") <html> <body> <script> var sum = 0; var num; num = prompt("enter new value, or 0 to end"); while (num!= 0) { sum = sum + parseint(num); num = prompt("enter new value, or 0 to end"); alert("sum = " + sum); </script>

Find the largest number: max.html needs an If to test whether new number is bigger needs another relational operator needs parseint or parsefloat to treat input as a number var max = 0; var num; num = prompt("enter new value, or 0 to end"); while (num!= 0) { if (parsefloat(num) > max) max = num; num = prompt("enter new value, or 0 to end"); document.write("<p> Max = " + max);

Variables, constants, expressions, operators a variable is a place in memory that holds a value has a name that the programmer gave it, like sum or Area or n in Javascript, can hold any of multiple types, most often numbers like 1 or 3.14, or sequences of characters like "Hello" or "Enter new value" always has a value has to be set to some value initially before it can be used its value will generally change as the program runs ultimately corresponds to a location in memory but it's easier to think of it just as a name for information a constant is an unchanging literal value like 3 or "hello" an expression uses operators, variables and constants to compute a value 3.14 * rad * rad operators include + - * /

Computing area: area.html var rad, area; rad = prompt("enter radius"); while (rad!= null) { area = 3.14 * rad * rad; document.write("<p> radius = " + rad + ", area = " + area); rad = prompt("enter radius"); how to terminate the loop 0 is a valid data value prompt() returns null for Cancel and "" for OK without typing any text string concatenation to build up output line there is no exponentiation operator so we use multiplication

Types, declarations, conversions variables have to be declared in a var statement each variable holds information of a specific type really means that bits are to be interpreted as info of that type internally, 3 and 3.00 and "3.00" are represented differently Javascript usually infers types from context, does conversions automatically "Sum = " + sum sometimes we have to be explicit: parseint(...) if can't tell from context that string is meant as an integer parsefloat(...) if it could have a fractional part

Making decisions and repeating statements if-else statement makes decisions the Javascript version of decisions written with ifzero, ifpos,... if (condition is true) { do this group of statements else { do this group of statements instead while statement repeats groups of statements a Javascript version of loops written with ifzero and goto while (condition is true) { do this group of statements

if-else examples (sign.html) can include else-if sections for a series of decisions: var num = prompt("enter number"); while (num!= null) { num = parseint(num); if (num > 0) { alert(num + " is positive"); else if (num < 0) { alert(num + " is negative"); else { alert(num + " is zero"); num = prompt("enter number");

"while loop" examples counting or "indexed" loop: i = 1; while (i <= 10) { // do something (maybe using the current value of i) i = i + 1; "nested" loops (while.html): var n = prompt("enter number"); while (n!= null) { // "!=" means "is not equal to" i = 0; while (i <= n) { document.write("<br>" + i + " " + i*i); i = i + 1; n = prompt("enter number");

Functions a function is a group of statements that does some computation the statements are collected into one place and given a name other parts of the program can "call" the function that is, use it as a part of whatever they are doing can give it values to use in its computation (arguments or parameters) computes a value that can be used in expressions the value need not be used Javascript provides some useful built-in functions e.g., prompt, alert,... you can write your own functions

Function examples syntax function name (list of "arguments" ) { the statements of the function function definition: function area(r) { return 3.14 * r * r; using ("calling") the function: rad = prompt("enter radius"); alert("radius = " + rad + ", area = " + area(rad)); alert("area of CD =" + area(2.3) - area(0.8));

Ring.html var r1, r2; r1 = prompt("enter radius 1"); while (r1!= null) { r2 = prompt("enter radius 2"); alert("area = " + (area(r1) - area(r2))); // parens needed! r1 = prompt("enter radius 1"); function area(r) { return 3.14 * r * r;

Why use functions? if a computation appears several times in one program a function collects it into one place breaks a big job into smaller, manageable pieces that are separate from each other defines an interface implementation details can be changed as long as it still does the same job different implementations can interoperate multiple people can work on the program a way to use code written by others long ago and far away most of Javascript's library of useful stuff is accessed through functions a good library encourages use of the language

A working sort example var name, i = 0, j, temp; var names = new Array(); // fill the array with names name = prompt("enter new name, or OK to end"); while (name!= "") { names[names.length] = name; name = prompt("enter new name, or OK to end"); // insertion sort for (i = 0; i < names.length-1; i++) { for (j = i+1; j < names.length; j++) { if (names[i] > names[j]) { temp = names[i]; names[i] = names[j]; names[j] = temp; // print names for (i = 0; i < names.length; i++) { document.write("<br> " + names[i]);

Summary: elements of (most) programming languages constants: literal values like 1, 3.14, "Error!" variables: places to store data and results during computing declarations: specify name (and type) of variables, etc. expressions: operations on variables and constants to produce new values assignment: store a new value in a variable statements: assignment, input/output, loop, conditional, call conditionals: compare and branch; if-else loops: repeat statements while a condition is true functions: package a group of statements so they can be called/ used from other places in a program libraries: functions already written for you

How Javascript works recall the process for Fortran, C, etc.: compiler -> assembler -> machine instructions Javascript is analogous, but differs significantly in details when the browser sees Javascript in a web page (<script> tags) passes the Javascript program to a Javascript compiler Javascript compiler checks for errors compiles the program into instructions for something like the toy machine, but richer, more complicated, higher level runs a simulator program (like the toy) that interprets these instructions simulator is often called an "interpreter" or a "virtual machine" probably written in C or C++ but could be written in anything browser and simulator interact when an event like click happens, browser tells Javascript ("onclick") Javascript tells browser to do things (e.g., pop up dialog box for alert)

The process of programming what we saw with Javascript or Toy is like reality, but very small figure out what to do start with a broad specification break into smaller pieces that will work together spell out precise computational steps in a programming language build on a foundation (rarely start from scratch) a programming language that's suitable for expressing the steps components that others have written for you functions from libraries, major components,... which in turn rest on others, often for several layers runs on software (the operating system) that manages the machine it rarely works the first time test to be sure it works, debug if it doesn't evolve as get a better idea of what to do, or as requirements change

Real-world programming the same thing, but on a grand scale programs may be millions of lines of code typical productivity: 1-10K lines/year/programmer thousands of people working on them lifetimes measured in years or even decades big programs need teams, management, coordination, meetings, schedules and deadlines constraints on how fast the program must run, how much memory it can use external criteria for reliability, safety, security, interoperability with other systems, maintenance of old ("legacy") programs is hard programs must evolve to meet changing environments and requirements machines and tools and languages become obsolete expertise disappears