1. The default case is required in the switch selection statement.
Answer: False. The default case is optional. If no default action is needed, then there is no need for a default case.
2. The break statement is required in the last case of a switch selection statement.
Answer: False. The break statement is used to exit the switch statement. The break statement is not required for the last case in a switch statement.
3. The expression ( x > y && a < b ) is true if either x > y is true or a < b is true.
Answer: False. Both of the relational expressions must be true in order for the entire expression to be true when using the && operator.
4. An expression containing the || operator is true if either or both of its operands is true.
Answer: True
1. Sum the odd integers between 1 and 99. Use a for statement. Assume that the variables sum and count have been declared.
Answer:
sum = 0;
for ( count = 1; count <= 99; count += 2 )
sum += count;
2. Calculate the value of 2.5 raised to the power of 3. Use the pow method.
Answer: Math.pow( 2.5, 3 );
3. Print the integers from 1 to 20 by using a while loop and the counter variable x. Assume that the variable x has been declared, but not initialized. Print only five integers per line. [Hint: Use the calculation x % 5. When the value of this expression is 0, use document.write( "
" ) to output a line break in the XHTML document.]
Answer:
x = 1;
while ( x <= 20 ) {
document.write( x + " " );
if ( x % 5 == 0 )
document.write( "<br />" );
++x;
}
4. Repeat Exercise 8.2 (c), but using a for statement.
Answer:
for ( x = 1; x <= 20; x++ ) {
document.write( x + " " );
if ( x % 5 == 0 )
document.write( "<br />" );
}
or
for ( x = 1; x <= 20; x++ )
if ( x % 5 == 0 )
document.write( x + "<br />" );
else
document.write( x + " " );
1.
x = 1; while ( x <= 10 ); ++x; }
2.
for ( y = .1; y != 1.0; y += .1 ) document.write( y + " " );
for ( y = 1; y != 10; y++ ) document.writeln( ( y / 10 ) + " " );
3.
switch ( n ) { case 1: document.writeln( "The number is 1" ); case 2: document.writeln( "The number is 2" ); break; default: document.writeln( "The number is not 1 or 2" ); break; }
4.The following code should print the values from 1 to 10:
n = 1; while ( n < 10 ) document.writeln( n++ );
1.
For ( x = 100, x >= 1, x++ ) document.writeln( x );
2. The following code should print whether integer value is odd or even:
switch ( value % 2 ) { case 0: document.writeln( "Even integer" ); case 1: document.writeln( "Odd integer" ); }
3. The following code should output the odd integers from 19 to 1:
for ( x = 19; x >= 1; x += 2 ) document.writeln( x );
The following code should output the even integers from 2 to 100:
counter = 2; do { document.writeln( counter ); counter += 2; } While ( counter < 100 );
1 <?xml version="1.0" encoding="utf-8" ?> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4 5 <!-- Exercise 8.5: ex08_05.html --> 6 <html xmlns="http://www.w3.org/1999/xhtml"> 7 <head><title>Mystery</title> 8 <script type="text/javascript"> 9 <!-- 10 document.writeln( "<table>" ); 11 12 for ( var i = 1; i <= 10; i++ ) 13 { 14 document.writeln( "<tr>" ); 15 16 for ( var j = 1; j <= 5; j++ ) 17 document.writeln( "<td>(" + i + ", " + j + ")</td>" ); 18 19 document.writeln( "</tr>" ); 20 } // end for 21 22 document.writeln( "</table>" ); 23 --> 24 </script> 25 </head><body /> 26 </html>
Write a script that finds the smallest of several non-negative integers. Assume that the first value read specifies the number of values to be input from the user.
Answer: Answer to 8.6
Write a script that calculates the product of the odd integers from 1 to 15 then outputs XHTML text that displays the results.
Answer: Answer to 8.7
Modify the compound interest program in Fig. 8.6 to repeat its steps for interest rates of 5, 6, 7, 8, 9 and 10 percent. Use a for statement to vary the interest rate. Use a separate table for each rate.
Answer: Answer to 8.8
Write a script that outputs XHTML to display the given patterns separately, one below the other. Use for statements to generate the patterns. All asterisks (*) should be printed by a single statement of the form document.write( "*" ); (this causes the asterisks to print side by side). A statement of the form document.writeln( "
>" ); can be used to position to the next line. A statement of the form document.write( " " ); can be used to display a space (needed for the last two patterns). There should be no other output statements in the program. [Hint: The last two patterns require that each line begin with an appropriate number of blanks. You may need to use the XHTML <pre></pre> tags.]
1.
* ** *** **** ***** ****** ******* ******** ********* **********Answer: Answer to 8.9
2.
********** ********* ******** ******* ****** ***** **** *** ** *Answer: Answer to 8.9
3.
********** ********* ******** ******* ****** ***** **** *** ** *Answer: Answer to 8.9
4.
* ** *** **** ***** ****** ******* ******** ********* **********Answer: Answer to 8.9
One interesting application of computers is the drawing of graphs and bar charts (sometimes called histograms). Write a script that reads five numbers between 1 and 30. For each number read, output XHTML text that displays a line containing the same number of adjacent asterisks. For example, if your program reads the number 7, it should output XHTML text that displays *******.
Answer: Answer to 8.10
(“The Twelve Days of Christmas” Song) Write a script that uses repetition and a switch structures to print the song “The Twelve Days of Christmas.” You can find the words at the site
www.santas.net/twelvedaysofchristmas.htm
Answer: Answer to 8.11
A mail-order house sells five different products whose retail prices are as follows: product 1, $2.98; product 2, $4.50; product 3, $9.98; product 4, $4.49; and product 5, $6.87. Write a script that reads a series of pairs of numbers as follows:
1. Product number
2. Quantity sold for one day
Your program should use a switch statement to determine each product’s retail price and should calculate and output XHTML that displays the total retail value of all the products sold last week. Use a prompt dialog to obtain the product number and quantity from the user. Use a sentinel-controlled loop to determine when the program should stop looping and display the final results.
Answer: Answer to 8.12
Assume that i = 1, j = 2, k = 3 and m = 2. What does each of the given statements print? Are the parentheses necessary in each case?
1. document.writeln( i == 1 );
Answer: true
2. document.writeln( j == 3 );
Answer: false
3. document.writeln( i >= 1&& j < 4 );
Answer: true
4. document.writeln( m <= 99 && k < m );
Answer: false
5. document.writeln( j >= i || k == m );
Answer: true
6. document.writeln( k + m < j || 3 - j >= k );
Answer: true
7. document.writeln( !( k > m ) );
Answer: false
Modify Exercise 8.9 to combine your code from the four separate triangles of asterisks into a single script that prints all four patterns side by side, making clever use of nested for statements.
* ********** ********** * ** ********* ********* ** *** ******** ******** *** **** ******* ******* **** ***** ****** ****** ***** ****** ***** ***** ****** ******* **** **** ******* ******** *** *** ******** ********* ** ** ********* ********** * * **********Answer: Answer to 8.14
(De Morgan’s Laws) In this chapter, we have discussed the logical operators &&, || and !. De Morgan’s Laws can sometimes make it more convenient for us to express a logical expression. These laws state that the expression !(condition1 && condition2) is logically equivalent to the expression (!condition1 || !condition2). Also, the expression !(condition1 || condition2) is logically equivalent to the expression (!condition1 && !condition2). Use De Morgan’s Laws to write equivalent expressions for each of the following, then write a program to show that the original expression and the new expression are equivalent in each case:
1. !( x < 5 ) && !( y <= 7 )
Answer: Answer to 8.15
2. !( a == b ) || !( g != 5 )
Answer: Answer to 8.15
3. !( ( x <= 8 ) && ( y < 4 ) )
Answer: Answer to 8.15
4. !( ( i < 4 ) || ( j <= 6 ) )
Answer: Answer to 8.15
* *** ***** ******* ********* ******* ***** *** *
Modify the program you wrote in Exercise 8.16 to read an odd number in the range 1 to 19. This number specifies the number of rows in the diamond. Your program should then display a diamond of the appropriate size.
Answer: Answer to 8.17
A criticism of the break statement and the continue statement is that each is unstructured. Actually, break statements and continue statements can always be replaced by structured statements, although coding the replacement can be awkward. Describe in general how you would remove any break statement from a loop in a program and replace it with some structured equivalent. [Hint: The break statement “jumps out of” a loop from the body of that loop. The other way to leave is by failing the loop-continuation test. Consider using in the loop-continuation test a second test that indicates “early exit because of a ‘break’ condition.”] Use the technique you develop here to remove the break statement from the program in Fig. 8.11.
Answer: Answer to 8.18
1 <?xml version="1.0" encoding="utf-8" ?> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4 5 <!-- Exercise 8.19: ex08_19.html --> 6 <html xmlns="http://www.w3.org/1999/xhtml"> 7 <head><title>Mystery</title> 8 <script type="text/javascript"> 9 <!-- 10 for ( var i = 1; i <= 5; i++ ) 11 { 12 for ( var j = 1; j <= 3; j++ ) 13 { 14 for ( var k = 1; k <= 4; k++ ) 15 document.write( "*" ); 16 document.writeln( "<br />" ); 17 } // end for 18 document.writeln( "<br />" ); 19 } // end for 20 // --> 21 </script> 22 </head><body></body> 23 </html>
Describe in general how you would remove any continue statement from a loop in a program and replace it with some structured equivalent. Use the technique you develop to remove the continue statement from the program in Fig. 8.12.
Answer: Most continue statements can be replaced with a if..else, or a switch statement in the loop. However, this really is not needed. the text argus that using continue and or break is a violation of structure code. I have never met anyone who would agree. Over nesting usually tends to lead to more unfreindly code, and more mistakes. This is just my opion.
1 switch ( k ) 2 { 3 case 1: 4 break; 5 case 2: 6 case 3: 7 ++k; 8 break;