Java Reference
In-Depth Information
Looping continues while the test condition is true . Each loop executes the block of
code and then executes the increment part of the for loop before re‐evaluating the
test condition to see if the results of incrementing have changed it.
The for . . . in loop: This is useful when you want to loop through an array without
knowing the number of elements in the array. JavaScript works this out for you so
that no elements are missed.
The while loop: This is useful for looping through some code for as long as a test
condition remains true . It consists of a test condition and the block of code that's
executed only if the condition is true . If the condition is never true , the code never
executes.
The do . . . while loop: This is similar to a while loop, except that it executes the code
once and then keeps executing the code as long as the test condition remains true .
break and continue statements: Sometimes you have a good reason to break out of a loop
prematurely, in which case you need to use the break statement. On hitting a break state-
ment, code execution stops for the block of code marked out by the curly braces and starts
immediately after the closing brace. The continue statement is similar to break , except that
when code execution stops at that point in the loop, the loop is not broken out of but instead
continues as if the end of that reiteration had been reached.
exerCises
You can find suggested solutions to these questions in Appendix A.
1. A junior programmer comes to you with some code that appears not to work. Can you spot
where he went wrong? Give him a hand and correct the mistakes.
var userAge = prompt("Please enter your age");
if (userAge = 0) {;
alert("So you're a baby!");
} else if ( userAge < 0 | userAge > 200)
alert("I think you may be lying about your age");
else {
alert("That's a good age");
}
2. Using document.write() , write code that displays the results of the 12 times table. Its output
should be the results of the calculations.
12 * 1 = 12
12 * 2 = 24
12 * 3 = 36
. . .
12 * 11 = 132
12 * 12 = 144
 
Search WWH ::




Custom Search