Review Questions

   


1:

What is the major difference between the while and the do-while statements? Why is the while statement most often the preferred type of statement?

2:

What is the output from the following piece of source code? (Mentally trace the code.)

 int counter = 0; while(counter < 4) {     counter++;     Console.Write(counter + " "); } 
3:

What is the output from the following do-while construct? (Trace the code.)

 int counter = 4; do {     Console.Write(counter + " ");     counter--; } while(counter > 0); Console.WriteLine("Value of counter: {0} ", counter); 
4:

What is the output from the following for loop construct? (Trace the code.)

 for(int i = 0; i < 0; i += 2) {     Console.Write(i + " "); } 
5:

Write a for loop that generates the following output:

 12  9  6  3  0  -3 
6:

Is the following for loop valid (disregarding that it is not shown to be part of a complete program). If so, what output will it generate?

 for(int i = 1, j = 1; i < 5; i++, j += 2) {     Console.Write("  {0} ", i * j); } 
7:

What is the output from the following nested pair of for loops? (Trace the source code.)

 for(int i = 1; i < 4; i++) {     for(int j = 1; j < 5; j++)     {         Console.Write(" {0} ", i + j);     } } 
8:

Consider the following for loop:

 for(int counter = 1;  ; counter++) {     Console.WriteLine("The value of counter: {0} ", counter);     Console.WriteLine("This value of counter * 10: {0} , counter * 10); } 

Syntactically, this for loop is valid. However, it is an infinite loop as it stands now, because it lacks a loop condition. Change the for loop so that at its fourth repeated loop it terminates between the two WriteLine statements. Thus, at its fourth repeated loop, it should only write

 The value of counter: 4 

and then terminate. Hint: You can use the break statement.

9:

Consider the following for loop shown previously in Listing 9.8:

 for(int i = 0; i <= 20; i++) {     if(i % 4 == 0)         continue;     Console.Write(i + ", "); } 

Rewrite the for loop and eliminate the continue statement while keeping the semantics of the for loop intact.

10:

Name one advantage of pseudocode over flowcharts in relation to constructing structured programs.


   


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net