Conditional and looping statement

Ad Code

Conditional and looping statement

 


JavaScript if else statement:
The if statement executes a statement, if a condition is satisfied.
Syntax
if( condition ) {
// statement
} else {
// statement (when condition evaluates to false)
}

Example 1
Enter two numbers then print Hihest between them.

<script>
var n = Number(prompt('Enter first number'));
var m = Number(prompt('Enter first number'));
if(n>m)
{
  document.write("Highest Number is:"+n);
}
else
{
  document.write("Highest Number is:"+m);
}
</script>


Example

<script type="text/javascript">
//Write a "Good morning" greeting if
//the time is less than 10
var d=new Date();
var time=d.getHours();
if (time<10)
 {
 document.write("<b>Good morning</b>");
 }
</script>


Nested if(If...else if...else Statement)

Using nested if( the if....else if...else) statement to select one of several blocks of code to be executed.

Syntax

if (condition1)
 {
 execute code if condition1 is true
 }
else if (condition2)
 {
 execute code if condition2 is true
 }
else
 {
 code to be executed if both are not true
 }


Example

<script type="text/javascript">
var d = new Date()
var time = d.getHours()
if (time<10)
 {
 document.write("<b>Good morning</b>");
 }
else if (time>10 && time<16)
 {
 document.write("<b>Good day</b>");
 }
else
 {
 document.write("<b>Good Night!</b>");
 }
</script>


JavaScript Switch Statement:

Use the switch statement to select one of many blocks of code to be executed just like as nested if statement.
The value of the expression n is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. Use break to prevent the code from running into the next case automatically.
The id attribute defines the HTML element and innerHTML property defines the content.

Syntax

switch(n)
{
case 1:
 execute code block 1
 break;
case 2:
 execute code block 2
 break;
default:
 code to be executed if n is different from case 1 and 2
}


Example

var d=new Date();
theDay=d.getDay();
switch (theDay)
{
case 5:
 document.write("Day of week Friday");
 break;
case 6:
 document.write("Day of week Saturday");
 break;
case 0:
 document.write("Day of week Sunday");
 break;
default:
 document.write("Day of week finding!");
}
</script> 


JavaScript Loops:
Loop is rotational statement which rotates the work again and again until condition reach.
In JavaScript, there are two different kind of loops:

  • for - loops through a block of code a specified number of times
  • while - loops through a block of code while a specified condition is true, first checks condition then work.

Syntax of for loop

for (var=startvalue;var<=endvalue;increment/decrement)
{
code to be executed
}


Example

<html>
<body>
<script type="text/javascript">
var i=0;
for (i=0;i<=10;i+=2)
{
document.write("The number is " + i);
document.write("<br />");
}
</script>
</body>
</html>


Syntax of while loop

while (condition) //var<=endvalue)
{
code to be executed
}


Example

<html>
<body>
<script type="text/javascript">
var i=2;
while (i<=10)
{
document.write("The number is " + i);
document.write("<br />");
i+=2;
}
</script>
</body>
</html>


The do...while Loop

This loop will execute the block of code once, and then it will repeat the loop again and again until the specified condition is true. First performs the work then checks the condition.

Syntax of while loop

do
{
code to be executed
}
while (var<=endvalue);


Example

<html>
<body>
<script type="text/javascript">
var i=2;
do
{
document.write("The number is " + i);
document.write("<br />");
i+=2;
}
while (i<=5);
</script>
</body>
</html>



Nested Loop:
When we use loop within loop is called nested loop.

Example

<html>
<script type="text/javascript">
var c=1,i=1;
while(i<=5)
{
  while(c<=i)
  {
     document.write("*");
     c++;
  }
  document.write("<br>");
c=1;
i++;
}
</script>
</html>

Post a Comment

0 Comments

Ad Code