Docsity

JavaScript Assignment Operators and Conditional Statements, Assignments of Computer Science

Toggle sidebar
bg1
1
U
UNIVERSITY
NIVERSITY
OF
OF M
MASSACHUSETTS
ASSACHUSETTS A
AMHERST
MHERST
•
•
C
CMP
MPS
SCI
CI 120 SPRING
120 SPRING 2009
2009
JavaScript
Read JavaScript by Example By Ellie Quigley
Chapters 5-7
U
UNIVERSITY
NIVERSITY
OF
OF M
MASSACHUSETTS
ASSACHUSETTS A
AMHERST
MHERST
•
•
C
CMP
MPS
SCI
CI 120 SPRING
120 SPRING 2009
2009
JavaScript Assignment Operators
Assignment operators are used to assign values to
JavaScript variables.
Given that x=10 and y=5, the table below explains the
assignment operators:
xx=x%yx%=y%=
x=2x=x/yx/=y/=
x=50x=x*yx*=y*=
x=5x=x-yx-=y-=
x=15x=x+yx+=y+=
x=5x=y=
ResultSame AsExampleOperator
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download JavaScript Assignment Operators and Conditional Statements and more Assignments Computer Science in PDF only on Docsity!

U

U

NIVERSITY

NIVERSITY

OF

OF

M

M

ASSACHUSETTS

ASSACHUSETTS

A

A

MHERST

MHERST

C

C

MP

MP

S

S

CI

CI

120 SPRING

120 SPRING

JavaScript

Read JavaScript by Example By Ellie Quigley

Chapters 5-

U

U

NIVERSITY

NIVERSITY

OF

OF

M

M

ASSACHUSETTS

ASSACHUSETTS

A

A

MHERST

MHERST

C

C

MP

MP

S

S

CI

CI

120 SPRING

120 SPRING

JavaScript Assignment Operators

Assignment operators are used to assign values to

JavaScript variables.

Given that x=10 and y=5, the table below explains the

assignment operators:

%= x%=y x=x%y x

/= x/=y x=x/y x=

= x=y x=x*y x=

-= x-=y x=x-y x=

+= x+=y x=x+y x=

= x=y x=

Operator Example Same As Result

U

U

NIVERSITY

NIVERSITY

OF

OF

M

M

ASSACHUSETTS

ASSACHUSETTS

A

A

MHERST

MHERST

C

C

MP

MP

S

S

CI

CI

120 SPRING

120 SPRING

Operators

String operator: +

The + operator can also be used to add

string variables or text values together.

To add two or more string variables

together, use the + operator.

txt1="What a very";

txt2="nice day";

txt3=txt1+txt2;

After the execution of the statements above,

the variable txt3 contains "What a verynice

day".

U

U

NIVERSITY

NIVERSITY

OF

OF

M

M

ASSACHUSETTS

ASSACHUSETTS

A

A

MHERST

MHERST

C

C

MP

MP

S

S

CI

CI

120 SPRING

120 SPRING

Operators

To add a space between the two strings,

insert a space into one of the strings:

txt1="What a very ";

txt2="nice day";

txt3=txt1+txt2;

txt1="What a very";

txt2="nice day";

txt3=txt1+" "+txt2”

or

U

U

NIVERSITY

NIVERSITY

OF

OF

M

M

ASSACHUSETTS

ASSACHUSETTS

A

A

MHERST

MHERST

C

C

MP

MP

S

S

CI

CI

120 SPRING

120 SPRING

Statements

This example demonstrates a link, when you click on

the link it will take you to W3Schools.com OR to

RefsnesData.no. There is a 50% chance for each of

them.

Random link

U

U

NIVERSITY

NIVERSITY

OF

OF

M

M

ASSACHUSETTS

ASSACHUSETTS

A

A

MHERST

MHERST

C

C

MP

MP

S

S

CI

CI

120 SPRING

120 SPRING

Additional Operators

The conditional operator:

condition? value_if_true : value_if_false

Special equality tests:

== and != try to convert their operands to the same type

before performing the test

=== and !== consider their operands unequal if they are

of different types

Additional operators (to be discussed with “objects”):

new typeof void delete

U

U

NIVERSITY

NIVERSITY

OF

OF

M

M

ASSACHUSETTS

ASSACHUSETTS

A

A

MHERST

MHERST

C

C

MP

MP

S

S

CI

CI

120 SPRING

120 SPRING

Comparison Operators

 Comparison operators are used in logical statements to determine equality

or difference between variables or values.

 Given that x=5, the table below explains the comparison operator

 Can be used in conditional statements to compare values and take action

depending on the result:

if (age<18) document.write("Too young")

<= is less than or equal to x<=8 is true

>= is greater than or equal to x>=8 is false

< is less than x<8 is true

> is greater than x>8 is false

!= is not equal x!=8 is true

x==="5" is false

=== is exactly equal to (value and type) x===5 is true

== is equal to x==8 is false

Operator Description Example

U

U

NIVERSITY

NIVERSITY

OF

OF

M

M

ASSACHUSETTS

ASSACHUSETTS

A

A

MHERST

MHERST

C

C

MP

MP

S

S

CI

CI

120 SPRING

120 SPRING

Example

Comparing Numbers

Example

U

U

NIVERSITY

NIVERSITY

OF

OF

M

M

ASSACHUSETTS

ASSACHUSETTS

A

A

MHERST

MHERST

C

C

MP

MP

S

S

CI

CI

120 SPRING

120 SPRING

Logical Expressions

Example

Logical OR Operator

U

U

NIVERSITY

NIVERSITY

OF

OF

M

M

ASSACHUSETTS

ASSACHUSETTS

A

A

MHERST

MHERST

C

C

MP

MP

S

S

CI

CI

120 SPRING

120 SPRING

Type Conversion

Remember JavaScript is loosely typed

Three basic methods to convert the primitive data types

String( )

Number( )

Boolean( )

This example

U

U

NIVERSITY

NIVERSITY

OF

OF

M

M

ASSACHUSETTS

ASSACHUSETTS

A

A

MHERST

MHERST

C

C

MP

MP

S

S

CI

CI

120 SPRING

120 SPRING

The parseInt( ) method

parseInt(string, radix)

string required, radix optional (base 2 - 36)

If the string begins with "0x", the radix is 16

(hexadecimal)

If the string begins with "0", the radix is 8 (octal). This

feature is deprecated

If the string begins with any other value, the radix is 10

(decimal)

Parses from left to right and returns integers until it

reaches a non-integer, then stops

if the string doesn’t begin with an integer, returns NaN

U

U

NIVERSITY

NIVERSITY

OF

OF

M

M

ASSACHUSETTS

ASSACHUSETTS

A

A

MHERST

MHERST

C

C

MP

MP

S

S

CI

CI

120 SPRING

120 SPRING

The parseInt( ) method examples

“hello”

“Route 66”

“6 dogs”

“0xa” hexadecimal!

“ 011 ” octal!

“b”, 16

Example 5.

U

U

NIVERSITY

NIVERSITY

OF

OF

M

M

ASSACHUSETTS

ASSACHUSETTS

A

A

MHERST

MHERST

C

C

MP

MP

S

S

CI

CI

120 SPRING

120 SPRING

JavaScript Functions

To keep the browser from executing a script when the

page loads, you can put your script into a function.

A function contains code that will be executed by an

event or by a call to the function.

You may call a function from anywhere within a page (or

even from other pages if the function is embedded in an

external .js file).

Functions can be defined both in the and in the

section of a document. However, to assure that

a function is read/loaded by the browser before it is

called, it could be wise to put functions in the

section

U

U

NIVERSITY

NIVERSITY

OF

OF

M

M

ASSACHUSETTS

ASSACHUSETTS

A

A

MHERST

MHERST

C

C

MP

MP

S

S

CI

CI

120 SPRING

120 SPRING

How to Define a Function

function functionname(var1,var2,...,varX)

some code

The parameters var1, var2, etc. are variables or values

passed into the function

The { and the } defines the start and end of the function.

 Notes:

A function with no parameters must include the parentheses ()

after the function name.

Do not forget about the importance of capitals in JavaScript!

The word function must be written in lowercase letters,

otherwise a JavaScript error occurs! Also note that you must

call a function with the exact same capitals as in the function

name.

 W3C example

U

U

NIVERSITY

NIVERSITY

OF

OF

M

M

ASSACHUSETTS

ASSACHUSETTS

A

A

MHERST

MHERST

C

C

MP

MP

S

S

CI

CI

120 SPRING

120 SPRING

The return Statement

The return statement is used to specify the value that is

returned from the function.

So, functions that are going to return a value must use

the return statement.

This W3C example returns the product of two numbers

(a and b)

Another W3C example -- passing a parameter

Another W3c example

Scope reminder

If you declare a variable within a function, the

variable can only be accessed within that

function. When you exit the function, the variable

is destroyed.

U

U

NIVERSITY

NIVERSITY

OF

OF

M

M

ASSACHUSETTS

ASSACHUSETTS

A

A

MHERST

MHERST

C

C

MP

MP

S

S

CI

CI

120 SPRING

120 SPRING

Back to Conditional Statements

We covered the following conditional statements:

if statement - use this statement if you want to execute

some code only if a specified condition is true

if...else statement - use this statement if you want to

execute some code if the condition is true and another

code if the condition is false

if...else if....else statement - use this statement if you

want to select one of many blocks of code to be

executed

switch statement - use this statement if you want to

select one of many blocks of code to be executed

Next we will cover loops

U

U

NIVERSITY

NIVERSITY

OF

OF

M

M

ASSACHUSETTS

ASSACHUSETTS

A

A

MHERST

MHERST

C

C

MP

MP

S

S

CI

CI

120 SPRING

120 SPRING

The while loop

Example

This W3C example defines a loop that starts with

i=0. The loop will continue to run as long as i is

less than, or equal to 5. i will increase by 1 each

time the loop runs:

U

U

NIVERSITY

NIVERSITY

OF

OF

M

M

ASSACHUSETTS

ASSACHUSETTS

A

A

MHERST

MHERST

C

C

MP

MP

S

S

CI

CI

120 SPRING

120 SPRING

The do/while loop

Executes the body of the loop at least once before

testing its condition … example

U

U

NIVERSITY

NIVERSITY

OF

OF

M

M

ASSACHUSETTS

ASSACHUSETTS

A

A

MHERST

MHERST

C

C

MP

MP

S

S

CI

CI

120 SPRING

120 SPRING

Quigley Examples

for loop

example

while loop

example

U

U

NIVERSITY

NIVERSITY

OF

OF

M

M

ASSACHUSETTS

ASSACHUSETTS

A

A

MHERST

MHERST

C

C

MP

MP

S

S

CI

CI

120 SPRING

120 SPRING

Nested loops

Nested loops