15.04.2018 Views

programming-for-dummies

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

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

184<br />

Picking Two Choices with the IF-THEN-ELSE Statement<br />

Picking Two Choices with the<br />

IF-THEN-ELSE Statement<br />

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

or it doesn’t. But what if you want the computer to take one action if<br />

something is True and a completely different action if something is False?<br />

In that case, you must use a variation — an IF-THEN-ELSE statement.<br />

The IF-THEN-ELSE statement gives the computer a choice of two mutually<br />

exclusive choices, as shown in Figure 4-2.<br />

Command<br />

True<br />

Comparison<br />

operation<br />

False<br />

Figure 4-2:<br />

An IF-THEN-<br />

ELSE<br />

statement<br />

offers two<br />

different<br />

sets of<br />

commands<br />

to follow.<br />

Command<br />

Command<br />

Command<br />

Like the simple IF-THEN statement, the IF-THEN-ELSE statement can run a<br />

single command or a block of commands, such as<br />

if (True or False) then<br />

{<br />

command #1<br />

command #2<br />

.<br />

.<br />

command #3<br />

}<br />

else<br />

{<br />

command #1

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

Saved successfully!

Ooh no, something went wrong!