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.

196<br />

Making Multiple Choices with the SELECT CASE Statement<br />

SELECT CASE Age<br />

CASE 65<br />

Status = Retired<br />

CASE 21<br />

Status – Working<br />

CASE 15<br />

Status = Student<br />

END SELECT<br />

The preceding SELECT CASE statement doesn’t do anything if the Age variable<br />

is 13, 25, or 81. To make sure the SELECT CASE statement always runs<br />

at least one command, you must add the ELSE statement, such as<br />

SELECT CASE Age<br />

CASE 65<br />

Status = Retired<br />

CASE 21<br />

Status – Working<br />

CASE 15<br />

Status = Student<br />

ELSE<br />

Status = Bum<br />

END SELECT<br />

In this example, if the value of the Age variable is 24 or 5, it doesn’t match<br />

any of the specific values, so the command under the ELSE statement runs<br />

instead (Status = Bum).<br />

Instead of using the ELSE statement, the curly bracket languages use a<br />

default statement, such as<br />

switch (age)<br />

{<br />

case 65: status = retired;<br />

break;<br />

case 21: status = working;<br />

break;<br />

case 15: status – student;<br />

break;<br />

default: status = bum;<br />

}<br />

Both the ELSE and default statements <strong>for</strong>ce the SELECT CASE<br />

(or switch) statement to always do something.<br />

As a general rule, use the IF-THEN statements <strong>for</strong> making the computer<br />

choose one or more commands (or blocks of commands). If you need the

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

Saved successfully!

Ooh no, something went wrong!