Online Documentation Server
 ПОИСК
ods.com.ua Web
 КАТЕГОРИИ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ПОДПИСКА

 О КОПИРАЙТАХ
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом.




Previous Table of Contents Next

Table 8-1. Conditional statements in Perl versus those in JavaScript.


Perl JavaScript

if (condition1) if (condition1)
{statements} {statements}
elsif (condition2) else
{statements} if (condition2)
else {statements}
{statements} else
{statements}

unless (condition) if (!condition)
{statements} {statements}
or
if(!condition)
{statements}
* Followed by an elsif or else
statement.
* Followed by an else statement.

statement if condition if (condition)
statement

statement unless condition if (!condition)
statement

The Nonexistent Switch Statement


Note:  JavaScript does not feature a switch statement similar to the one in C++. In this section we’ll refer to the switch statement in C++, and the possible workarounds in JavaScript. When JavaScript supports this statement, it should use the same syntax as in C++.


When your nested if-else structures build up, it becomes difficult to get on top of the script. A simple menu system would solve the problem by offering multiple choices or actions.

A switch statement in C++ solves the problem. It consists of two parts:

  • The declaration of the switch, which is the value to be tested
  • A list of cases which associate actions with certain values. A specific action is taken when the switch value matches the value in the corresponding case.

Here is the general syntax of the switch statement in C++:

switch (switchingVariable) {
 case value1 :
  statement1;
  break;
 case value2 :
  statement2;
  break;
 case value3 :
  statement3;
  break;
 default:
  statement4;
}

The break statement is supported by JavaScript—it is explained later in this chapter.

The switch statement starts by comparing the switchingVariable with the value of case1. If they match, statement1 is executed, and the switch statement is terminated by the break statement. If they do not match, testing continues. The testing continues until a break statement is reached or until the program drops through the bottom brace.

The switch statement in C++ is similar to the case statement in Pascal. Because JavaScript does not support this statement, you must use nested if-else constructions instead. For example, the preceding switch statement is equivalent to:

if (switchingVariable == value1)
   statement1
else
   if (switchingVariable == value2)
  statement2
   else
  if (switchingVariable == value3)
  statement3
  else
  statement4 // default case

Loop Statements

Loops are control structures that perform a set of actions more than once. Everyone agrees that a computer can calculate faster than a human. Using loops you can repeat calculations and take advantage of the computer’s ability to do them faster. Theoretically, a loop repeats only one statement. However, you already know that a statement can be a block of statements, allowing the repetition of many statements, perhaps the whole program. JavaScript features two basic loop types:

  • The for loop
  • The while loop

Each loop type has its own advantages. You will learn to choose the most suitable one for your task.

for Statement

Syntax:

for ([initialExpression;] [condition;] [operation])
statement

The most commonly used loop is the for one. Because a loop usually repeats more than one statement, you use a command block in the following format:

for ([initialExpression;] [condition;] [operation]) {
 statements
}

initialExpression is usually a statement or a variable declaration. It should evaluate to a single value and is typically used to initialize a counter variable. This expression may optionally declare new variables with the var keyword.

condition is a condition that is evaluated before each successive pass through the loop. The statement is executed only if the condition evaluates to true.

operation is a statement that is executed after each consecutive pass through the loop’s body. It is typically used to update or increment the counter variable, which counts the number of passes through the loop.

Consider the following looping script fragment:

var number1 = 1
var number2 = 1

for (var counter = 1; counter <= 10; counter++) {
   document.write(number1 + " ")
   number2 = number2 + number1
   number1 = number2 – number1
}

This piece of code prints the first ten numbers of the Fibonacci sequence. The Fibonacci sequence includes the following numbers:

1 1 2 3 5 8 13 21 34 55 89 144 . . . (f1 = 1, f2=1, fx = fx–1 + fx–2)

Each number is equal to the sum of the two preceding numbers. The first two numbers are both equal to 1.

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us