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.

546<br />

Using Operators<br />

Relational operators compare two values and return a True or False value.<br />

The six relational operators available are shown in Table 2-5.<br />

Table 2-5<br />

Relational Operators<br />

Relational Operator<br />

Purpose<br />

== Equal<br />

!= Not equal<br />

< Less than<br />

Greater than<br />

>= Greater than or equal to<br />

The relational operator in Java/C# is two equal sign symbols (==) whereas<br />

the relational operator in other <strong>programming</strong> languages is just a single equal<br />

sign symbol (=). If you only use a single equal sign to compare two values in<br />

Java/C#, your program will work but not the way it’s supposed to.<br />

Logical operators compare two Boolean values (True or False) and return<br />

a single True or False value, as shown in Table 2-6.<br />

Table 2-6<br />

Logical Operator<br />

&&<br />

Logical Operators<br />

Truth Table<br />

True && True = True<br />

True && False = False<br />

False && True = False<br />

False && False = False<br />

|| True || True = True<br />

True || False = True<br />

False || True = True<br />

False || False = False<br />

! !True = False<br />

!False = True<br />

Increment and decrement operators<br />

Both Java and C# have a special increment (++) and a decrement (--) operator,<br />

which simply adds or subtracts 1 to a variable. Typically, adding 1 to a<br />

variable looks like this:<br />

j = 5;<br />

i = j + 1;

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

Saved successfully!

Ooh no, something went wrong!