70Part IIJavaScript TutorialIn your trip to the (Yahoo free web hosting) store,
70Part IIJavaScript TutorialIn your trip to the store, you go through the same kinds of decisions and repetitions that yourJavaScript programs also encounter. If you understand these frameworks in real life, you cannow look into the JavaScript equivalents and the syntax required to make them work. Control StructuresIn the vernacular of programming, the kinds of statements that make decisions and looparound to repeat themselves are called control structures. A control structure directs the exe- cution flow through a sequence of script statements based on simple decisions and otherfactors. An important part of a control structure is the condition. Just as you may travel differentroutes to work depending on certain conditions (for example, nice weather, nighttime, attend- ing a soccer game), so, too, does a program sometimes have to branch to an execution routeif a certain condition exists. Each condition is an expression that evaluates to trueorfalse one of those Boolean data types mentioned in Chapter 6. The kinds of expressionscommonly used for conditions are expressions that include a comparison operator. You dothe same in real life: If it is true that the outdoor temperature is less than freezing, you put ona coat before going outside. In programming, however, the comparisons are strictly compar- isons of values. JavaScript provides several kinds of control structures for different programming situations. Three of the most common control structures you ll use are ifconstructions, if…elseconstructions, and forloops. Chapter 31 covers in great detail other common control structures you should know. For thistutorial, however, you need to learn about the three common ones just mentioned. if constructionsThe simplest program decision is to follow a special branch or path of the program if a cer- tain condition is true. Formal syntax for this construction follows. Items in italics get replacedin a real script with expressions and statements that fit the situation. if (condition) { statement[s] if true} Don t worry about the curly braces yet. Instead, get a feel for the basic structure. The key- word, if, is a must. In the parentheses goes an expression that evaluates to a Boolean value. This is the condition being tested as the program runs past this point. If the condition evalu- ates to true, one or more statements inside the curly braces execute before continuing onwith the next statement after the closing brace. If the condition evaluates to false, the state- ments inside the curly braces are ignored and processing continues with the next statementafter the closing brace. The following example assumes that a variable, myAge, has had its value set earlier in thescript (exactly how is not important for this example). The condition expression comparesthe value myAgeagainst a numeric value of 18. if (myAge < 18) { alert( Sorry, you cannot vote. ); }
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision virtual web hosting services