|
Table 8-1. Conditional statements in Perl versus those in JavaScript.
The Nonexistent Switch Statement
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:
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 StatementsLoops 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:
Each loop type has its own advantages. You will learn to choose the most suitable one for your task. for StatementSyntax:
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.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
With any suggestions or questions please feel free to contact us |