Using comparison operators

You can compare one value to another using comparison operators, and the result will be true or false. You can use the following comparison operators:

Operators Description
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

 

Let's see how these operators are used. Enter the following code and run it:

//comparison operators
1 == 1 // equal to, true because 1 is equal to 1
2 != 1 // not equal to, true because 2 is not equal to 1
2 > 1 // greater than, true because 2 is greater than 1
1 < 2 // less than, true because 1 is less than 2
1 >= 1 // greater or equal to, true because 1 is greater than or equal to 1
2 <= 1 // less or equal to, false because 2 is not less than or equal to 1

All the statements except for the last one will return true. The last statement will return false. The returned Boolean values will be displayed in the Results area.

What happens if you want to check more than one condition? That's where logical operators come in. You'll study those in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.10.162