logo codesdope

Boolean and more


I hope that you are getting things and practicing well. You can always ask your doubts ( even tiniest one ) to us in the discussion section.

In this chapter, you will learn about booleans.
Like,
1, 2, 3, 4, etc are integers,
1.2, 3.2,332.23, etc are floats,
Boolean is something which can either be true or false.

a = true
b = false
puts a
Output
true
false

Let's do something more. We already know that x = 10 means that x is 10. In the next example, we will use x == 10. == is a comparison operator. It is used for comparison. See one example after which I will explain further.

a = 10
puts a == 10
puts a == 5
Output
true
false

a = 10 : This means that a is 10.
puts a == 10 : In this line, a == 10 will check whether a is 10 or not. If a is 10, then it will be true, else it will be false.
This is the reason why in the third line, i.e. puts a == 5, false got printed (since the value of a is not equal to 5).

a = 2
b = 3
puts a == b
Output
false

Here, the compiler checked whether the value of a is equal to b or not. Since both are not equal, so a == b gave us false.

== in ruby

AND, OR and NOT


In this section, we will learn about three operators- AND, OR and NOT. To understand these, first see this table:

Exp1 Operator Exp2 Boolean
True AND True True
True AND False False
False AND False False
True OR True True
True OR False True
False OR False False

So, if we use AND with any two operands and if both of them are true, then the result will be true. Otherwise, it will be false
If we use OR and if atleast one of the two operands is true, then the result will be true. The result will be false if both the operands are false.

AND can be understood as both ( first and second both )
OR can be understood as either ( first or second any ). See the next line to understand it more clearly.
True OR False -> As OR is used, either of the two is true -> True
True AND False -> As AND is used, both are not true -> False

Try to understand this more or just remember the chart.

In Ruby,
AND is &&
OR is ||
NOT is !.

x = 10
y = 20
puts x == 10 && y == 20
puts x == 3 || y == 20
puts x == 3 && y == 20
puts x == 3 || y == 2
Output
true
true
false
false

Here, the value of x is 10 and y is 20. So, x==10 is true and also y == 20 is true. So, x == 10 && y == 20 is also true as both the operands are true ( && (AND) is used ).

In the next line, x == 3 is false but y == 20 is true. So, x == 3 || y == 20 is true because atleast one operand is true ( || (OR) is used ).

In the next line, x == 3 is false and y == 20 is true. So, x == 3 && y == 2 is false because both operands are not true ( && (AND) is used ). (For the expression in an and to be true, both operands must be true).

In the next line, x == 3 is false and y == 2 is also false. So, x == 3 || y == 2 is false because none of the operands is true ( || (OR) is used ). (For the expression in a or to be true, any operands must be true).

NOT


NOT -> You can understand 'NOT' by thinking that it will do the opposite. This can be understood as described below.
not false is true
not true is false
Yeah! It is simple.
E.g.-
! (true || false) -> false
How?
( true || false ) is true because || (OR) is used and at least one of the operands is true, then ( !(true) ) is false.

x = 10
y = 20
puts !(x == 10 && y == 20)
puts !(x == 3 || y == 20)
Output
false
false

This is the same example as the previous one. We just used ! here and saw the answers got reversed. Earlier those were true but now those are false.

Some operators


First have a look at the following operators:

Operator Description Example
!= Not equal to (5 != 2) is True, (5 != 5) is False
> Greater than (5 > 5) is False
< Less than (5 < 5) is False
>= Greater than or equal to (2 >= 2) is True
<= Less than or equal to (5 <= 2) is false

!= is not equal to operator. It gives true if both the operands are not equal, else it gives false.
> is greater than or equal to operator. It gives true if the first operand is greater than the second operand.
>= is greater than or equal to operator. It gives true if the first operand is greater than or equal to the second operand.
Similarly, < and <= are less than and less than or equal to operators respectively.
You will learn more about operators in the next chapter.

x = 10
y = 20
z = 10
puts x > y
puts x < y
puts x >= z
puts x > z
Output
false
true
true
false
Now go through our practice section of boolean to make your knowledge more concrete.

You practice and you get better. It's very simple.
-Phillip Glass

Share what you know
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.