Explain Java Script Statement. Describe each of the Loop Constructs that Java Script Provides
https://www.computersprofessor.com/2016/06/explain-java-script-statement-describe.html
Programs are composed of two things.
® data
® code which manipulates the data
Program instructions are grouped
into units called statements.
If else: Whenever we want to
test the truth of a condition before executing anymore of our program use
this construct. This statement means that if some condition is true then do
one thing. If the condition is false then does another thing.
Eg:- if (x > 0)
document.write (“ x is positive”);
else
document.write (“ x is negative”);
write a program to find biggest of three numbers
<html>
<head>
<title> biggest of 3 no’s </title>
</head>
<body>
<script language = “ java script”>
var a, b, c;
a = parseInt (window.prompt (“enter a value”, “ “));
b = parseInt (window.prompt (“enter b value”, “ “));
c = parseInt (window.prompt (“enter c value”, “ “));
if ((a >b) & & (a > c))
document. write (“ a is big” + a);
else if (b > c )
document. write (“ b is big” + b);
else
document. write (“c is big” + c);
</script>
</body>
</html>
|
for statement:-
Syntax: for (initialization; condition;
increment/decrement)
{
Statements;
}
Eg:- for (counter = 0; count < =
n; counter + + )
{
- - - -
}
Many
operations need to be repeated a no.of times they are go inside a ‘for’ loop
by convention. ‘For’ loop start counting at ‘o’ and terminate when the
desired no.of iterations has been reached. The variable which holds the
counter corn the given any name you like often counters are called ‘I’ (or)
‘j’.
Write a program to calculate the
factorial of the given number
<html>
<head>
<title> factorial of a number </title>
<script language = “javascript”>
var n1 fact = 1, I;
n = parseInt (window. prompt(“enter n value”,” “));
if (n = 0)
document. wirte (“factorial of 0 is 1”);
else
{
for (i = 01; I < = n; i + + )
{
fact = fact * i;
}
}
document. write (“ factorial of “ + n + “ is “ + fact):
</script>
</head>
<body> </body>
</html>
While:
Some
we don’t known how many iterations are going to be needed, the loop may
continue forever if an external event does not act upon it. In such case we
can use ‘while’ loop.
Syntax:
Initialization;
while
(condition)
{
Statements;
-
- - - - - -
Increment/decrement;
}
write a program to generate Fibonacci series.
<html>
<head>
<title> fibonacci series </title>
<script language = “ java script”>
var n, a = 0, b = 1, c;
document. write (a +”<br>” + b);
n = parseInt (window.prompt (“enter n value”, “ “));
c = a + b;
while (c < = n)
{
document. write (“<br>” + c);
a = b;
b = c;
c = a + b;
}
</script>
</head>
<body> </body>
</html>
Evaluate function eval():-
This is a very useful java scripts built in function. String versions of
mathematical expressions can b e passed into the function, where they are
evaluated and the result returned as an integer.
Eg:- eval (“32 * 75624.82”);
Switch:-
Choosing
between a no.of alternatives can be done using if else statement. Where you
need to make a choice between more than two items we can use switch statement. Switch is much
easier to write and maintain.
Syntax:- switch (expression)
{
caselabel 1 :
Statements:
Break;
caselabel 2:
Statements;
Break;
:
caselabel n :
Statements:
Break;
Default:
statements;
}
A switch selects between a number
of choices depending upon the value of
expression the choices are identified by ‘case’ statements each case has a
label which equals one of the potential values of the expression.
If
none of the case matches the expression, the
optional default statement may be used instead. Each case includes one
(or) more statements and is terminated by a break.
Write a program to print day using
switch.
<html>
<head>
<title> days </title>
<script language = “java script”>
var n;
n = parseInt (window.prompt (“enter
n value “, “ “));
switch (n)
{
caselabel 1 :
document
.write (“Sunday”);
break;
caselabel 2 :
document.
write (“Monday”);
break;
caselabel 3:
document.
write (“Tuesday”);
break;
caselabel 4:
document.
write (“Wednesday”);
break;
caselabel 5:
document.
write (“Thursday”);
break;
caselabel 6:
document.
write (“Friday”);
break;
caselabel 7:
document
.write (“Saturday”);
break;
default: document .write(“code mismatch”);
}
</script>
</head>
<body>
</body>
</html>
Break:-
if you want to be
able to leep out of the middle of a loop (or) create a construct based around
a ‘while’ loop with if statements embedded in it (or) use the break
statements. If you break out of the ,middle of them you may put variables
into unknown state. Compare this two loops decides which you refer.
Without using break:
var answer = 0;
var correct = 49;
var done = false;
var counter = 0;
while (done = =
false)&&(counter < 3 ) )
{
answer
= prompt (“what is 7 times of 7? “,”0”);
if (answer = = correct)
{
done = true;
}
else
{
counter
+ + ;
}
}
With
using break:
var correct = 49;
for (counter = 0; counter < 3 ;
counter + + )
{
answer
= prompt (“what is 7 times of 7?”,”0”);
{
break;
}
}
|