Review Questions



Which general type of statement would you use to implement each of the following logical descriptions:

  1. If number is greater than 100, write "greater than 100" onscreen; otherwise, write "less than or equal to 100."

  2. Repeatedly check each letter of a string until no more letters are left in the string.


Suppose that your program contains three variables called rainfall, wind, and temperature, all of type double. For each of the following points, write a few lines of code that implement the described logic (you don't need to write the whole program):

  1. If rainfall is greater than 100, write "Heavy rainfall" onscreen.

  2. If rainfall is greater than 100 or wind is greater than 120, write "Bad weather" onscreen.

  3. If rainfall is equal to 0 and wind is less than 10 and temperature is between 23 and 27 (Celsius), write "Nice weather."

  4. If rainfall is equal to 0, write "It is not raining"; otherwise, write "It is raining."


No matter which value rainfall has, the following lines will always write "It's raining" onscreen. Why?

 if(rainfall > 10);     Console.WriteLine("It's raining"); 

What is the purpose of nested if statements?


Write logical expressions (using C#'s comparison operators and logical operators) that represent the following conditions:

  1. rainfall is between 100 and 150.

  2. number is odd and not equal to 23.

  3. number is even and smaller than 100, or weight is less than 100.


A method with the header bool IsEven(int number) returns true if the argument passed to its parameter number is even; otherwise, it returns false. Write a Boolean expression containing a call to IsEven that is true if the number passed along with the call is odd.


To what does the term scope refer?


What is one of the few correct uses of the goto statement?


Rewrite the following if-else multibranch statement using a switch statement.

 if(timeOfDay == "morning")     Console.WriteLine("Good Morning"); else if(timeOfDay == "midday")     Console.WriteLine("Good Day"); else if(timeOfDay == "evening")     Console.WriteLine("Good evening"); else     Console.WriteLine("Invalid time"); 

To what does "falling through" in relation to the switch statement refer? How can "falling through" be prevented?


Your program contains variables called cost1, cost2, and minimumCost. Write a statement involving the conditional operator that assigns the smaller of the values in cost1 and cost2 to minimumCost.


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

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: