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.

186<br />

Picking Three or More Choices with the IF-THEN-ELSEIF Statement<br />

Checking a condition <strong>for</strong> each set of commands<br />

The IF-THEN-ELSEIF statement only runs a command (or block of commands)<br />

if some condition is True, as shown in Figure 4-3.<br />

Command<br />

True<br />

Comparison<br />

operation<br />

False<br />

Command<br />

True<br />

Comparison<br />

operation<br />

False<br />

Figure 4-3:<br />

An IF-THEN-<br />

ELSEIF<br />

statement<br />

offers two<br />

different<br />

sets of<br />

commands<br />

to follow.<br />

Command<br />

Command<br />

If every conditional expression is False, the IF-THEN-ELSE statement<br />

doesn’t run any commands. Only if one of its conditional expressions is True<br />

does the IF-THEN-ELSE statement run exactly one set of commands, such as<br />

IF (Salary > 100000) THEN<br />

TaxRate = 0.45<br />

ELSEIF (Salary > 50000) THEN<br />

TaxRate = 0.30<br />

END IF<br />

In this example, the computer has three possible choices:<br />

✦ If Salary > 100000, set TaxRate = 0.45.<br />

✦ If Salary > 50000 (but less than 100000), set TaxRate = 0.30.<br />

✦ If Salary

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

Saved successfully!

Ooh no, something went wrong!