Friday, May 22, 2009

0

For....Next Loop In VB.


For...Next Loop.


Use the For...Next statement to run a block of code a specified number of times.

The For statement specifies the counter variable (i), and its start and end values. The Next statement increases the counter variable (i) by one.

Example.


<html>
<body>

<script type="text/vbscript">
for i = 0 to 5
document.write("The number is " & i & "<br />")
next
</script>

</body>
</html>

The Step Keyword.


With the Step keyword, you can increase or decrease the counter variable by the value you specify.

In the example below, the counter variable (i) is INCREASED by two, each time the loop repeats.

For i=2 To 10 Step 2
some code
Next

To decrease the counter variable, you must use a negative Step value. You must specify an end value that is less than the start value.

In the example below, the counter variable (i) is DECREASED by two, each time the loop repeats.

For i=10 To 2 Step -2
some code
Next

Exit a For...Next.


You can exit a For...Next statement with the Exit For keyword.
0

VBScript How To.


VBScript How To


The HTML <script> tag is used to insert a VBScript into an HTML page.

Put a VBScript into an HTML page


The example below shows how to use VBSript to write text on a web page:




Example


<html>
<body>
<script type="text/vbscript">
document.write("Hello World!")
</script>
</body>
</html>

The example below shows how to add HTML tags to the VBScript:

Example ( IE Only )


<html>
<body>
<script type="text/vbscript">
document.write("<h1>Hello World!</h1>")
</script>
</body>
</html>
0

JavaScript Throw Statement.


JavaScript Throw Statement.


The throw statement allows you to create an exception.

The Throw Statement.


The throw statement allows you to create an exception. If you use this statement together with the try...catch statement, you can control program flow and generate accurate error messages.

Syntax.


throw(exception)

The exception can be a string, integer, Boolean or an object.

Note that throw is written in lowercase letters. Using uppercase letters will generate a JavaScript error!

Example.
The example below determines the value of a variable called x. If the value of x is higher than 10, lower than 0, or not a number, we are going to throw an error. The error is then caught by the catch argument and the proper error message is displayed:

Example.


<html>
<body>
<script type="text/javascript">
var x=prompt("Enter a number between 0 and 10:","");
try
{
if(x>10)
{
throw "Err1";
}
else if(x<0)
{
throw "Err2";
}
else if(isNaN(x))
{
throw "Err3";
}
}
catch(er)
{
if(er=="Err1")
{
alert("Error! The value is too high");
}
if(er=="Err2")
{
alert("Error! The value is too low");
}
if(er=="Err3")
{
alert("Error! The value is not a number");
}
}
</script>
</body>
</html>
0

JavaScript For...In Statement.


JavaScript For...In Statement.


The for...in statement loops through the elements of an array or through the properties of an object.

Syntax.


for (variable in object)
{
code to be executed
}
Note: The code in the body of the for...in loop is executed once for each element/property.

Note: The variable argument can be a named variable, an array element, or a property of an object.

Example.
Use the for...in statement to loop through an array:

Example.


<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>

Thursday, May 21, 2009

0

JavaScript Break and Continue Statements.


The break Statement.


The break statement will break the loop and continue executing the code that follows after the loop (if any).

Example.


<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>
</body>
</html>

The continue Statement.


The continue statement will break the current loop and continue with the next value.

Examplet.


<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>
</body>
</html>
1

Do while Loop in Java Script (JS).


The do...while Loop.


The do...while loop is a variant of the while loop. This loop will execute the block of code ONCE, and then it will repeat the loop as long as the specified condition is true.

Syntax.


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

Example.
The example below uses a do...while loop. The do...while loop will always be executed at least once, even if the condition is false, because the statements are executed before the condition is tested:

Example.


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

JavaScript While Loop.


Loops execute a block of code a specified number of times, or while a specified condition is true.

The while Loop.


The while loop loops through a block of code while a specified condition is true.

Syntax.


while (var<=endvalue)
{
code to be executed
}
Note: The <= could be any comparing statement.

Example.
The example below defines a loop that starts with i=0. The loop will continue to run as long as i is less than, or equal to 5. i will increase by 1 each time the loop runs:

Example.


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