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.

512<br />

Using Operators<br />

Table 5-1<br />

Mathematical Operators<br />

Mathematical Operator Purpose Example<br />

+ Addition 5 + 3.4<br />

- Subtraction 203.9 – 9.12<br />

* Multiplication 39 * 146.7<br />

/ Division 45 / 8.41<br />

% Modula division 35 % 9 = 8<br />

(returns the remainder)<br />

** Exponentiation 2**3 = 8<br />

If you divide two integers with the / operator, the answer will be rounded to<br />

the nearest integer. If you want to return a decimal value, at least one of the<br />

numbers must be written as a decimal value such as 2.0 / 3 or 2 / 3.0. If<br />

you just type 2 / 3, the answer will be 0.<br />

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

value. The seven comparison operators available are shown in Table 5-2.<br />

Table 5-2<br />

Relational Operators<br />

Relational Operator<br />

Purpose<br />

== Equal<br />

=== Identical<br />

!= Not equal<br />

< Less than<br />

Greater than<br />

>= Greater than or equal to<br />

Ruby uses three equal signs (===) to compare to values and determine if they’re<br />

of the same data type. For example, Ruby treats 1.0 and 1 as identical because<br />

both are numbers, but 1.0 and “1.0” wouldn’t be considered equal<br />

because one is a number and the other is a different data type (a string).<br />

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

a single True or False value, as shown in Table 5-3.

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

Saved successfully!

Ooh no, something went wrong!