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.

174<br />

Using Boolean Operators<br />

The computer examines the first character of each string. If they’re equal, it<br />

continues with the second character, a third, and so on.<br />

In the preceding example, the computer sees that the numbers 97 (which<br />

represent the character a) are equal, so it checks the second character. The<br />

number 65 (A) isn’t greater than the number 97 (a), so this comparison<br />

returns a False value.<br />

What happens if you compare unequal strings, such as<br />

“aA” > “a”<br />

The computer compares each character as numbers as follows:<br />

97 65 > 97<br />

The first numbers of each string (97) are equal, so the computer checks the<br />

second number. Because the second string (a) doesn’t have a second character,<br />

its value is 0. Because 65 > 0, the preceding comparison returns a<br />

True value.<br />

Now look at this comparison:<br />

“Aa” > “a”<br />

The computer translates these characters into their equivalent numbers, as<br />

follows:<br />

65 97 > 97<br />

Comparing the first numbers (characters), the computer sees that 65 > 97, so<br />

this comparison returns a False value. Notice that as soon as the computer<br />

can decide whether one character is greater than another, it doesn’t bother<br />

checking the second character in the first string.<br />

Using Boolean Operators<br />

Comparison operators always return a True or False value, which are<br />

Boolean values. Just as you can manipulate numbers (addition, subtraction,<br />

and so on) and strings (trimming or searching <strong>for</strong> characters), so can you<br />

also manipulate Boolean values.<br />

When you manipulate a Boolean value, you get another Boolean value.<br />

Because there are only two Boolean values (True or False), every Boolean<br />

operator returns a value of either True or False.

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

Saved successfully!

Ooh no, something went wrong!