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.

172<br />

Using Comparison Operators<br />

Using Comparison Operators<br />

Unlike math and string operators that can change data, comparison operators<br />

compare two chunks of data to determine which one is bigger than the<br />

other. Table 3-7 lists common comparison operators. When comparison<br />

operators compare two items, the comparison operator returns one of two<br />

values: True or False.<br />

A single comparison operation is also called a conditional expression.<br />

The values True and False are known as Boolean values or Boolean arithmetic.<br />

(The mathematician who invented Boolean arithmetic is named<br />

George Boole.) Computers are essentially built on Boolean arithmetic<br />

because you program them by flipping switches either on (True) or off<br />

(False). All <strong>programming</strong> ultimately boils down to a series of on-off commands,<br />

which is why machine language consists of nothing but 0’s and 1’s.<br />

Table 3-7<br />

Common Comparison Operators<br />

Comparison Operator What It Means Example Result<br />

= or == Equal 45 = 37 False<br />

A = A<br />

True<br />

< Less than 563 < 904 True<br />

a”< A<br />

False<br />

Greater than 51 > 4 True<br />

A > a<br />

False<br />

>= Greater than or 76 >= 76 True<br />

equal to a > – z False<br />

< > or != Not equal to 46 < > 9 True<br />

a < > a<br />

False<br />

Many curly bracket languages, such as C, use != as their not equal comparison<br />

operator instead of < >.<br />

Curly bracket languages, such as C and C++, use the double equal sign (==)<br />

as the equal comparison operator whereas other languages just use the<br />

single equal sign (=). If you use a single equal sign in C/C++, you’ll assign a<br />

value rather than compare two values. In other words, your C/C++ program<br />

will work, but it won’t work correctly.<br />

Knowing whether two values are equal, greater than, less than, or not equal<br />

to one another is useful to make your program make decisions, which you<br />

read about in Chapter 4 of this mini-book.

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

Saved successfully!

Ooh no, something went wrong!