Ruby Programming Tutorial - Lesson 05 - Comparison operators :) lets compare values

in ruby •  7 years ago  (edited)

ruby.jpg

lets recap a little bit, what have we learnt so far?

  1. Different Datatypes, Numbers, texts, booleans
  2. Holding values of different datatypes inside of variables.
  3. Arithmetic operations on those values,
  4. Logical operators ..

Its time to learn some Comparison operators.
Comparison operators allow us to compare two values, using them our programs can tell, whether two values are equal, not equal. Whether one of the values is greater than or less than the other.
Based upon those comparisons, our programs will then be able to make decisions and do certain tasks which we want them to do..
few examples where comparisons can be used :)

1. if user has account balance of 20,000 SP send him a message to donate something to me :p
2. scan all the users on steemit, check to see who is cute and find someone who is more cute than me :p
3. scan all the girls on steemit, check to see who is single and looking for a boy

you can do all the wonders, if you can make your programs do comparisons between values.
So lets learn some comparison operators :)

1. Greater Than comparison operator (>)

This operator is used to check, if first value is greater than other ..

result = 10 > 5

as you can see, we made a comparison between two numbers and we wanted to see the result
.. we got true.. 10 is indeed greater than 5 :) ruby WORKS :)
so that means our comparison operator works just fine.. lets do some fun comparisons

2. Less than Operator (<)

This operator returns true, if first value is less than the second value..
5 < 10
will return true .. and
10 < 10 will return false.

lets make a fun example to learn this operator :)

Here are some more operators you can try..

Greater Than OR equal to (>=)

10 >= 10 will result true , because 10 equals 10,
9 >= 10 will result false, because 9 does not equals 10 and also it is not greater than 10
20 >= 10 will result true, because 20 is greater than 10 ...

Less Than or equal to (<=)

Its works the same way, as its brother operator does..
10 <= 1000 will result true, because 10 is less than 1000
etc

Not Equal to (!=)

10 != 9 will result true, because 10 is not equal to 9
9!=9 will result false, you know why :)

Equal to (==)

bilal = "cute"
bilal == "cute" will result in true , because the variable bilal has a value of "cute" stored inside of it.. 

these are all comparison operators which every programming language has.. and these operators are used in every single program. that does comparisons between values.

Here is a quick little example which demonstrates their uses.. you are free to try every single of them..
make lots of comparisons between values. and store the resulting value in a variable and display on your screen
check to see if you get correct results.

Now that we are learned to make comparisons between values, Next step is to make Conditional statements, that do logical comparisons based upon arithmetic operations :)

If you understood what we are going to do ? .. I would like to congratulate you,
because you are now a little programmer :)

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  


Congratulations!

This post has been voted Good! Upvoted and resteemed on Salt Water.

Click @alphawhale to have your best post upvoted and resteem free.

Read some of our quality resteemed here
&
Click here to read about @alphawhale

ey great pos and god information

vote mee

Please upvote my post

Nice, dude!