CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications...

41
CSCE 102 – Chapter 7 (Intro. to Program. & CSCE 102 - General Applications Programming Benito Mendoza 1 22/6/27 Benito Mendoza 1 By Benito Mendoza Department of Computer Science & Engineering Introduction to Programming Algorithms and Control Structures JavaScript

Transcript of CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications...

Page 1: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript )

CSCE 102 - General Applications Programming

CSCE 102 - General Applications Programming

Benito Mendoza 123/4/21 Benito Mendoza 1

By

Benito Mendoza

Department of Computer Science & Engineering

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

Page 2: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 2

Low-level vs High-level

Low-level languages such as assembly language take many months to master and are dependent on the processor hardware usedHigh-level languages have more high end features which are easier to learn

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

A Hierarchy of Languages

Page 3: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 3

Interpreters vs CompilersLanguages must be translated into machine code for the computer to executeInterpreters do this on the fly as the program is being executedCompilers convert the program into machine code prior to the program being executed and store the machine code in a file to be run laterCompiled programs run faster than interpreted onesInterpreted programs are easier to debug because you do not have to compile each time a change is made

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

A Hierarchy of Languages

Page 4: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 4

Structured vs. Object-Oriented Structured programming languages were developed to solve sloppy programming practices that led to errors. However, these languages did not work well for large programming tasks that involved multiple developersObject-oriented programming languages provide the compartmentalization that the structured languages were missing enabling many developers to work together on the same project

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

A Hierarchy of Languages

Page 5: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 5

Computers make decisions by using control structures in their programsAlgorithms are the combination of small steps that allow a particular problem to be solved

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

Page 6: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 6

Brute ForceTraveling Salesman Problem

Programming TechniquesIntroduction to ProgrammingAlgorithms and Control Structures

JavaScript

Page 7: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 7

New York

Boston

Washington

Philadelphia

Chicago

Atlanta

St. Louis

Denver

Houston

Seattle

Los Angeles

Programming TechniquesIntroduction to ProgrammingAlgorithms and Control Structures

JavaScript

Page 8: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 8

Brute ForceTraveling Salesman Problem

Algorithm“A detailed sequence of actions to perform to accomplish some task”1

Al-KhawarizmiDied about 840 A.D.HUGE influence on algebra (name comes from his book)

1. Free On-Line Dictionary of Computing

Programming TechniquesIntroduction to ProgrammingAlgorithms and Control Structures

JavaScript

Page 9: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 9

Sequence1. Do this.2. Do something else.3. Do another thing.

Programming TechniquesControl Structures

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

Page 10: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 10

SequenceConditional (if-then, branching)1. Do this.2. Do something else.3. If some condition is true then

3a. Do this bitOtherwise

3b. Do these things.4. Do another thing.

The steps you do if the

condition is true

The steps you do if the

condition is false

Programming TechniquesControl Structures

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

Page 11: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 11

SequenceConditionalLooping (iteration)1. Do this.2. Do something else.3. Do the following steps x number of times

3a. Do this sub-step3b. Do another sub-step.

4. Do another thing.

Programming TechniquesControl Structures

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

Page 12: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 12

SequenceConditionalLoopingTransfer (go to)1. Do this.2. Go to step 4.3. Do something else.4. Do another thing.5. Go to step 1.

Never gets executed

Programming TechniquesControl Structures

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

Page 13: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 13

Unstructured“Go To Statement Considered Harmful”Edsger W. Dijkstra, 1968

StructuredTop-down construction

Object-Oriented“Objects” composed of data and instructionsSelf-contained

Programming TechniquesControl Structures

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

Page 14: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 14

Flowcharts (graphical symbols)

Stoplightgreen?

Stop(light is red)

Continueon

Stoplightgreen?

YES

NO

YES

NO

Programming TechniquesControl Structures

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

Page 15: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 15

Pseudocode (half code, half natural)

While the stoplight is red, stop.If the stoplight is green then continue on.

Advantages:Avoids programming language technicalitiesEasy to understand and change

Programming TechniquesControl Structures

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

Page 16: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 16

Programming languagesC, C++, Java, COBOL, FORTRANStand-alone software

Scripting languagesJavaScript, VBScript“Mini” program that runs inside another programCannot be used outside that application

What is JavaScript?Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

Page 17: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 17

CompilersTranslates entire source file at onceProduces a single executable file

InterpretersTranslates source statements one at a timeImmediately executes each statement

What is JavaScript?Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

Page 18: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 18

Browsers have two functions:HTML

Translate the tagsDisplay the content appropriately

JavaScriptInterpret a lineExecute the line

Both are executed in order

What is JavaScript?Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

Page 19: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 19

JavaScript was introduced by Netscape to validate information entered into text boxes (and other form elements) before it was submitted to a serverJavaScript is not Java, and is not a stripped down version of Java

What is JavaScript?Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

Page 20: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 20

JavaScript code composed of:Instructions/Commands/StatementsCase sensitive!Inserted directly into HTML<script> element

<script>(scripting commands)

</script>

What is JavaScript?The <script> element

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

Page 21: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 21

More than one scripting languageMust specify JavaScript in XHTML code

<script type=“text/javascript”>…

</script>

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

What is JavaScript?The <script> element

Page 22: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 22

Don’t confuse HTML with JavaScript

<script type=“text/javascript”>

</script>

HTMLJavaScript

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

What is JavaScript?The <script> element

Page 23: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 23

Can have multiple <script> elements in one HTML documentCh07-Ex-01.htm

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

What is JavaScript?The <script> element

Page 24: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 24

Can access JavaScript code stored in an external file<script src=“my_code.js”

type=“text/javascript”>…</script>

Only JavaScript code in the .js file, not the <script> tags

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

What is JavaScript?The <script> element

Page 25: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 25

External filesGood for repeated codeHides code from casual users

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

What is JavaScript?The <script> element

Page 26: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 26

Entomology: “a branch of zoology that deals with insects ”, i.e., debugging

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

What is JavaScript?The <script> elementEntomology of Programming

Page 27: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 27

SyntaxConstructed improperly

LogicConstructed properly but does not

achieve thedesired result

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

What is JavaScript?The <script> elementEntomology of Programming

Error types

Page 28: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 28

<script typee=“test/javascript”>

alert(“My first JavaScript.) Alert(“Too exciting for

words.”) alert[“This is big time stuff.”]</script>

Spelling

Closing quote

Capital letter

Square brackets

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

What is JavaScript?The <script> elementEntomology of Programming

Page 29: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 29

Message box:Line number – not always accurateError – not always informative

Skips:Invalid line andAll subsequent lines in same <script> element

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

What is JavaScript?The <script> elementEntomology of Programming

Browser errors

Page 30: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 30

Fixing errorsCorrect line by lineUse trial and errorRewrite code entirelyUse comments to eliminate items

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

What is JavaScript?The <script> elementEntomology of Programming

Page 31: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 31

CommentsHelp other readersHelp yourself at a later timeEliminate sections of code during debugging

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

What is JavaScript?The <script> elementEntomology of Programming

Page 32: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 32

Comments

Single-line<script …>

alert(“Hi!”) // A friendly greeting

// End of this script element</script>

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

What is JavaScript?The <script> elementEntomology of Programming

Page 33: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 33

CommentsMultiple-line

<script …>/* This is a multiple line comment. *//* So is this */

</script>

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

What is JavaScript?The <script> elementEntomology of Programming

Page 34: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 34

CommentsDon’t confuse with HTML comment:

<!-- This is an HTML comment

-->

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

What is JavaScript?The <script> elementEntomology of Programming

Page 35: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 35

Interactivity

HTML has form elements like text boxes, check boxes, radio buttons, etc.JavaScript permits processing of these elements

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

Interactivity

Page 36: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 36

EventsAn action associated with some object

Types:ClickSelectMouseover

Ch07-Ex-03.htm

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

Interactivity

Page 37: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 37

Browser automatically tracks eventsProgrammers job is to write code that responds to eventsEvent Handler

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

Interactivity

Page 38: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 38

User clicks the buttonBrowser automatically detects the clickBrowser generates a click eventEvent handler associated with the button is triggeredJavaScript commands associated with that event are processed

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

Interactivity

Sequence of events

Page 39: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 39

onclick attribute describes the action(s) to take when a click event is detected

<input type=“button” … ¬ onclick=“alert(‘You clicked me’)” />

alert is JavaScript exception to <script> rule

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

Interactivity

Page 40: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 40

InteractivityMultiple JavaScript commands are valid

<input type=“button” … ¬ onclick=“alert(‘You clicked me’)”; onclick=“alert(‘Well done’)” />

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

Interactivity

Page 41: CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) CSCE 102 - General Applications Programming Benito Mendoza 1 2015-11-26 Benito Mendoza 1 By Benito.

CSCE 102 – Chapter 7 (Intro. to Program. & JavaScript ) Benito Mendoza 41

InteractivityMouse events

onmouseover – when cursor is moved over an objectonmouseout – when cursor over an object moves away from the object

Introduction to ProgrammingAlgorithms and Control Structures

JavaScript

Interactivity