Chapter 8

8.1

a) False. The default case is optional. If no dfault action is needed, then there is no need for a default case.

b). False. The break statment is used to exit the switch stament and not required to exit the last statement.

c). False. Both must be true for AND to be true.

d). True.

8.2

a) sum = 0;

for ( count = 1; count <= 99; count += 2 )

sum += count;

b) Math.pow( 2.5, 3 )

c) x = 1;

while ( x <= 20 ) {

document.write( "<br/>");

++x;

}

d) 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 + " " );

8.3

a) Error: The semicolon after the while causes an infinite loop and missing a left brace.

b) Error: using a float will cause an error. Use an integer.

c) Error: missing a break statement in the statements for the first case.

d) Error: improper relational operator where it will miss the last operation. Add one more or use <=.

8.4

a) For ( x = 100, x >= 1, x++ )

document.writeln( x ); The loop will continue forever due to the condition being set true always and never ending.

b) The following code should print whether integer walue is odd or even:

switch ( value % 2 ) {

case 0:

documentwriteln( "Even integer" );

case 1:

document.writeln( "Odd integer" );

} There isn't a break statement to prevent the execution of the following lines of code.

c) The following code should output the odd integers from 19 to 1:

for ( x = 19; x >= 1; x += 2 )

document.writeln( x ); The statement starts at 19 and increments by two instead of decrement. It will never end.

d) The following code shoule output the even integers from 2 to 100:

counter = 2;

do {

document.writeln( counter );

counter += 2;

} while ( counter < 100 ); The do..while loop will break at 98 and not print 100 due to the "=" not being present.

News:

Working on Chapter 15