14/11/11. these words have special meanings in themselves these should not be used as identifiers....

42
Lecture 7 14/11/11

Post on 19-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

Lecture 714/11/11

Page 2: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

Reserved Words

These words have special meanings in themselves

These should NOT be used as Identifiers.

For example: Break, do, function, case, else, if, for, new…

2

Page 3: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

JavaScript Objects The browser contains a complete set of

objects that allow script programmers to access and manipulate every element of a HTML document

These objects reside in the computers memory and contains information used by the script

Each object has attributes and behaviours associated with it

3

Page 4: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

Variables A variable is a name associated with a

data value

A variable contains or stores the value

Variables are names that have values assigned to them

They provide a way to manipulate values by name

4

Page 5: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

Variables The value associated with a name need not

be constant

Since the value associated with a name may vary these names are called variablesT = 3; T = 3T = T + 4; T = 8

5

Page 6: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

Assign a Value to a Variable

You assign a value to a variable like this with the var statement:

var strname = “Mary”;

Or like this without the var statement:strname = “Mary”;

6

Page 7: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

Variables It is good programming practice to declare

variables before using themvar t;var sum;Or:var t; var sum;orvar t = 2;

7

Page 8: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

Untyped Variables

◦ Unlike other languages JavaScript is untyped Variables can hold data of any variable type

T = 10T = “ten”

In Java you would have to use:

int T = 10;String T = “ten”;

DataTypes can change accordingly.

8

Page 9: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

Example

<html><head><script type="text/javascript">function message(){alert("This alert box was called with the onload event");}</script></head>

<body onload="message()">

</body></html>

9

Page 10: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

Javascript

The JavaScript is contained within a <script> container tag.◦ First line reads:

<script language “JavaScript”> followed by browser hiding (<!-- -->) followed by your code followed by closing hiding comment followed by closing the script tag

◦ Generally variables are all declared in the head of the document.

◦ These are interpreted first

10

Page 11: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

Arithmetic Operators

Multiplication *Division /

Addition +Subtraction -

And - Modulo

◦ The % operator returns the remainder when the first operand is divided by the second

E.g. 5 % 2 the result is 1

11

Page 12: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

Comparison Operators These are operators that compare values of

various types and return a Boolean value (true or false)

Use of these operators involves flow controls – i.e. structures which control the flow of the program.

12

Page 13: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

Comparison Operators

<Less than

<=Less than or equal to

>Greater than

>=Greater than or equal to

!=Not equal to

==Equal to13

Page 14: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

Comparison Operators Equality (= =)

◦ The = = operator returns true if two operands are equal and returns false if they are not

◦ The operands may be of any type and the definition of “equal” will depend on the type

◦ Two variables are only equal if the contain the same value

◦ Two Strings are only equal if they each contain exactly the same characters (bearing in mind that ‘A’ and ‘a’ are separate characters

14

Page 15: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

Comparison Operators◦ Usually, if two values have different types, then

they are not equal◦ JavaScript sometimes automatically converts data

types when needed “1” = = 1 returns true

◦ Equality operator = = is very different from the assignment operator =.

15

Page 16: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

Comparison Operators

Inequality (!=)◦ The != operator tests for the exact of the = =

operator◦ If two variables are equal to each other, then

comparing them with ! = will return false 4 = = 4 returns true 4 != 4 returns false

◦ Remember != stands for not equal to Less than (<)

◦ The < operator evaluates to true if the first operand is less than its second operand, otherwise it evaluates to false 16

Page 17: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

Logical Operators

The logical operands expect their operands to be Boolean values

Usually used with the comparison operators to express complex comparisons that involve more than one variable.

&&Logical AND ||Logical OR !Logical NOT

17

Page 18: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

Logical Operators

Logical AND (&&)◦ The && operator evaluates to true if and only if

both its first operand and its second operand are true. If either operand equates to false, then the result will be false.

Logical OR (||)◦ The || operator evaluates to true if its first

operand or its second operand or both are true Logical NOT

◦ The ! operator is placed before a single operand◦ Its purpose is to invert the Boolean value of its

operand18

Page 19: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

Examples: x=15 y=20 w=10 z=5

Evaluate each of the following:

(x <17 && y>25)(y<=30 || w >12)z==y

19

Page 20: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

String Operator

A string is most often a text, for example "Hello World!". To stick two or more string variables together, use the + operator.

var txt1="What a very“;var txt2="nice day!" ;var txt3=txt1+txt2 ;

The variable txt3 now contains - "What a verynice day!"

20

Page 21: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

String Operator To add a space between two string

variables, insert a space into the expression, OR in one of the strings.

txt1="What a very" ;txt2="nice day!" ;txt3=txt1+" "+txt2;Ortxt1="What a very " ;txt2="nice day!" ;txt3=txt1+txt2;

The variable txt3 now contains "What a very nice day!"

21

Page 22: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

Selection & Iteration: Flow Controls

JavaScript consists of a collection of statements

Statements usually end with a semi-colon You can define variables and manipulate

them The general route a program/script takes

can be altered this is known as the flow of the program

22

Page 23: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

Primary Flow Controls

23

Performs code while a condition is true

While

An Iterative LoopFor

If, but with an alternative clause.

If/else

Makes a decision based on a condition

if

Page 24: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

If

This is the simplest flow control. It alters the flow of the program according to a condition

Allows JavaScript to make decisions Syntax

if (some condition is true) {statements }

24

Page 25: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

Example1<html><head><title>Check student Grade</title>

<script language="JavaScript">

var studentGrade;studentGrade=30;

if (studentGrade >=60)document.writeln("Passed");

elsedocument.writeln("Failed");

</script></head></html>

25

Page 26: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

If/ ElseIf/Else The second form of the if statement

introduces an else clause that is executed when the expression is false

The expression is evaluated and if it equates to true then statement1 is executed, if not statement2 is executed

Else if can be used to differentiate between several conditions

26

Page 27: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

Example <script language="JavaScript">

var studentGrade;studentGrade=78;

if (studentGrade >=80)document.writeln("A");

else if(studentGrade <=80)document.writeln("B");

else if(studentGrade <=70)document.writeln("C");

else if(studentGrade <=60)document.writeln("D");

else if(studentGrade <=50)document.writeln("Pass");

else document.writeln("Failed");

</script>27

Page 28: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

This structure performs one of many different actions, depending on the value of an expression.

28

Switch Statements

Page 29: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

An alternative to If/ ElseIf Statements.

Switch (n){

case 1: // start here if n = 1 (n = = 1)// execute code block # 1break; // stop here

case 2: // start here if n = 2// execute code block # 1break; // stop here

case 3: // start here if n = 3// execute code block # 1break; // stop here

default: // if none of the previous conditions// equates to true

break; // stop here}

29

Page 30: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

<script language="JavaScript">var studentGrade;studentGrade=window.prompt("Input Student Grade:\n"+ "90 (A),

80 (B), 70 (C), 60 (D), 50 (Pass)");

switch(studentGrade) {case "90":

document.writeln("A");break;

case "80":document.writeln("B");break;

case "70":document.writeln("C");break;

case "60":document.writeln("D");break;

case "50":document.writeln("Pass");break;

default: document.writeln("fail");

}</script>

30

Page 31: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

Increment Operators

Operator Called Sample Expression

Explanation

++ preincrement ++a Increment a by 1, then use the new value of a in the expression in which a resides

++ postincrement a++ Use the current value of a in the expression in which a resides then increment a by 1

31

Page 32: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

Decrement Operators

Operator Called Sample Expression

Explanation

-- predecrement --b Decrement b by 1, then use the new value of b in the expression where b resides

-- postdecrement b-- Use the current value of b in the expression in which b resides, then decrement b by 1.

32

Page 33: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

Example Increment Operators<html><title>Increment Operators</title>

<head>

<script langauge="JavaScript">

var c; // declare c as a variablec=5; // let c equal to 5

document.writeln("<h3>Postincrementing</h3>");document.writeln(c); //prints the value of cdocument.writeln("<br>" + c++); /prints the value of c then incrementsdocument.writeln("<br>" + c); //prints the incremented value of c

33

Page 34: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

Continued….

var c; // declare c as a variablec=10; // let c equal to 10

document.writeln("<h3>Preincrementing</h3>");document.writeln(c); //prints the value of cdocument.writeln("<br>" + ++c); // increments c by 1document.writeln("<br>" + c); //prints the incremented value of c

</script>

</head>

</html>34

Page 35: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

Output

35

Page 36: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

Executes a statement or statements for a number of times – iteration.

Syntaxfor(initialize; test; increment){// statements to be executed}

Initial Value is the starting number of the loop.

If the condition(test) is true the code is executed.

The initial value is changed by the step size.

36

For Loops

Page 37: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

The expression is evaluated, if it is false, JavaScript moves to the next statement in the program

If it is true then the statement is executed and expression is evaluated again

Again if the expression is false the JavaScript moves on the next statement in script, otherwise it executes the statement that forms the body of the loop

37

For Loops

Page 38: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

<html>

<head><title> For</title>

</head><body><script type="text/javascript">for (i = 0; i <= 5; i++){document.write("The number is " + i);document.write("<br />");}</script>

</body></html>

38

For Loops

Page 39: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

While a condition is true execute one or more statements.

“While Loops” are especially useful when you do not know how many times you have to loop

but you know you should stop when you meet the condition, i.e. an indeterminate loop

39

While Loops:

Page 40: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

The while Loop is the basic statement that allows JavaScript to perform repetitive actions. It has the following syntax.

while (expression) {// Statement to be executed}… more code…

40

While Loop

Page 41: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

This cycle continues until the expression evaluates to false

It is important that a Loop counter that is used within the body of the Loop is declared outside it.

41

While Loops

Page 42: 14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,

<html><body>

<script type="text/javascript">i=0;while (i<=5){document.write("The number is " + i);document.write("<br />");i++;}</script></body></html>

42