0% found this document useful (0 votes)
6 views

Exp 1

Uploaded by

Arya M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Exp 1

Uploaded by

Arya M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Experiment no 1

Aim: Write a JavaScript with HTML for arithmetic expression evaluation and message
printing.

Theory:
1. Arithmetic operators:-

Operator Use in script


+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
++ Increment by 1
-- Decrement by 1

Order of operation: It specify set of rules that indicate order in which an expression is
evaluated by the browser.

1. Calculations must be performed from left to right.


2. Calculations in parenthesis are performed first. When more than one set of
parentheses are included, the expression in inner parenthesis is performed first.
3. Multiplication and division operations are performed next. If both operations are in an
expression, the calculations are performed left to right.
4. Addition and subtraction are next. If both operations are in expression, then
calculations are performed from left to right.
Example: 10*(5+6)= 10*11=110

2. Logical / Relational operators:-


These operators are used to combine two logical expressions into one expression. A
logical expression is an expression that evaluate to either true or false.

Operator Use in script


AND (&&) When both the conditions are true returns true i.e 1
OR (||) When one of the conditions is true returns true i.e. 1
NOT (!) Returns inverse i.e it negates condition

3. Assignment operators:-
These operators assign the value from the right side of the operator to the variable on
the left side of the operator.

Operator Use in script


= Assign value
+= Add value then assign
-= Subtract value then assign
*= Multiply value then assign
/= Divide value then assign
%= Modulus value then assign

4. Comparison operators:-
These operators are used to compare two values(operands).

Operator Use in script


== Equivalency
!= Not equivalent
> Greater than
>= Greater than equal to
< Less than
<= Less than equal to

5. Conditional (ternary) operator:-


It consists of three parts: first part is expression(condition) to be evaluated, second
part is expression to be executed if condition is true and third part is expression to be
executed if condition is false.
Syntax: Condition ? True expression : False expression OR expression ? value1 : value2
Example:
var a=10
var b=20
var result
result=(a>b)? a : b
document.write(result)
Output: 20

Programs:
1. Arithmetic operators:
Program: Output: -
<html> Arithmetic operators
<head></head>
<body> Addition= 10
<h1>Arithmetic operators</h1> Subtraction =2
<script type="text/javascript"> Multiplication =24
var a,b,c; Division = 1.5
a=6; Modulus = 2
b=4; Pre-increment = 7
c=a+b; Post-increment = 6
document.write("Addition= "+ c + "<br>");
c=a-b;
document.write("Subtraction =" + c +"<br>");
c=a*b;
document.write("Multiplication =" + c +
"<br>");
c=a/b;
document.write("Division = " + c + "<br>");
c=a%b;
document.write("Modulus = " + c + "<br>");
a=6;
c=++a;
document.write("Pre-increment = " + c +
"<br>");
a=6;
c=a++;
document.write("Post-increment = " + c +
"<br>");
</script>
</body>
</html>

2. Logical operators:
Program: Output: -
<html> Logical operators
<head></head>
<body> AND(&&) operator
<h1>Logical operators</h1> a and b both are greater than 0
<script type="text/javascript"> OR(||) operator
var a,b; a is a vowel
a=6; NOT(!) operator
b=4; a and b are not equal
document.write("AND(&&) operator <br>");
if(a>0 && b>0)
{
document.write(" a and b both are greater than
0");
}
document.write("<br> OR(||) operator <br>");
a='i';
if(a=='a'|| a=='e' || a=='i' || a=='u' || a=='o')
{
document.write(" a is a vowel");
}
else
{
document.write(" a is not a vowel");
}
document.write("<br> NOT(!) operator
<br>");
if(!(a==b))
{
document.write("a and b are not equal");
}
else
{
document.write(" a and b are equal");
}
</script>
</body>
</html>

3. Assignment operators:
Program: Output:
<html> Assignment operators
<head></head>
<body> Equalto (=) operator :a=4
<h1>Assignment operators</h1> Add then Assign (+=) operator :b=10
<script type="text/javascript"> Subtract then Assign (-=) operator :b=2
var a,b; Multiplication then Assign (*=)
a=4; operator :b=24
document.write(" Equalto (=) operator :" + Division then Assign (/=) operator :b=1.5
"a=" + a); Modulus then Assign (%=) operator :b=2
document.write(" <br>Add then Assign (+=)
operator :");
b=6;
b+=a;
document.write("b="+b);
document.write(" <br>Subtract then Assign (-
=) operator :");
b=6;
b-=a;
document.write("b="+b);
document.write(" <br>Multiplication then
Assign (*=) operator :");
b=6;
b*=a;
document.write("b="+b);
document.write(" <br>Division then Assign
(/=) operator :");
b=6;
b/=a;
document.write("b="+b);
document.write(" <br>Modulus then Assign
(%=) operator :");
b=6;
b%=a;
document.write("b="+b);
</script>
</body>
</html>
4. Comparison operators:
Program: Output:
<html> Comparison operators
<head></head>
<body> Equal to equal to (==) operator :a and b are
<h1>Comparison operators</h1> not equal
<script type="text/javascript"> Not Equal to (!=) operator :a and b are not
var a,b; equal
a=6; Greater than (>) operator :a is greater than b
b=4; Greater than equal to (>=) operator :a is
document.write(" Equal to equal to (==) greater than or equal to b
operator :"); Less than (<) operator :a is not less than b
if(a==b) Less than equal to (<=) operator :a is not
document.write("a and b are equal<br>"); less than or equal to b
else
document.write("a and b are not equal<br>");

document.write(" Not Equal to (!=)


operator :");
if(a!=b)
document.write("a and b are not equal<br>");
else
document.write("a and b are equal<br>");

document.write(" Greater than (>)


operator :");
if(a>b)
document.write("a is greater than b<br>");
else
document.write("a is not greater than b<br>");

document.write(" Greater than equal to (>=)


operator :");
if(a>=b)
document.write("a is greater than or equal to
b<br>");
else
document.write("a is not greater than or equal
to b<br>");

document.write(" Less than (<) operator :");


if(a<b)
document.write("a is less than b<br>");
else
document.write("a is not less than b<br>");

document.write(" Less than equal to (<=)


operator :");
if(a<=b)
document.write("a is less than or equal to
b<br>");
else
document.write("a is not less than or equal to
b<br>");
</script>
</body>
</html>

5. Conditional operator:
Program: Output:
<html> Conditional operator
<head></head>
<body> Answer for conditional operator
<h1>Conditional operator</h1> 14
<script type="text/javascript">
var a,b,result;
a=6;
b=14;
result=(a>b)?a:b;
document.write("Answer for conditional
operator <br>" +result);
</script>
</body>
</html>

Conclusion:
With all the concepts based on operators and printing message ,successfully executed all
programs with correct output.

You might also like