Chapter 7

7.1

a) All programs can be written in terms of three types of control structures: sequence, selection and repetition.

b) The if...else double-selection statment is used to execute one action when a condition is true and another is action when that condition is false.

c) Repeating a set of instriuction a specific number of times is called counter controlled repetition.

d) When it is not known in advance how many times a set of statements will be repeated, a sentinel, signal flag or dummy value can be used to teminate the repetition.

7.2

Write four javascript statements that each add 1 to variable x, which contains a number.

x = x + 1

x += 1;

++x;

x++;

7.3

Write a javascript statment to accomplish each of the following tasks:

a) Assign the sum of x and y to z, and increment the value of x by 1 after the calculation. Use only one statment.

z = x++ + y;

b) Test whether the value of the variable count is greater than 10. If it is, then print "Count is greater than ten.".

if ( count > 10 )

document.writeln( "Count is greater than ten." );

c) Decrement the variable x by 1, then subtract it from the variable total. Use only one statement.

total -= --x;

d) Calculate the remainder after q is divided by divisor, and assign the result to q. Write this statment in two different ways.

q %= divisor;

q = q % divisor;

7.4

Write javascript statements to accomplish each of the following tasks:

a) Declare variables sum and x.

var sum, x;

b) Assign 1 to variable x.

x = 1;

c) Assign 0 to variable sum.

sum = 0;

d) Add variable x to variable sum, and assign the result to variable sum.

sum += x; or sum = sum + x;

e) Print "The sum is: ", followed by the value of variable sum.

document.writeln( "The sum is: " + sum );

 

7.5

7.6

Determine the value of each variable after the calculation is performed. Assume that, when each statement begins executing, all variables have the interger value 5.

a) product *= x++;

product = 25, x = 6;

b) quotient /= ++x;

quotient = 0.833333..., x = 6;

7.7

Identify and correct the errors in each of the following segments of code:

a) while ( c <= 5 ) {

product *= c;

++c;

Missing a closing brace for while.

b) if ( gender == 1 )

document.writeln ( "Woman" );

else;

document.writeln( "Man " );

7.8

What is wrong with the following whil repetition statement?

while ( z >= 0 )

sume += z;

The value z is never changed in the body of the loop thus creating an infinite loop.

Semicolon after "else" causes statement to always execute.

7.9 Identify and correct the errors in each of the follwong segments of code:

a) if ( age >= 65 ); There is a semi colon at the end of the "if" statement.

      document.writeln( "Age greater then or equal to 65" );

else

      document.writeln( "Age is less than 65" );

b) var x = 1, total; "total" is never given a value to start with and produces a "NAN" error.

while ( x <= 10 )

{

     total += x;

     ++x;

}

c) var x = 1; the while loop is only running the first line underneath it instead of the preincrement x variable addition function that would eventually end the loop.

     var total = 0;

While ( x <= 100 )

     total += x;

     ++x;

d) var y = 5;There is no closing brace for the while loop.

while ( y > 0 )

{

     document.writeln( y );

     ++y;

7.10 What does the following program print?

 

1

4

9

16

25

32

48

64

81

100

Total is 100

7.11

To calculate an average of miles per gallon for a vehicle over the history of miles/gallons factor.

1. Input number of full tanks to average.

2. Input the gallons of fuel filled for number x

3. Input the miles driven for number x

4. Repeat entry of gallons and miles until the number of tanks is entered.

5. Print the average miles per gallon for each tankful and print an average at the bottom.

 

7.12

To calculate whether a customer has exceeded the credit limit of an account.

1. Obtain account number, balance previous, total charges, total payments/credits, and credit limit.

2. calculate current debt balance

3. compare that the debt is < credit limit, if so print leftover credit, if not print credit exceeded

 

7.13

To compute commission and total payment of sales employees.

1. Ask for sale item amount.

2. Repeat until data is done being entered.

3. Calulate the percentage of commission based on total sales.

4. Add base pay.

5. Print items and total amount.

 

7.14

Gross pay calculator with overtime compensation.

1. Take in employee name.

2. Take in employee rate pay.

3. Take in employee hours worked that week.

4. If hours over 40, then the hours minus 40 should have a rate of time and a half added to the total 40 * rate.

5. Print results.

6. Repeat until all employees are added.

News:

Working on Chapter 15