Data Comparisons

One of the most basic capabilities of any programming language is the ability to make comparisons. There are special symbols used to compare data in programming. The result of every comparison is either true or false. Ex 5 is greater than 7 is false, but 7 is less than 9 is true.


Let us look at a simple example where we compare two people’s account balance and determine who has more money. Mark has $56 in his account and Angela has $73.
(int mark_balance = 56; int angela_balance = 73;)

==

If we want to determine whether Mark & Angela have the same amount of money in their account, we will use the == symbol to check for equality.

Ex. mark_balance == angela_balance

This will return false because mark_balance is 56 and angela_balance is 73, since 56 does not equal 73.

== Means check to see if the values are the same.

 

!=

!= means not equal, this will check for non-equality. Therefore if since Mark and Angela have different balances mark_balance != angela_balance will be true. != means check to see if the values are not the same.

<

>

Now suppose we want to check whether mark_balance is less than angela_balance, we will use the < symbol to check if the first data is less than the second data.

Ex. mark_balance < angela_balance

This will return true because mark_balance (56) is greater than angela_balance (73).

< means check to see if the first value is less than the second

 

> means check to see if the first value is greater than the second

 

<= 

>=

If we want to check whether Angela is a millionaire, we must check if angela_balance is greater than or equal to 1,000,000. To do that we use the >= symbol.

Ex angela_balance >= 1000000

This would return false since Angela is not a millionaire.

<= means check to see if the first value is less than or equal to the second value.

 

>= means check to see if the first value is greater than or equal to the second value.

&&

&& in programming represents ‘and’.

Suppose we want to check if Mark’s balance is between $50 and $100

Ex mark_balance > 50 && mark_balance < 100

This would determine if his balance is greater than 50 AND less than 100

&& (AND) means both values are true

||

|| represents ‘or’. In another example Ex mark_balance == 50 || angela_balance != 4

This would return true only if mark_balance equals 50 OR angela_balance does not equal 4

|| (OR) means one or more of the values are true

 

 

* The double sign (==) should not be confused with the single equal sign (=). The both have very different purposes.
The single equal sign is used to assign value to a variable, the double equal sign is used to determine whether two values are equal.
* To compare Strings a different method must be used.


Determine whether the result of each comparison is true or false

int numA = 5, numB = 7, numC = 3;

numA < numB
32 >= 24
numC != numA-2
numB > numA && numB < numA
numB > numA || numB < numA
numC == 3 && numB > numA && numB > 3+2
Back to Course Menu
What do you think about this topic? Share your suggestions / comments