ch5 - Java Part1 Operators
ch5 - Java Part1 Operators
Fatih ABUT
Introduction to JavaScript
Variables and Data Types
Operator Basics
2
JavaScript is used in millions of Web pages to
improve the design, validate forms, detect
browsers, create cookies, and much more.
3
JavaScript was designed to add interactivity to HTML
pages
5
Nat 4/5
◦ Interpreter
Translates and executes one line at a time
◦ Compiler
Translates and executes the entire program at
once
This program can then be ran repeatedly
6
Nat 4/5
Think complete!
8
Nat 4/5
Pctoutput.visible = true
Object Code
1001 1000 1100 1110 0011
Compiler
1010 1010 1010 0111 0011
Syntax Error
10
<html>
<head
<script type="text/javascript">
document.write("Hello World!")
</script>
</head>
<body>
</body>
</html>
11
A variable is a name associated with a piece of data
13
Primitive Data Types
◦ Numbers
◦ Strings
◦ Boolean (True, False)
14
Numbers - A number can be either an
integer or a decimal (real)
◦ Integers are used to count whole (i.e. complete)
things
1 person, 4 balls, 12 moons
16
JavaScript is untyped; It does not have
explicit data types
17
Although JavaScript does not have explicit
data types, it does have implicit data types
18
var x = 4; Ans = x + y;
Ans => 15
var y = 11;
Ans = z + x;
var z = “cat”; Ans => cat4
19
var x = 4; Ans = x + y + z;
Ans => 15cat
var y = 11;
Ans = q + x + y;
var z = “cat”; Ans => 17411
var q = “17”;
20
// single-line comment
/* multi-line comment */
JS
21
JavaScript is case sensitive
22
A statement is a Examples:
section of JavaScript Last_name = “Dunn”;
that can be x = 10 ;
evaluated by a Web y = x*x ;
browser
A script is simply a
collection of
statements
23
Assignment, Arithmetic,
Relations, Logical, Bitwise and
Expressions
24
+ Addition ! Logical NOT
- Subtraction && Logical AND
* Multiplication || Logical OR
/ Division ~ Bitwise NOT
% Modulus & Bitwise AND
++ Increment | Bitwise OR
-- Decrement << Bitwise Left Shifting
== Equality >> Bitwise Right Shifting
!= Inequality ?: Conditional Selection
26
The assignment operator must be used with
care and attention to detail
Right-to-Left evaluation: A = B = C
27
Arithmetic operators are used to express the
logic of numerical operations
28
Unary versus Binary
◦ It is meaningful to say –X (negative X) so JavaScript
permits use of the minus symbol (hyphen) as a
unary operator. It also permits use of + as unary.
Ex. A = -3 ;
29
There are two division operators in JavaScript
◦ / (quotient) and % (modulus)
◦ Both are binary operators
◦ Modulus division is used almost exclusively for
division of integers, since it evaluates to the
remainder
Division : / %
30
A common programming statement involves
adding (or subtracting) 1 to (from) a variable
used for counting
◦ N = N+1; N = N–1;
31
The JavaScript language supports two
operators that automatically generate
increment or decrement statements on
integer variables
◦ Auto-Increment ++
◦ Auto-Decrement --
32
There is a very important difference between
using these operators before versus after a
variable symbol
◦ AFTER (POST) :
If an expression contains N++, the expression is evaluated
using the value stored at the location N. After the
expression is evaluated, the value at N is incremented by 1.
◦ BEFORE (PRE) :
If an expression contains ++N, the value at N is incremented
by 1 and stored at N, before any other parts of the
expression are evaluated. The expression is then evaluated
using the new value at N.
33
Assume the declarations with initial values
specified
◦ var A, B, N = 4, M = 3 ;
◦ A = N++ ;
◦ B = ++M + N-- ; /* watch out ! */
◦ A = --A ;
34
Operator augmentation involves combining two
operator symbols to form a new symbol with
extended meaning
◦ += and -=
◦ *=
◦ /= and %=
◦ In some cases they may lead to hardware optimization of
executable code.
35
Although these operations have a certain kind
of elegance, they may create ambiguity.
◦ However, programmers should ensure that
programs have clarity.
◦ Examples:
◦ Longhand Shorthand
X=X+Y; X += Y ;
X=X*Y; X *= Y ;
X=X%Y; X %= Y ;
36
Relational operators are used to express the
concept of comparison of two values
◦ Based on the Boolean notions of True and False
37
Formally, these operators are defined as
38
Each matching colour pair is complementary.
39
Each relational operator is a binary operator,
with an operand on the left and another on
the right of the operator symbol(s)
40
Boolean Set Theory defines several operations
that act on values 0 and 1
◦ These values apply to relational expressions and also
integer variables (limited to these two values)
Complement (Not) : !
◦ Unary !(X<Y)
41
The logical operators considered at this time
are a subset of the logic operators. There
PROPOSITION
are many other operators
I will available.
go to the movies if:
42
& | ^ >> >>> <<
(A > B ) ? 10 : 20
44
String operator:
+
45
45
Complex expressions can be constructed
using the various operators seen so far
◦ Such expressions must be constructed with care,
taking into account the issue of data type
compatibility
◦ It is also important to avoid ambiguity in how the
expression is to be interpreted (both by the
compiler and by the programmer)
Parentheses ( ) are often used to encapsulate
sub-expression terms
◦ Sub-expressions within parentheses are compiled
before other terms.
46
When an expression is constructed using
parenthesized sub-expressions, these sub-
expressions themselves may be further
broken down into parenthesized sub-sub-
expressions
47
Example:
(1+5)*3–(4–2)%3
48
Example:
(1+5)*3–(4–2)%3
(6)*3 - (2)%3
18 - 2
16
49
Example:
(1+5)* (3–(4–2)/(5–1))%3
50
Example:
(1+5)* ( 3–(4–2)/(5–1) ) %3
6 * ( 3 - 0.5 ) % 3
6 * 2.5 % 3
15 % 3 = 0
51