9/17/2011

BCS2233 18/9/ 2011 Lab OOP LOOPING



Review Questions




1. How many times is the following loop body repeated? What is the printout of the loop?

a)
int i=1;
while(i>10)
if((i++) % 2 ==0)
system.out.println(i);

b)
int i=1;
while(i10)
if((i++) % 2 ==0)
system.out.println(i);


2. What are the differences between a while loop and a do-while loop?

3. Do the following two loops result in the same value in sum?
a)
for( int i = 0; i<10;++i) {
sum += i;
}

b)
for( int i = 0; i<10;i++) {
sum += i;
}

4. What are the three parts of a for loop control? Write a for loop that prints the numbers from 1 to 100.

5.What does the following statement do?
for ( ; ; ) {   do something; }
6. If a variable is declared in the for loop control, can it be used after the loop exits?
7. Can you convert a for loop to a while loop? List the advantages of using for loops.
8. Convert the following for loop statement to a while loop and to a do-while loop:

long sum = 0;
for (int i = 0; i <= 1000; i++)
sum = sum + i;

Programming Exercises
1* (Repeating additions)
Listing 4.1, SubtractionTutorLoop.java, generates ten random subtraction questions.
Revise the program to generate ten random addition questions for two integers between 1 and 15.
Display the number of correct answers and test time.

2* (Counting positive and negative numbers and computing the average of numbers)
Write a program that reads an unspecified number of integers,
determines how many positive and negative values have been read, and computes the total and
average of the input values, not counting zeros. Your program ends with the input 0.
Display the average as a floating-point number. (For example, if you entered 1, 2, and 0, the average should be 1.5.)

No comments:

Cara download Installer windows 10 dalam format ISO

1. Jika anda bercadang untuk download windows 10 melalui website rasmi windows - pilihan untuk download dalam format ISO tidak di berikan.  ...