Skip to main content

Ruby Programming Tutorial - Lesson 04 -Logical Operators - bearshares.com



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 ..



Originally posted on my blog at bearshares.com
https://bearshares.com/programming/@ruby/ruby-programming-tutorial-lesson-04-logical-operators

Comments

Popular posts from this blog

Ruby Programming Tutorial - Lesson 02 - Constant values and Data types - bearshares.com

If you followed my previous lecture, you should be ready to write some ruby programs now :) Before we start .. Download and install your favorite text editor software. in my Case i am using Visual studio code. print "Hello World" Lets write our first ruby program now, creating a new file, and saving it with .rb extension will create a ruby program for us. its time to execute the code, to see if it works we can execute a ruby program doing this. ruby variable.rb Constants Now lets talk about constant values. every number like, 1, 2, 3, 1.34, 1.09, 0.0000034, or -1.98 is a constant value. it is called constant because it never changes.. like 2 is a constant value which always remains to be 2 :) Similarly each alphabet characters are also constant values like "a", "b" , "c" these are all constant values.. lets type some constant values. and see what we get when we execute our program Variables Variables are value holders

Ruby Programming Tutorial - Lesson 01 - Installation of Ruby Environment - bearshares.com

Hey Friends, This is the first part of ruby programming Lessons. In this part, we are going to learn, setting up the Ruby environment for development. I suggest doing it on linux operating system .. Ubuntu 16.04 is suggested .. If you are on windows, there is an easier method of installing ruby, that is via rubyinstaller. You can find it here :) https://rubyinstaller.org/downloads/ otherwise if you are on linux operating system, follow this guide, I am sure we are going to have so much fun. Let us Begin :) Execute this command in your terminal. we are installing ruby via rbenv .. so first lets get it git clone https://github.com/rbenv/rbenv.git ~/.rbenv After cloning rbenv execute these two commands. echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc echo 'eval "$(rbenv init -)"' >> ~/.bashrc Next step is sourcing rbenv, that you can do by typing following command source ~/.bashrc after sourcing it, you can che