|
|||
Basic Information on Using JavaScriptConditional Statements Loop Statements Loop Statement Examples Mathematical Expressions NOTE: This page, as well as the other pages to a certain extent, presupposes some general appreciation of computer programming and HTML. Conditional StatementsThe conditional statements available in JavaScript are if ... else and switch. The general structure of the if ... else is shown below:if (condition){ truestatements } else { falsestatements } The truestatements are executed if the condition is true. The falsestatements are executed if the condition is false. The switch statement is also used as a conditional statement. It has the following syntax: switch (expression){ case label1 : statement; break; case label2 : statement; break; ... default : statement; } The value of the expression is evaluated, and if label matches this value, the label's statements are executed. If no matching label is found, the default statements are executed. The break after each set of statements is optional, but they ensure that the loop is exited after the statments associated with a certain label are executed. LoopsThe for and while statements are the two loop statements available to JavaScript. The for statement has the following general structure:for ([initial expression]; [condition]; [increment expression]){ statements } The intial expression is the expression of the initial condition of the loop. It usually contains an expression stating the intial value of a single variable. While the condition is true, the loop statements are executed. The increment expression describes how the variable is incremented each pass through the loop. The while statement has the general structure: while (condition) { statements } The statements in the while loop are executed when the condition is true. There is also a do...while statement which looks like: do { statements } while (condition) This loop statement executes once before the condition is checked. The loop executes until the condition is false. Loop ExamplesThe following is an example utilizing some of the concepts already discussed. The following code:<SCRIPT LANGUAGE="JavaScript1.1"> for (var i=1; i==50; i++ ){ if ((i % 10) == 0) { document.write(i + "<BR>"); } else { document.write(i + " "); } } would result in: Notice that the increment expression i++ is equivalent to the expression i = i + 1. The expression i % 10 is equivalent to the expression i mod 10 which appears in other languages. This expression, in conjunction with the if responsible for determining which values of i are divisible by 10 with no remainder. When these values are encountered a break <Br> generated by the JavaScript to produce a new line in the browser. The same result could be accomplished by the code below: <SCRIPT LANGUAGE="JavaScript1.1"> i = 0 while (i < 50) { i++ if ((i % 10) == 0) { document.write(i + "<BR>"); } else { document.write(i + " "); } } producing: This works in a similiar mannner to the previous script; however, it utilizes a while loop. Mathematical ExpressionsA number of mathematical constants and functions are available in JavaScript under the built-in Math object. A complete listing will not be provided here; you can refer to a more extensive reference if you would like to find out more. For example, the script below:<SCRIPT LANGUAGE="JavaScript1.1"> document.write(Math.E + "<BR>"); document.write(Math.PI + "<BR>"); document.write(Math.sin(Math.PI / 2) + "<BR>"); document.write(Math.cos(Math.PI / 3) + "<BR>"); document.write(Math.sqrt(2) + "<BR>"); document.writeln(Math.log(10) + "<BR>"); </SCRIPT> would produce the following: Notice that the Math.log(argument) method returns the natural logarithm of the argument. If you would like to find out more about the many methods available to JavaScript, probably the best reference available is the Netscape JavaScript Authoring Guide which contains reference material on using JavaScript. |
|||
|
|||
|
|
|||