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

Exp 2

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)
5 views

Exp 2

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.

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.

Syntax: for(initializer ; conditional expression ; post loop statement)


{
Loop statements;
}
- Initializer: It is a counter variable that stores count for execution of loop.
- Conditional expression: It is a condition that terminates the loop when desired number of iterations are
completed.
- Post loop statement: It is used to increment or decrement the counter variable after each iteration.
- Statements: It contains one or more statements to be executed in each iteration when condition evaluates
to true value.
For … in loop: It is a special kind of loop that is used when browser executes statements within the code
block for each item on a list. It is used to retrieve all properties of an object available in JavaScript. For
example, if the list contains five items then browser executes loop for five times.
Syntax: for(list)
{
Statements;
}
- List contains list of items stored in an array.
- Statements are the instructions to be executed for each item in a list.

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy