25.12.2015 Views

Professional

1l6xhbR

1l6xhbR

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 4<br />

Using decision statements<br />

After completing this chapter, you will be able to:<br />

■<br />

■<br />

■<br />

■<br />

Declare Boolean variables.<br />

Use Boolean operators to create expressions whose outcome is either true or false.<br />

Write if statements to make decisions based on the result of a Boolean expression.<br />

Write switch statements to make more complex decisions.<br />

Chapter 3, “Writing methods and applying scope,” shows how to group related statements into<br />

methods. It also demonstrates how to use parameters to pass information to a method and how to<br />

use return statements to pass information out of a method. Dividing a program into a set of discrete<br />

methods, each designed to perform a specific task or calculation, is a necessary design strategy. Many<br />

programs need to solve large and complex problems. Breaking up a program into methods helps you<br />

to understand these problems and focus on how to solve them, one piece at a time.<br />

The methods in Chapter 3 are very straightforward, with each statement executing sequentially<br />

after the previous statement completes. However, to solve many real-world problems, you also need<br />

to be able to write code that selectively performs different actions and that takes different paths<br />

through a method depending on the circumstances. In this chapter, you’ll learn how to accomplish<br />

this task.<br />

Declaring Boolean variables<br />

In the world of C# programming (unlike in the real world), everything is black or white, right or<br />

wrong, true or false. For example, if you create an integer variable called x, assign the value 99 to it,<br />

and then ask whether x contains the value 99, the answer is definitely true. If you ask if x is less than<br />

10, the answer is definitely false. These are examples of Boolean expressions. A Boolean expression<br />

always evaluates to true or false.<br />

87

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!