Home / code / javascript

Control statements in Javascripts,

Object Manipulation statements

 

IF-statement

You can use the IF-statement for a logical condition.

if (condition)
{
  statement1
}
else
{
  statement2
}

I the condition is true, statement1 is executed. Else statement 2. The else-part is optional.

Example:

function checkdata ()
{
  if (document.form1.threeChar.value.length == 3)
  {
	return true
  }
  else
  {
	alert("Enter exactly three characters. " + document.form1.threeChar.value
	 + " is not valid.")
	return false
  }
}

SWITCH-statement

A switch statement is actually many IF-statements. You compare 1 expression to a list of possible labels. When a match is found the according statement is executed else the default statement is executed.

switch (expression)
{
  case label :
	statement;
	break;
  case label :
	statement;
	break;
  ...
  default : statement;
}

Example:

switch (expr)
{
  case "Oranges" :
	document.write("Oranges are $0.59 a pound.
"); break; case "Apples" : document.write("Apples are $0.32 a pound.
"); break; case "Bananas" : document.write("Bananas are $0.48 a pound.
"); break; case "Cherries" : document.write("Cherries are $3.00 a pound.
"); break; default : document.write("Sorry, we are out of " + expr + ".
") }

Well, this explains itself I think. One thing you have to remember is always place the break. If you forget the break all the statements below the correct label are executed.

FOR-statement

With a for-loop you can say how many times a condition must be repeated.

for (initial expression; condition; incrementexpression)
{
  statements
}

When a loop executes, the following occurs.

  1. The initializing expression, if any, is executed. This expression usually initializes one or more loop counters, but the syntax allows an expression of any degree of complexity.
  2. The condition expression is evaluated. If the value of condition is true the loop statement execute. If the value of condition is false, the for loop terminates.
  3. The statements executes.
  4. The update expression incrementexpression executes, and control returns to step 2.

<SCRIPT>
function howMany(selectObject)
{
  var numberSelected=0
  for (var i=0; i < selectOpject.options.length; i++)
  {
	if (selectObject.options[i].selected==true)
	  numberSelected++
  }
  return numberSelected
}
</SCRIPT>

<FORM NAME="selectForm">
<P><B>Choose some music types, then click the button:</B>
<BR>
<SELECT NAME="musicTypes" MULTIPLE>
  <OPTION SELECTED> R&B</OPTION>
  <OPTION> Jazz</OPTION>
  <OPTION> Blues</OPTION>
  <OPTION> New Age</OPTION>
  <OPTION> Classical</OPTION>
  <OPTION> Opera</OPTION>
</SELECT>
<P>
<INPUT TYPE="button" VALUE="How many are selected?"
onClick="alert ('Number of options selected: ' + howMany(document.selectForm.muzicTypes))">
</FORM>

DO..WHILE statement

The do..while statement repeats until a specified condition evaluated to false.
Importand to know here is that the statement is executed at least once, even if the condition is false.

do
{
  statement
}while (condition)

Example:

do
{
  i+=1
  document.write(i);
}while (i<5);

WHILE statement

A while statement executes its statements as long as a specified condition is true.

while (condition)
{
  statements
}

The condition test occurs before the statements in the loop are executed. If the conditions returns true, the statements are executed and the condition is tested again. If the condition returns false, execution stops and control is passed on to the next statement following while.

Example:

n=0
x=0
while (n < 3)
{
  n++
  x+=n
}

LABEL statement

A label provides a statement with an identifier that lets you refer to it elsewhere in your program. For example, you can use a label to identify a loop, and then use the break or continue statements to indicate whether a program should interrupt the loop or continue its execution.

label :
  statement

The value of label may be any JavaScript identifier that is not a reserved word. The statements that youidentify with a labelmay be any type

Example: The label markloop identifies a while loop

markloop:
  while (theMark == true)
  {
	doSomething()
  }

Break statement

Use the break statement to terminate a loop, switch, or loop statement.

  • When you use break with a while, do-while, for or switch statement, break terminates the innermost enclosing loop or switch immediately and transfer control to the following statement.
  • When you use break within an enclosing label statement, it terminates the statements and transfers control to the following statement. If you specify a label when you issue the break, the break statements terminates the specified statement.

The syntax of the break statement looks like the following:

break
break [label]

The first form of the syntax terminates the innermost enclosing loop, switch or label; the second form terminates the specific enclosing label statement.

Example:

for ( i=0; i<a.length; i++)
{
  if (a[i] = theValue);
	break;
}

The example iterates through the elements in an array until it find the index who's value is theValue

CONTINUE statement

The Continue statement can be used to restart a while, do-while,for or label statement.

  • In a while or for statement, continue terminates the current loop and continues execution of the loop with the next iteration. In contrast to the break statement, continue does not terminate the execution of the loop entirely. In a while loop, it jumps back to the condition. In a for loop, it jumps to the increment-expression.
  • In a label, continueis followed by a labelthat identifies a label statement. This type of continue restarts a label statement or continues execution of a labelled loop with the next iteration. continue must be in a looping statement identified by the label used by continue.
continue
continue [label]

Example:

i = 0
n = 0
while (i<5)
{
  i++
  if (i==3)
	continue
  n+=i
}

The example shows a while loop with a continue statement that executes when the value of i is 3. Thus, n takes the value 1,3,7 and 12

Object Manipulation statements

JavaScript uses the for..in and with statements to manipulate objects.

FOR..IN statement

The for..in statement iterates a specified variable over all the properties of an object. For each distinct property, JavaScript executes the specified statements.

for (variable in object)
{
  statements
}

Example:

function dump_props(obj, obj_name)
{
  var result = " "
  for (var i in obj)
  {
	result += obj_name + "." + i + " = " + obj[i] + "<BR>
  }
  result += "<HR>
  return result
}

The function takes its argument, an object and the object's name. It then iterates over all the object's properties and returns a string that lists the property names and there values.

WITH statement

The with statement establishes the default object for a set of statements. JavaScript looks up any unqualified names within the set of statements to determine if the names are properties of the default object. If an unqualified name matches a property, then the property is used in the statement; otherwise, a local or global variable is used.

with (object)
{
  statements
}

Example:

var a, x, y
var r=10
with (Math)
{
  a=PI*r*r
  x=r*cos(PI)
  y=r*sin(PI/2)
}

The with statement specifies that the Math-object is the default object. The statements following the with statement refer to the PI property and the cos and sin methods, without specifying an object. JavaScript assumes the Math object for these references.

Comments

Comments are author notations that explain what a script does. Comments are ignored by the interpreter. JavaScript supports Java-style comments:

  • Comments on a single line are preceded by a double-slash (//)
  • Comments that span multiple lines are preceded by /* and followed by */

Example:

// single line comment
/* This is a multiple-line comment. It can be of any length,
 and you can put whatever you want here. */

 

TOP

Latest script:

 

Books: