19.04.2017 Views

Learn to Program with Small Basic

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

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

<strong>Small</strong> <strong>Basic</strong> au<strong>to</strong>matically indents<br />

If statements as you type the code. This<br />

makes the program easier <strong>to</strong> read and<br />

clearly shows when statements are part<br />

of code blocks. If your code ever gets unindented,<br />

right-click in the Edi<strong>to</strong>r and<br />

select Format <strong>Program</strong> from the pop-up<br />

menu <strong>to</strong> indent all your code. Awesome!<br />

The If statement is the basis of all<br />

decision making in <strong>Small</strong> <strong>Basic</strong>. Check<br />

out the illustration in Figure 8-1 <strong>to</strong><br />

understand how it works.<br />

The condition of an If statement is<br />

a logical expression (also called a Boolean<br />

expression or a conditional expression) that’s<br />

either true or false. If the condition is<br />

Is<br />

condition<br />

true?<br />

true, the program runs the statements between the If and EndIf keywords<br />

(which is called the body of the If statement). But if the condition is false,<br />

the statements in the block are skipped. The program runs the statement<br />

after the EndIf keyword whether the condition is true or not.<br />

No<br />

Statement<br />

after EndIf<br />

Yes<br />

Execute<br />

statement(s)<br />

between the<br />

If and EndIf<br />

keywords<br />

Figure 8-1: The flowchart of the<br />

If/Then/EndIf block<br />

Tip<br />

You can think of an If statement as a de<strong>to</strong>ur in the flow of a program. It’s like an<br />

optional roller coaster loop.<br />

Booleans in the Real World<br />

The word Boolean is used in honor of George Boole, a 19th-century British<br />

mathematician who invented a system of logic based on just two values: 1 and<br />

0 (or true and false). Boolean algebra eventually became the basis for modernday<br />

computer science.<br />

In real life, we use Boolean expressions all the time <strong>to</strong> make decisions.<br />

Computers also use them <strong>to</strong> determine which branch of a program <strong>to</strong> follow. A<br />

remote server may grant or deny access when you swipe your credit card at a<br />

department s<strong>to</strong>re based on whether your card was valid (true) or invalid (false).<br />

A computer in a vehicle will au<strong>to</strong>matically deploy the airbags when it decides<br />

that a collision has occurred (collision = true). Your cell phone may display<br />

a warning icon when the battery is low (batteryLow = true) and remove the<br />

icon when the battery’s charge is acceptable (batteryLow = false).<br />

These are just few examples of how computers cause different actions <strong>to</strong><br />

be taken by checking the results of Boolean conditions.<br />

Making Decisions <strong>with</strong> If Statements 99

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

Saved successfully!

Ooh no, something went wrong!