官术网_书友最值得收藏!

Practice and explore

Test your knowledge and understanding by answering some questions, get some hands-on practice, and explore with deeper research into this chapter's topics.

Exercise 3.1 – test your knowledge

Answer the following questions:

  1. What happens when you pide an int variable by 0?
  2. What happens when you pide a double variable by 0?
  3. What happens when you overflow an int variable, that is, set it to a value beyond its range?
  4. What is the difference between x = y++; and x = ++y;?
  5. What is the difference between break, continue, and return when used inside a loop statement?
  6. What are the three parts of a for statement and which of them are required?
  7. What is the difference between the = and == operators?

Exercise 3.2 – explore loops and overflow

What will happen if this code executes?

int max = 500;
for (byte i = 0; i < max; i++)
{
    WriteLine(i);
}

Add a new Console Application named Ch03_Exercise02 and enter the preceding code. Run the application by pressing Ctrl + F5. What happens?

What code could you add (don't change any of the preceding code) to warn us about the problem?

Exercise 3.3 – practice loops and operators

FizzBuzz is a group word game for children to teach them about pision. Players take turns to count incrementally, replacing any number pisible by three with the word "fizz", any number pisible by five with the word "buzz", and any number pisible by both with "fizzbuzz".

Some interviewers give applicants simple FizzBuzz-style problems to solve during interviews. Most good programmers should be able to write out on paper or whiteboard a program to output a simulated FizzBuzz game in under a couple of minutes.

Want to know something worrisome? Many computer science graduates can't. You can even find senior programmers who take more than 10-15 minutes to write a solution.

This quote is taken from http://blog.codinghorror.com/why-cant-programmers-program/.

Refer to the following link for more information:

http://imranontech.com/2007/01/24/using-fizzbuzz-to-find-developers-who-grok-coding/

Create a Console Application named Ch03_Exercise03 that outputs a simulated FizzBuzz game counting up to 100. The output should look something like this:

1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, 16, 17, Fizz, 19, Buzz, Fizz, 22, 23, Fizz, Buzz, 26, Fizz, 28, 29, FizzBuzz, 31, 32, Fizz, 34, Buzz, Fizz, 37, 38, Fizz, Buzz, 41, Fizz, 43, 44, FizzBuzz, 46, 47, Fizz, 49, Buzz, Fizz, 52, 53, Fizz, Buzz, 56, Fizz, 58, 59, FizzBuzz, 61, 62, Fizz, 64, Buzz, Fizz, 67, 68, Fizz, Buzz, 71, Fizz, 73, 74, FizzBuzz, 76, 77, Fizz, 79, Buzz, Fizz, 82, 83, Fizz, Buzz, 86, Fizz, 88, 89, FizzBuzz, 91, 92, Fizz, 94, Buzz, Fizz, 97, 98, Fizz, Buzz

Exercise 3.4 – practice exception handling

Create a Console Application named Ch03_Exercise04 that asks the user for two numbers in the range 0-255 and then pides the first number by the second:

Enter a number between 1 and 255: 100
Enter another number between 1 and 255: 8
100 pided by 8 is 12

Write exception handlers to catch any thrown errors:

Enter a number between 1 and 255: apples
Enter another number between 1 and 255: bananas
FormatException: Input string was not in a correct format.

Exercise 3.5 – explore topics

Use the following links to read in more detail about the topics covered in this chapter:

主站蜘蛛池模板: 泗阳县| 乌海市| 西宁市| 屯门区| 二连浩特市| 东乌珠穆沁旗| 泾川县| 曲周县| 肇东市| 资源县| 绿春县| 太白县| 康马县| 安岳县| 石首市| 德州市| 南阳市| 北宁市| 中西区| 龙陵县| 称多县| 乐昌市| 邵阳市| 沅江市| 绥棱县| 手机| 璧山县| 栾川县| 栖霞市| 岢岚县| 榕江县| 绥芬河市| 原平市| 新绛县| 阜阳市| 石泉县| 南汇区| 庆云县| 礼泉县| 峨边| 淄博市|