Ruby Programming Tutorial - Lesson 04 -Logical Operators

in #ruby7 years ago (edited)

ruby.jpg

Logical Operators

In previous article, I wrote about arithmetic operators.. in this article I will give you a list of logical operators
that you will be using later on to add decision making capabilities in your programs.
Yes .. that is correct your program will be able to make logical decisions and based on those decisions they do certain tasks.

So lets prepare yourself, for how to write a code that makes a decision.

Before we go and dig into logical operations, we need to learn this DataType which is called "Boolean" ..
well you have different Datatypes in any programming language.

Numbers
Strings
Dates
and Booleans

Boolean means, a value that is either True or False.. 0 or 1, you can think of it as a question whose answer is either Yes or No
that's all what a boolean is.

1. OR , || Logical Operator

It is used among two or more variables .. for to check whether one of the values is true ..
if any single value is true among those values, result will be true..

print i_am_cute || i_am_honest

Here is an example

If i have 30 Bitcoins OR 300 Ethers , That means I am rich
You need to think about programs like this, then convert them into logical statement .. which computer can execute.
We will write programs which will use conditional statements later in the series. for now, we only need to understand
what an OR operator does ..

OR will result true, if any single value is True.

e.g if there are 1000 values, which are false, and only 1 value which is true.. the result of the whole statement will be true

AND operator &&

AND operator is used where we want all of our conditions to be true..

I want to marry a girl who is Cute, lovely, faithful and that has ability to program ruby

In this statement the girl will be rejected if it does not meet all the conditions. :p

This is a perfect example of an AND operational statement. Which if we want to convert into a computer program
we need to use AND operator.

lets take another example.

I will give this guy a vote up if his posts are original, well written and full of knowledge otherwise I won't

Here is a programming example.

NOT ! operator ..

You met people who always do opposite to what to say to them ?? well they are programmed with Not operator.. :D
It result exactly opposite to what its feeded into it.

Lets check to see, if I ask my program to tell me, whether I am cute or not..

Program tells me that I am not :p thats because of the NOT operator.

In this article , we learned about logical operators,
AND (&&) , OR (||), NOT (!)

we can use them in decision making, or conditional statements in our programs.
Post few example statements for all of these operators in comments :) most funny comment gets a vote up from me.

GV-congrats_9d4ce745-f823-4038-9eb3-eefbdc07a2d0.jpg

Now you are a little more logical ..

Sort:  


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

Very good post.I'm also going to start posting my writings on Java and other languages shortly.

good post. Read well.

Logically correct