Additional Javascript Flow Control Statements
The break statement
The continue statement
Javascript for...in statement
Javascript switch statement
The break and continue statements are used to control loop execution.
The break statement can you used to halt execution of a loop if , for example an error condition is encountared.
The continue statement is used to begin the next iteration of a loop without executing all the statements in the block
The break statement
The break statement will terminate execution of the loop and continue executing the code that follows after the loop(if any)
<html>
<body>
<script type="text/javascript">
var i=0;
for(i=0;i<=10;i++)
{
if(i==3)
{
break;
}
document.write("The number is "+i);
document.write("<br/>");
}
</script>
<p>Explanation: The loop will break when i=3</p>
</body>
</html>
The continue statement
the continue statement will terminate the current iteration and restart the loop with the next value.
<html>
<body>
<script type="text/javascript">
var i=0;
for(i=0;i<=10;i++)
{
if(i==3)
{
continue;
}
document.write("The number is "+i);
document.write("<br/>");
}
</script>
<p>Exxplanation:The loop will break the current loop and continue with the next value when i=3</p>
</body>
</html>
javascript for...in statement
The for ... in statement loops through the elements of an array or through the properties of an object.
for(variable in object)
{
code to be executed
}
the code in the body of the for... in loop is executed once for each element/proparty.
the variable argument can be a named variable, an array element, or a property of an object
<html>
<body>
<script type="text/javascript">
var x;
var mycars=new array();
mycars[0]="saab";
mycars[1]="volvo";
mycars[2]="BMW";
for(x in mycars)
{
document.write(mycars[x]+"<br/>");
}
</script>
</body>
</html>
conditional statements are used to perform different actions based on different conditions
Javascript switch statement
use the switch statement to select one of many blocs of code to be executed
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
}
<html>
<body>
<script type="text/javascript">
var i=1;
switch(i)
{
case 0:
document.write("<b>i==0</b><br/>");
case 1:
document.write("<b>i==1</b><br/>");
case 2:
document.write("<b>i==2</b><br/>");
break;
case 3:
document.write("<b>i==3</b><br/>");
break;
default:
document.write("<b>i>3</b><br>");
}
</script>
<p>Note that when i==1 execution begins with case 1: but continues until the break statement is encountered.</>
</body>
</html>
<html>
<body>
<script type="text/javascript">
var d=new Date();
var time = d.getDay();
switch(theDay)
{
case 5:
document.write("<b>Finally Friday</b>");
break;
case 6:
document.write("<b>Finally Saturday</b>");
break;
case 0:
document.write("<b>Finally Sunday</b>");
break;
default:
document.write("<b>I'm really looking forward to this weekend!</b>");
break;
}
</script>
<p>This Javascript will generate a different greeting based on what day it is.
Note that sunday=0,monday=1,tuesday=2 etc.</p>
</body>
</html>
The break statement
The continue statement
Javascript for...in statement
Javascript switch statement
The break and continue statements are used to control loop execution.
The break statement can you used to halt execution of a loop if , for example an error condition is encountared.
The continue statement is used to begin the next iteration of a loop without executing all the statements in the block
The break statement
The break statement will terminate execution of the loop and continue executing the code that follows after the loop(if any)
<html>
<body>
<script type="text/javascript">
var i=0;
for(i=0;i<=10;i++)
{
if(i==3)
{
break;
}
document.write("The number is "+i);
document.write("<br/>");
}
</script>
<p>Explanation: The loop will break when i=3</p>
</body>
</html>
The continue statement
the continue statement will terminate the current iteration and restart the loop with the next value.
<html>
<body>
<script type="text/javascript">
var i=0;
for(i=0;i<=10;i++)
{
if(i==3)
{
continue;
}
document.write("The number is "+i);
document.write("<br/>");
}
</script>
<p>Exxplanation:The loop will break the current loop and continue with the next value when i=3</p>
</body>
</html>
javascript for...in statement
The for ... in statement loops through the elements of an array or through the properties of an object.
for(variable in object)
{
code to be executed
}
the code in the body of the for... in loop is executed once for each element/proparty.
the variable argument can be a named variable, an array element, or a property of an object
<html>
<body>
<script type="text/javascript">
var x;
var mycars=new array();
mycars[0]="saab";
mycars[1]="volvo";
mycars[2]="BMW";
for(x in mycars)
{
document.write(mycars[x]+"<br/>");
}
</script>
</body>
</html>
conditional statements are used to perform different actions based on different conditions
Javascript switch statement
use the switch statement to select one of many blocs of code to be executed
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
}
<html>
<body>
<script type="text/javascript">
var i=1;
switch(i)
{
case 0:
document.write("<b>i==0</b><br/>");
case 1:
document.write("<b>i==1</b><br/>");
case 2:
document.write("<b>i==2</b><br/>");
break;
case 3:
document.write("<b>i==3</b><br/>");
break;
default:
document.write("<b>i>3</b><br>");
}
</script>
<p>Note that when i==1 execution begins with case 1: but continues until the break statement is encountered.</>
</body>
</html>
<html>
<body>
<script type="text/javascript">
var d=new Date();
var time = d.getDay();
switch(theDay)
{
case 5:
document.write("<b>Finally Friday</b>");
break;
case 6:
document.write("<b>Finally Saturday</b>");
break;
case 0:
document.write("<b>Finally Sunday</b>");
break;
default:
document.write("<b>I'm really looking forward to this weekend!</b>");
break;
}
</script>
<p>This Javascript will generate a different greeting based on what day it is.
Note that sunday=0,monday=1,tuesday=2 etc.</p>
</body>
</html>
No comments:
Post a Comment