Chapter 2. Doing Things Conditionally in C#

In This Chapter

Telling true from false with boolean expressions

Keeping your options open with if expressions

Operating logically with (you guessed it) logical operators

Knowing when to pull the (logical) switch

So far in this book, all the programs have run straight through from start to finish, without making any decisions along the way. In this chapter, you discover two C# statements that let you create some variety in your programs. The if statement lets you execute a statement or a block of statements only if some conditional test turns out to be true. And the switch statement lets you execute one of several blocks of statements, depending on the value of variable.

These statements rely heavily on the use of boolean expressions — expressions that yield a simple true or false result. Because you can’t do even the simplest if statement without a boolean expression, this chapter begins by showing you how to code simple boolean expressions that test the value of a variable. Later, after looking at the details of how the if statement works, I revisit boolean expressions and show how to combine them to make complicated logical decisions. Then I get to the switch statement.

Doing Things Conditionally in C#

You’re going to have to put on your thinking cap for much of this chapter — most of it plays with logical puzzles. Find yourself a comfortable chair in a quiet part of the ...

Get ASP.NET 2.0 All-In-One Desk Reference For Dummies® 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.