15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 4: Making Decisions<br />

by Branching<br />

In This Chapter<br />

Using the IF-THEN, IF-THEN-ELSE, and IF-THEN-ELSEIF statements<br />

Using multiple Boolean operators<br />

Using the SELECT CASE statement<br />

The simplest program lists commands one after another in a sequence,<br />

much like following the steps of a recipe. Follow a recipe step by step<br />

and you always create the same dish. If a program lists commands step by<br />

step, the computer always produces the same result.<br />

In some cases, you may want a program to do the exact same thing over and<br />

over again, such as a simple program to display traffic signals. However <strong>for</strong><br />

most programs, you want the computer to react to outside data. To make a<br />

computer respond in different ways, a program needs to offer two or more<br />

choices <strong>for</strong> the computer to follow.<br />

When you quit a program, the program may ask, “Do you really want to<br />

exit?” At this point, the program is giving the computer a choice of two possible<br />

actions to take based on your answer.<br />

If you answer Yes, the computer quits the program. If you answer No, the<br />

computer keeps running the program.<br />

When a program gives the computer a choice of two or more commands to<br />

follow, that’s called a branching or decision statement.<br />

All branching statements work the same way:<br />

✦ A comparison operator (or a conditional expression) compares an<br />

expression (such as A > 45) to determine a True or False value.<br />

✦ The branching statement offers at least two groups of commands <strong>for</strong><br />

the computer to follow based on whether its comparison is True or<br />

False.

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

Saved successfully!

Ooh no, something went wrong!