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.

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

CASE 15<br />

Status = Student<br />

END SELECT<br />

The curly bracket languages, like C and C++, don’t let you check <strong>for</strong> a range<br />

of values in a switch statement.<br />

Comparing values<br />

Listing a range of values can be useful, but what if there’s no upper (or<br />

lower) limit? For example, anyone over the age of 65 might be considered<br />

retired, so you need to use a comparison operator to check a variable with a<br />

value, such as Age >= 65.<br />

To use a comparison operator in a SELECT CASE statement, languages such<br />

as BASIC use the following syntax:<br />

SELECT CASE Age<br />

CASE IS >= 65<br />

Status = Retired<br />

CASE 21 TO 64<br />

Status – Working<br />

CASE 15<br />

Status = Student<br />

END SELECT<br />

Book II<br />

Chapter 4<br />

Making Decisions<br />

by Branching<br />

In this example, the first part of the SELECT CASE statement tells the<br />

computer to check if the value in the Age variable is (note the IS keyword)<br />

>= 65.<br />

The second part of the SELECT CASE statement checks if the Age variable<br />

falls within the range of 21 to 64.<br />

The third part of the SELECT CASE statement checks if the Age variable is<br />

exactly equal to 15.<br />

As you can see, each part of a SELECT CASE statement can check a value<br />

by matching it exactly, checking a range of values, or using a comparison<br />

operator.<br />

The curly bracket languages, like C and C++, don’t let you use comparison<br />

operators in a switch statement.<br />

Running at least one command<br />

with the ELSE statement<br />

It’s possible <strong>for</strong> a SELECT CASE statement to run zero commands if the<br />

CASE statement can’t match a variable to any specific value, such as

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

Saved successfully!

Ooh no, something went wrong!