|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
Testing Conditions JavaScript provides a single type of control statement for making decisions: the if…else statement. To make a decision, you supply an expression that evaluates to TRUE or FALSE; which code is executed depends on what your expression evaluates to. The simplest form of if…else uses only the if part. If the specified condition is TRUE, the code following the condition is executed; if not, its skipped. In the following code fragment, for example, the message appears only if the condition (that the lastModified.year property of the document object says it was modified before 1995) is TRUE: if (document.lastModified.year < 1995) document.write(Danger! This is a mighty old document.) You can use any expression as the condition. Because you can nest expressions and combine them with the logical operators, your tests can be pretty sophisticated. For example: if ((document.lastModified.year >= 1995) && (document.lastModified.month >= 10)) document.write(This document is reasonably current.) The else clause enables you to specify a set of statements to execute when the condition is FALSE. For instance, if ((document.lastModified.year >= 1995) && (document.lastModified.month >= 10)) document.write(This document is reasonably current.) else document.write(This document is quite old.) Repeating Actions JavaScript provides two loop constructs that you can use to repeat a set of operations. The first, called a for loop, executes a set of statements some number of times. You specify three expressions: an initial expression that sets the values of any variables you need to use, a condition that tells the loop how to see when it is done, and an increment expression that modifies any variables that need it. Heres a simple example: for (count=0; count < 100; count++) document.write(Count is , count); This loop executes 100 times and prints out a number each time. The initial expression sets the counter, count, to zero. The condition tests to see whether count is less than 100 and the increment expression increments count. You can use several statements for any of these expressions, as follows: for (count=0, numFound = 0; (count < 100) && (numFound < 3); count++) if (someObject.found()) numFound++; This loop either loops 100 times or as many times as it takes to find three itemsthe loop condition terminates when count >= 100 or when numFound >= 3. The second form of loop is the while loop. It executes statements as long as its condition is TRUE. You can rewrite the first for loop in the preceding example, for instance, as follows: count = 0 while (count < 100) { count++; if (someObject.found()) numFound++; document.write(Count is , count) } Which form you use depends on what you are doing; for loops are useful when you want to perform an action a set number of times, and while loops are best when you want to keep doing something as long as a particular condition remains TRUE. Notice that by using braces, you can include more than one command to be executed by the while loop. (This is also true of for loops and if...else constructs.) JavaScript Reserved WordsJavaScript reserves some keywords for its own use. You cannot define your own methods or properties with the same name as any of these keywords; if you do, the JavaScript interpreter complains.
Table 18.3 shows some of JavaScripts reserved keywords.
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |