#### Decision/Conditional(if-else)Statement Examples

##### Example : A JS program to print large values from two values.
``````<html>
<h4>Logical Comparision</H4>
<body>
<script>
x=20;
y=30;
if(x>y)
{
document.write("First value is Larger");
}
if(x<y)
{
document.write("Second value is Larger");
}
if(x==y)
{
document.write("Both values are equal");
}

</script>
</body>
</html>

--------------------  OR  --------------------

<html>
<h4>Logical Comparision</H4>
<body>
<script>
x=20;
y=30;
if(x>y)
{
document.write(x +" is Larger");
}
if(x<y)
{
document.write(y +" is Larger");
}
if(x==y)
{
document.write("Both values are equal");
}

</script>
</body>
</html>

--------------------  OR  --------------------

<html>
<h4>Logical Comparision</H4>
<body>
<script>
var x=prompt("Enter first value", "Enter Numeric Value");
var y=prompt("Enter second value", "Enter Numeric Value");
if(x>y)
{
document.write("First value" + x + "is Larger");
}
if(x<y)
{
document.write("Second value" + y + "is Larger");
}
if(x==y)
{
document.write("Both values are equal");
}

</script>
</body>
</html>``````
##### Example : A JS program to check the no. is even or odd.
``````<html>
<h4>Odd Even Program</H4>
<body>
<script>
x=20;
if(x%2==0)
{
document.write(x +" is Even no.");
}
else
{
document.write(x +" is Odd no.");
}
</script>
</body>
</html>

--------------------  OR  --------------------

<html>
<h4>Odd Even Program</H4>
<body>
<script>
var x=prompt("Enter a tble value");
if(x%2==0)
{
document.write(x +" is Even no.");
}
else
{
document.write(x +" is Odd no.");
}
</script>
</body>
</html>``````
##### Example : A JS program to Print the Grade of Students of an Exam.
``````<html>
<h4>Table values Program</H4>
<body>
<script>
var totmarks=prompt("Enter total marks of students");
if(totmarks>=800 && totmarks<=900)
{
}
else if(totmarks>=600 && totmarks<800)
{
}
else if(totmarks>=400 && totmarks<600)
{
}
else
{
document.write("<b>Not Satisfactory</b>");
}
</script>
</body>
</html>``````
##### Example : A JS program to Print the name of the week on entering values from 1-7 using Switch Case Statement.
``````<html>
<h4>Switch case Program</H4>
<body>
<script>
var choice=parseInt(prompt("Enter your choice between 1-7 to see name of week"));
switch(choice)
{
case 1:
{
document.write("Sunday");
break;
}
case 2:
{
document.write("Monday");
break;
}
case 3:
{
document.write("Tuesday");
break;
}
case 4:
{
document.write("<b>Wednesday</b>");
break;
}
case 5:
{
document.write("<b>Thursday</b>");
break;
}
case 6:
{
document.write("<b>Friday</b>");
break;
}
case 7:
{
document.write("<b>Saturday</b>");
break;
}
default:
{
document.write("<b>Invalid Entered Choice</b>");
}
}
</script>
</body>
</html>``````
##### Example : A JS program to print the output of Simple Calculator using Switch Case statement.
``````<!DOCTYPE html>
<html>
<title>Simple Calculator</title>

<script>
function calculate()
{
var num1=parseInt(prompt("User Prompt Instruction for Simple Calculator",'Enter first numeric value'));
var num2 = parseInt(prompt("User Prompt Instruction for Simple Calculator",'Enter Second numeric value'));

var opt = prompt("User Prompt Instruction for Simple Calculator","Now Enter the Operator symbol such as +,-,*,/,%",'User Prompt Instruction');
var result;

switch(opt)
{
case '+':
{
result = num1 + num2;
break;
}
case '-':
{
result = num1 - num2;
break;
}
case '*':
{
result = num1 * num2;
break;
}
case '/':
{
result = num1 / num2;
break;
}
case '%':
{
result = num1 % num2;
break;
}

default:
{
document.write("<b>Invalid/Wrong Choice</b>");
}
}
alert("The result is = " + result);
}
</script>

<body>
<h1>Simple Calculator Using JS</h1>
<form>
<td><button onclick="calculate()">Click for Simple Calculator</button></td>
</form>
</body>
</html>``````

#### Looping Statement Examples

##### Example : A JS program to print all the values from 50 to 60.
``````<html>
<h4>Loop values Program</H4>
<body>
<script>

for(var i=50; i<=60; i=i+1)
{
document.write(i+" ");
//document.write(i+"</br>");
}
</script>
</body>
</html>``````
##### Example : A JS program to print all the values of a table given by the user using for, while, and do-while loop.
``````<html>
<h4>Table values Program</H4>
<body>
<script>
var x=prompt("Enter a table value");
for(var i=1; i<=10; i=i+1)
{
document.write((i*x)+"</br>");
}
</script>
</body>
</html>

--------------------  OR  --------------------

<html>
<h4>Table values Program</H4>
<body>
<script>

var x=prompt("Enter a table value");
var i=1;
while(i<=10)
{
document.write((x*i)+"</br>");
i=i+1;
}

</script>
</body>
</html>

--------------------  OR  --------------------

<html>
<h4>Table values Program</H4>
<body>
<script>
var x=prompt("Enter a table value");
var i=1;
do
{
document.write((x*i)+"</br>");
i=i+1;
}while(i<=10);
</script>
</body>
</html>``````
##### Example : A JS program to print all the values from an array using a for-in looping statement.
``````<html>
<body>
<script>
var fruits = ["Apple", "Banana", "Mango", "Orange", "Papaya", "Guava", "PineApple", "Pomegranate"];

for(var i in fruits)
{
document.write(fruits[i] + "</br>");
}
</script>
</body>
</html>

Output :
Apple
Banana
Mango
Orange
Papaya
Guava
PineApple
Pomegranate``````
##### Example : A JS program to display the output using the concept of With Statement.
``````<html>
<script>
{
with(this)
{
price = amount;
}
}

function book_details(title, author)
{
this.title = title;
this.author = author;
this.price = 0;
}
</script>

<body>
<script>
var ObjBook = new book_details("Mahabharat", "VedVyas");
document.write("The holy book title is : " + ObjBook.title + "<br>");
document.write("The holy book author is : " + ObjBook.author + "<br>");
document.write("The holy book price is : " + ObjBook.price + "<br>");
</script>
</body>
</html>

Output:
The holy book title is : Mahabharat
The holy book author is : VedVyas
The holy book price is : 750``````
##### Example : A JS program to print all the values except a few values from a range of values given by the user using the Continue statement.
``````<html>
<h4>Loop values Program</H4>
<body>
<script>
for(var i=50; i<=60; i=i+1)
{
if(i==54 || i==56)
{
continue;
}
document.write(i+" ");
//document.write(i+"</br>");
}
</script>
</body>
</html>

Output : 50 51 52 53 55 57 58 59 60``````
##### Example : A JS program to print all the values using a Label Break statement.
``````<html>
<body>
<script type="text/javascript">

outerloop:
for (var i = 0; i < 5; i++)
{
document.write("Outerloop value is : " + i + "<br />");

innerloop:
for (var j = 0; j < 5; j++)
{
if (j > 3 ) break ; // Quit the innermost loop
if (i == 2) break innerloop; // Do the same thing
if (i == 4) break outerloop; // Quit the outer loop
document.write("Innerloop value is : " + j + " <br />");
}
}
document.write("<br/>Program is Terminated now<br/> ");
</script>
</body>
</html>

Output :

Outerloop value is : 0
Innerloop value is : 0
Innerloop value is : 1
Innerloop value is : 2
Innerloop value is : 3

Outerloop value is : 1
Innerloop value is : 0
Innerloop value is : 1
Innerloop value is : 2
Innerloop value is : 3

Outerloop value is : 2
Outerloop value is : 3

Innerloop value is : 0
Innerloop value is : 1
Innerloop value is : 2
Innerloop value is : 3

Outerloop value is : 4

Program is Terminated now``````
##### Example : A JS program to print all the values using the Label Continue statement.
``````<html>
<body>
<script>
outerloop:
for (var i = 0; i < 3; i++)
{
document.write("Outerloop value is : " + i + "<br/>");
for (var j = 0; j < 5; j++)
{
if (j == 3)
{
continue outerloop;
}
document.write("Innerloop value is : " + j + "<br/>");
}
}
document.write("Program is Exiting<br /> ");
</script>
</body>
</html>

Output:

Outerloop value is : 0
Innerloop value is : 0
Innerloop value is : 1
Innerloop value is : 2

Outerloop value is : 1
Innerloop value is : 0
Innerloop value is : 1
Innerloop value is : 2

Outerloop value is : 2
Innerloop value is : 0
Innerloop value is : 1
Innerloop value is : 2

Program is Exiting``````