Exp 2
Exp 2
2
Aim: Develop JavaScript to use decision making and looping statements.
Theory:
Decision making statements:
if statement: It tells the browser to execute one or more statements if a condition expression evaluates to
true.
Syntax: if(conditional expression)
{
Statement/code block;
}
Conditional expression evaluates either to true or false value. Statement/code block contains one or more
statements to be executed if conditional expression evaluates to true.
if … else statement: It tells the browser to execute one or more statements depending on evaluation of
conditional expression.
Syntax: if(conditional expression)
{
True statement/code block;
}
else
{
False statement/code block;
}
Conditional expression either evaluates to true or false. If conditional expression evaluates to true then true
statements/code block is executed and false block is ignored. If conditional expression evaluates to false then true block
is ignored and false block is executed.
if … elseif statement: It tells the browser to evaluate first conditional expression and if it is true then,
execute true statement/code block and if it is false then evaluate another conditional expression and so on.
Syntax: if (conditional expression)
{
True statement/code block;
}
else if(conditional expression)
{
True statement/code block;
}
else
{
False statement/code block;
}
Nested if statement: When one if statement is written inside another if statement then it is called as nested
if statement.
Syntax: if(conditional expression)
{
if(conditional expression)
{
Statements;
}
}
switch … case statement : It is used to compare a single switch value with multiple values specified as
case values. While executing switch … case statement, switch value is compared with a series of case values.
If the switch value matches a case value, then the browser executes all statements that are placed inside case
value block.
Syntax:
switch(value)
{
case ‘value1’:
statements;
break;
case ‘value2’:
statements;
break;
.
.
.
default:
statements;
}
- switch, case are keywords.
- switch value is the value to be compared with each case value.
- case value1,2...,n are the possible values for switch value.
- statements inside case executes when switch value and case value match with each other.
- break statement is used to tell browser to skip all remaining cases and exit from switch statement.
default statements execute when all case values don’t match will switch value.
Loop statement:
For loop: It tells the browser to execute statements within the loop until a condition statement returns false
value. It contains three parts: initialization, condition checking and increment or decrement counter variable.
While loop: It tells the browser to execute one or more statements until the condition in while loop becomes
false. While loop doesn’t always specify number of times statements to be executed in a loop i.e. when in an
application loop execution depends on certain user action or application’s action then while loop can be used.
It is a entry control loop i.e. before executing loop statements it evaluates conditional expression and is
expression is true then only enters into the loop.
Syntax: while(conditional expression)
{
Statements;
}
Do … while loop: It is used to execute one or more statements repeatedly. In this loop, condition is checked
after executing all statements from the loop at least once. So even though the condition is false, statements
from loop executes at least once. It is exit controlled loop i.e. after execution of loop statements after every
iteration conditional expression is evaluated and if the condition is true then only next iteration executes.
Syntax: do
{
Statements;
}while(conditional expression);
do indicates enter the loop and execute all statements. Once control comes out of loop condition is evaluated.
If it evaluates to true then loop executes again otherwise loop exits.
Programs:
Develop a JavaScript code to accept one number and display whether it is even or odd using if statement.
<html> Prompt :
<body>
<script type="text/javascript">
var no=prompt("Enter number","1");
if(no%2==0)
document.write("Entered number is even");
else
document.write("Entered number is odd"); Output:
</script>
</body>
</html>
Develop a JavaScript code to accept student name and percentage from user. Display grade as distinction for
percentage>=75,first for percentage>=60,second for percentage>=45,pass for percentage >=40 and otherwise
fail using if … else statement.
<html> Prompt 1:
<body>
<script type="text/javascript">
var name=prompt("Enter name: ","rutuja");
var per=prompt("Enter percentage: ","80");
if(per>=75)
document.write(name + "- Your Grade is Distinction");
else if(per>=60 && per<75)
document.write(name + "- Your Grade is First"); Prompt 2:
else if(per>=45 && per<60)
document.write(name + "- Your Grade is Second");
else if(per>=40 && per<45)
document.write(name + "- Your Grade is Pass");
else
document.write(name + "- Your Grade is Fail");
</script>
</body> Output:
</html>
Develop JavaScript code to accept one number and display whether it is greater than 1000 and less than 2000
using nested if statement.
<html>
<body> Prompt :
<script type="text/javascript">
var no=prompt("Enter number: ","0");
if(no>1000)
{
if(no<2000)
{
document.write("Number" + no + " is in between 1000 &
2000");
}
else
Output:
{
document.write("Number" + no + " is not in between 1000 &
2000");
}
}
else
{
document.write("Number" + no + " is not in between 1000 &
2000");
}
</script>
</body>
</html>
Develop a JavaScript code to accept three names of flowers in an array and display them using for loop.
<html>
<body> Three prompt dialog boxes will be displayed
<script type="text/javascript"> to accept three names of flowers.
var flowers=new Array();
var i;
flowers[0]=prompt("Enter name");
flowers[1]=prompt("Enter name");
flowers[2]=prompt("Enter name");
for(i=0; i<flowers.length;i++)
document.write(flowers[i] + "<br>");
</script>
</body>
</html>
Develop a JavaScript code to accept three names of flowers in an array and display them using for… in loop.
<html> Three prompt dialog boxes will be displayed
<body> to accept three names of flowers.
<script type="text/javascript">
var flowers=new Array();
var i;
flowers[0]=prompt("Enter name");
flowers[1]=prompt("Enter name");
flowers[2]=prompt("Enter name");
for(i in flowers)
document.write(flowers[i] + "<br>");
</script>
</body>
</html>
Develop a JavaScript code to display table of entered number using while loop.
<html> Prompt :
<body>
<script type="text/javascript">
var no=prompt("Enter number: ","0");
var i=1;
document.write("Multiplication table for " + no);
while(i<=10)
{
document.write("<br>" + no + "*" + i + "=" + no*i); Output:
i++;
}
</script>
</body>
</html>
Develop JavaScript code to accept two numbers and perform addition, subtraction, multiplication and division
using switch … case statement.
<html> Prompt :
<body>
<script type="text/javascript">
var no1=prompt("Enter number: ","0");
var no2=prompt("Enter number: ","0");
var choice=prompt("Enter choice 1.Addition
2.Subtraction 3.Multiplication 4.Division : ","1"); Prompt :
switch(choice)
{
case '1':
document.write("Addition = " + (parseInt(no1)
+parseInt(no2)));
break;
case '2': Prompt :
document.write("Subtraction = " + (no1-no2));
break;
case '3':
document.write("Multiplication = " + (no1*no2));
break;
case '4': Output:
document.write("Division = " + (no1/no2));
break;
default:
document.write("Invalid Choice");
}
Conclusion:
With all the concepts based on decision making and looping statements, successfully developed and executed
all JavaScript codes with correct output.