Chapter 6. Making Decisions in Code

If computers could not use data to make decisions, they would be little more than very expensive boat anchors. Because it is hard to write a nontrivial program without some form of decision-making ability in it, I have used some decision-making keywords in previous programs. However, I never really explained what those keywords actually do. That changes by the end of this chapter. In this chapter, you will learn about:

  • Relational operators

  • Comparing value types

  • Comparing reference types

  • The if and else keywords

  • Cascading if statements

  • Logical operators

  • The switch, case, break, and default keywords

  • Data validation

This chapter also presents some thoughts on coding style. Coding style simply refers to the way you write program code. There are myriad coding styles. Because C# is not form-specific, you are free to use just about any coding style you wish. The author has known brilliant programmers whose code is almost impossible to read or decipher. On the other hand, I've had mediocre students who write code that is a joy to read. True, beauty is in the eye of the beholder. However, experience has taught me that 80 percent of a program's development time is spent in testing, debugging, and maintaining the code, and only 20 percent in writing it. For that reason, anything you can do to make your code clearer and more easily understood is a good idea.

Get Beginning C# 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.