Introduction to JavaScript Tutorial
By Neil Williams
2007-11-09
JavaScript Statements A-F
Simple statements
Statements make up the backbone of a Javascript program. Each time a
variable is created, changed, deleted, or output, each time a function
is declared, called or assigned, a statement is created. The following
are all examples of simple statements:
var s;
s="Hello, World!";
x=Math.sqrt(4);
y++;
Compound statements
To execute a series of statements as one, use {} brackets. Function declarations,
while and for loops and if and
switch conditionals all use {} brackets.
y=4; // simple statement
if (x==2) {
// conditional compound statement
y+=x;
c=Math.random();
y*=c;
}
break
Break from executing the current loop, if more than one, break stops
execution of the innermost loop, handing control back to the next
innermost. This example code never allows the value of x to exceed 8,
despite the loop being able to run to 14.
x=0;
while (x<=14) {
if (x==8) {break;}
x++;
}
case
Case is a keyword used to label compound statements within a switch statement.
switch (x) {
case 1 : {y=x;break;}
case 2 : {y=x;}
case 3 :
case 4 : {y=x;break;}
default : {y=0;break;}
}
Note that if x equals 2 or 3, the switch statement continues execution on to the
case 4 :
statement where the break statement stops further execution.
continue
Continue is used to restart the innermost loop.
x=1;y=1;
while (x<10) {
y++;
if (y==3) break;
else continue;
x++;
}
In this example, the x++ statement is never executed. At the
end of the loop, x remains equal to one and y is incremented to 3, at
which point the loop reaches break before the continue can be executed. Note that as x is never incremented, the loop must exit using break as the condition while (x<10) will never be met and the loop becomes infinite - your Javascript program will be terminated.
default
default is a keyword used in a switch statement usually as the last in
a series of case statements. The default case only executes if no case
statement matches the value of the expression in the switch statement.
do
Javascript 1.2 only
Use do to extend the function of the while loop. By default while doesn't execute any code within the loop unless the while condition evaluates to true. If you need the code to execute ONCE even if the while condition doesn't (yet) evaluate to true, use do to enclose the code:
x=1;y=1;
do {
y++;
if (y==3) break;
else continue;
x++;
}
while (x<10);
for
Simple loops can use for to save code by initialising, testing and
incrementing the test conditions in one statement:
for (initialise ; test ; increment) {}
for (x=0;x<10;x++) {}
The loop executes repeatedly until the test condition evaluates to
true. Initialisation code is executed once before the first evaluation
of the test or the loop. The test condition is evaluated before each
loop is executed and the increment expression is executed only once the
loop has completed (i.e. just before the next test condition
evaluation).
function
Functions are a way of executing compound statements in one HTML or
Javascript call with the extra flexibility of passing variables to the
function.
function my_func(x,y,z) {
a=x+y-z;
alert(a);
}
This function can be called from HTML as:
<a href="javascript:my_func(10,2,3);">Test</a>
Test
In this example, the alert box should show the result as 9.
Tutorial Pages:
»
It's flavours and versions
»
Structure and syntax
»
Data types and objects
»
Functions and operators
» JavaScript Statements A-F
»
JavaScript Statements G-Z
»
JavaScript Events
»
JavaScript Global Properties
»
Javascript code to identify your browser
»
Redirecting the browser once identified
Copyright © Neil Williams
|
