spring-5-with-spring-boot-2 / 02 - Core Java /014 Logical Operators_en.srt
Tan115's picture
Add files using upload-large-folder tool
78b4844 verified
Raw
History Blame Contribute Delete
19.6 kB
1
00:00:00,000 --> 00:00:02,000
(calm music)
2
00:00:03,000 --> 00:00:06,000
-: So we are done with the arithmetic operators
3
00:00:06,000 --> 00:00:07,000
and relational operators, right?
4
00:00:07,000 --> 00:00:08,000
Then we had one question.
5
00:00:08,000 --> 00:00:10,000
What if you have two relational operators
6
00:00:10,000 --> 00:00:12,000
and which will result in true or false?
7
00:00:12,000 --> 00:00:14,000
Right? And then you want to combine them.
8
00:00:14,000 --> 00:00:17,000
So you want 1 true or false out of 2
9
00:00:17,000 --> 00:00:19,000
or 3 true or false?
10
00:00:19,000 --> 00:00:21,000
So what I'm saying is let's say I have two variables here,
11
00:00:21,000 --> 00:00:24,000
which is x, y and also we have one more commission,
12
00:00:24,000 --> 00:00:25,000
which is a and b.
13
00:00:25,000 --> 00:00:29,000
And then I want to check if, I mean I want x
14
00:00:29,000 --> 00:00:30,000
to be less than y
15
00:00:30,000 --> 00:00:34,000
and I want a to be greater than b, this might result
16
00:00:34,000 --> 00:00:38,000
into a true and false or it might result in true and false.
17
00:00:38,000 --> 00:00:42,000
What I want is the output of I want is I want output
18
00:00:42,000 --> 00:00:46,000
of these two combined can be either true or false,
19
00:00:46,000 --> 00:00:50,000
but I want the combined output irrespective if they are true
20
00:00:50,000 --> 00:00:51,000
or false individually.
21
00:00:51,000 --> 00:00:53,000
So how do I combine this?
22
00:00:53,000 --> 00:00:55,000
Now to combine these two different conditions,
23
00:00:55,000 --> 00:00:59,000
we can use a logical operator in between, example,
24
00:00:59,000 --> 00:01:03,000
this logical operator can be and or I mean,
25
00:01:03,000 --> 00:01:05,000
and the second option is or.
26
00:01:05,000 --> 00:01:06,000
So it can be and and or.
27
00:01:06,000 --> 00:01:08,000
Now what exactly it means?
28
00:01:08,000 --> 00:01:12,000
See, if you want that your output to be true.
29
00:01:12,000 --> 00:01:15,000
So let's say if you want your output to be true.
30
00:01:15,000 --> 00:01:19,000
So you can use, if you can say, hey, you know I want both
31
00:01:19,000 --> 00:01:21,000
of these to be true, then only I want true.
32
00:01:21,000 --> 00:01:23,000
Or you can say if any
33
00:01:23,000 --> 00:01:26,000
of this can be true, I want it to be true.
34
00:01:26,000 --> 00:01:30,000
Now this is very important in terms of computation.
35
00:01:30,000 --> 00:01:32,000
So when you say your computer is smart, it's
36
00:01:32,000 --> 00:01:33,000
because of a computer can decide
37
00:01:33,000 --> 00:01:35,000
based on multiple conditions.
38
00:01:35,000 --> 00:01:39,000
So to do that we use something called a logical operators.
39
00:01:39,000 --> 00:01:41,000
So what we do is again,
40
00:01:41,000 --> 00:01:42,000
this concept is coming from mathematics
41
00:01:42,000 --> 00:01:46,000
is not a programming concept, it's coming from mathematics
42
00:01:46,000 --> 00:01:49,000
where you can use something called a and operator.
43
00:01:49,000 --> 00:01:51,000
We can use or operator
44
00:01:51,000 --> 00:01:55,000
or we can use a not operator, which is with exclamation.
45
00:01:55,000 --> 00:01:58,000
So the and operator in programming, we represent that
46
00:01:58,000 --> 00:02:01,000
with the help of AND and the OR operator.
47
00:02:01,000 --> 00:02:05,000
So we use ampersand for and and we use a pipe for OR
48
00:02:05,000 --> 00:02:08,000
so we can use ampersand which is AND,
49
00:02:08,000 --> 00:02:10,000
we can use pipe operator, which is OR,
50
00:02:10,000 --> 00:02:15,000
or we can use the exclamation mark for not, okay, so
51
00:02:15,000 --> 00:02:19,000
how do we decide what could be the output of two operations?
52
00:02:19,000 --> 00:02:21,000
So let's say the first operation gives you true,
53
00:02:21,000 --> 00:02:23,000
the second operation also gives you true.
54
00:02:23,000 --> 00:02:25,000
So maybe we can do that with the help
55
00:02:25,000 --> 00:02:26,000
of these two conditions.
56
00:02:27,000 --> 00:02:30,000
If both are true, and then what should be the output?
57
00:02:30,000 --> 00:02:32,000
So the output will be true.
58
00:02:32,000 --> 00:02:35,000
Again, I'm talking about in terms of AND operation.
59
00:02:35,000 --> 00:02:39,000
If one is true, one is false, it is false.
60
00:02:39,000 --> 00:02:41,000
If one is false,
61
00:02:41,000 --> 00:02:43,000
I mean the first one is false, second is true.
62
00:02:43,000 --> 00:02:44,000
Again, it is false.
63
00:02:44,000 --> 00:02:48,000
And if you want, if you have both of them as false,
64
00:02:48,000 --> 00:02:50,000
again, it'll give you false.
65
00:02:50,000 --> 00:02:52,000
Now when you want such type of output,
66
00:02:54,000 --> 00:02:56,000
if both are true, then only should be true,
67
00:02:56,000 --> 00:03:00,000
then we can use something called an AND operation here.
68
00:03:00,000 --> 00:03:03,000
But what if you have a different set of things in your mind?
69
00:03:03,000 --> 00:03:05,000
Let me just divide this.
70
00:03:05,000 --> 00:03:07,000
And the second set would be,
71
00:03:07,000 --> 00:03:10,000
let's say if the first one is true
72
00:03:10,000 --> 00:03:13,000
and second one is also true, you will get true.
73
00:03:13,000 --> 00:03:15,000
If the second one is true,
74
00:03:15,000 --> 00:03:17,000
sorry, the first one is true, second one is false,
75
00:03:17,000 --> 00:03:19,000
it'll give you true.
76
00:03:20,000 --> 00:03:21,000
If the first one is false,
77
00:03:21,000 --> 00:03:23,000
second is true, it'll give you true.
78
00:03:23,000 --> 00:03:27,000
And if both are false, it'll give you false.
79
00:03:27,000 --> 00:03:29,000
If you have a condition
80
00:03:29,000 --> 00:03:33,000
or if you have a requirement where if any of them is true,
81
00:03:33,000 --> 00:03:34,000
then it should be true.
82
00:03:34,000 --> 00:03:37,000
So example, if you can see if any of one, so
83
00:03:37,000 --> 00:03:39,000
of course both are true, that's great.
84
00:03:39,000 --> 00:03:42,000
But even if you have one true, that's true,
85
00:03:42,000 --> 00:03:44,000
even if you have one true, that's true.
86
00:03:44,000 --> 00:03:46,000
Even if you have one true, that's true, right?
87
00:03:46,000 --> 00:03:49,000
But if you have both false, then it'll be false.
88
00:03:49,000 --> 00:03:51,000
Now if you compare with this and,
89
00:03:51,000 --> 00:03:53,000
when you have both true, it'll give you true.
90
00:03:53,000 --> 00:03:56,000
If any of them is false, you can see if there's are false,
91
00:03:56,000 --> 00:03:58,000
you will get a false here.
92
00:03:58,000 --> 00:04:03,000
So this is your AND and OR, so that's how you can combine.
93
00:04:03,000 --> 00:04:05,000
We have one more here, which is NOT.
94
00:04:05,000 --> 00:04:08,000
Now NOT basically used to reverse the thing.
95
00:04:08,000 --> 00:04:10,000
Example, if you have true
96
00:04:10,000 --> 00:04:12,000
and if you want the output to be false,
97
00:04:12,000 --> 00:04:14,000
and if you have false
98
00:04:14,000 --> 00:04:16,000
and if the output you want is true,
99
00:04:16,000 --> 00:04:19,000
then you will apply a NOT operation.
100
00:04:19,000 --> 00:04:21,000
Now apart from this, we have one more which we
101
00:04:21,000 --> 00:04:24,000
have not talked about, which is excel.
102
00:04:24,000 --> 00:04:26,000
Again, we'll talk about that in some time.
103
00:04:26,000 --> 00:04:28,000
But then we have three basic operation here we
104
00:04:28,000 --> 00:04:30,000
have AND, OR and NOT.
105
00:04:30,000 --> 00:04:32,000
So basically how you combine this, if you wanna do
106
00:04:32,000 --> 00:04:36,000
that in programming, of course you can also use AND operator
107
00:04:36,000 --> 00:04:38,000
or pipe or you can use NOT directly.
108
00:04:38,000 --> 00:04:40,000
The other option you have is something
109
00:04:40,000 --> 00:04:42,000
called a short circuit.
110
00:04:42,000 --> 00:04:44,000
So instead of using and you can use double and,
111
00:04:44,000 --> 00:04:46,000
instead of using or you can use double or
112
00:04:46,000 --> 00:04:50,000
and not is as it is, we don't have two nots there.
113
00:04:50,000 --> 00:04:52,000
Now what's the use of this?
114
00:04:52,000 --> 00:04:54,000
So first of all, let's talk about and.
115
00:04:54,000 --> 00:04:58,000
So let's say if you have x, x is greater than y.
116
00:04:58,000 --> 00:05:01,000
And if you say, and here and you can say a is less than b.
117
00:05:01,000 --> 00:05:03,000
So we have these two conditions here
118
00:05:03,000 --> 00:05:05,000
and I want both them to be true.
119
00:05:05,000 --> 00:05:06,000
That's where you want the output to be true.
120
00:05:06,000 --> 00:05:09,000
So example, if you have a boolean value, which is r here.
121
00:05:09,000 --> 00:05:12,000
So you can say r is equal to x is greater than y
122
00:05:12,000 --> 00:05:14,000
and a is greater than b.
123
00:05:14,000 --> 00:05:18,000
If in this case, if both are true, the output is going
124
00:05:18,000 --> 00:05:20,000
to be true, right?
125
00:05:20,000 --> 00:05:22,000
But if you, what if you use r here.
126
00:05:22,000 --> 00:05:26,000
So if I remove this, and if I put up r here, if any
127
00:05:26,000 --> 00:05:29,000
of them is true, it'll give you a true.
128
00:05:29,000 --> 00:05:31,000
As simple as that, okay?
129
00:05:31,000 --> 00:05:35,000
Now why do we call it a short circuit?
130
00:05:35,000 --> 00:05:38,000
The reason is, let's talk about OR.
131
00:05:38,000 --> 00:05:41,000
In OR, see the point is if even if you have one true,
132
00:05:41,000 --> 00:05:43,000
the output would be true, right?
133
00:05:43,000 --> 00:05:47,000
So if the first condition itself is true,
134
00:05:47,000 --> 00:05:49,000
it'll not even check for the second one.
135
00:05:49,000 --> 00:05:50,000
Beauty, right?
136
00:05:50,000 --> 00:05:52,000
I mean you're saving your time,
137
00:05:52,000 --> 00:05:54,000
that's why it's called short circuit.
138
00:05:54,000 --> 00:05:57,000
You are saving the time, you're taking a shortcut.
139
00:05:57,000 --> 00:06:00,000
And you will say, hey then what happens
140
00:06:00,000 --> 00:06:05,000
when you have AND here, so let's say, let me put AND,
141
00:06:05,000 --> 00:06:08,000
now in this case, even if so, let's say first one is true
142
00:06:08,000 --> 00:06:10,000
of course we'll check with the second one.
143
00:06:10,000 --> 00:06:13,000
But what if the first one is false?
144
00:06:13,000 --> 00:06:15,000
If the first one is false, it'll not even check
145
00:06:15,000 --> 00:06:17,000
with the second one because the output is going
146
00:06:17,000 --> 00:06:19,000
to be false, right?
147
00:06:19,000 --> 00:06:22,000
So that is why it is called short circuit, okay?
148
00:06:22,000 --> 00:06:24,000
So from this point we are always going to use short circuits
149
00:06:24,000 --> 00:06:27,000
and an order of using a single ampersand.
150
00:06:27,000 --> 00:06:29,000
So it can be, you can say double ampersand for AND,
151
00:06:29,000 --> 00:06:34,000
double pipe for OR, and of course NOT will remain NOT.
152
00:06:34,000 --> 00:06:36,000
So example, let's say if you already have a boolean value,
153
00:06:36,000 --> 00:06:39,000
which is r, so I can say r
154
00:06:39,000 --> 00:06:41,000
and if you want to save that value in s,
155
00:06:41,000 --> 00:06:44,000
so whatever value of r you will get, you will save that.
156
00:06:44,000 --> 00:06:47,000
In example, if r is t, s will be t,
157
00:06:47,000 --> 00:06:51,000
but what if you put a ampersand here or so?
158
00:06:51,000 --> 00:06:53,000
But what if you put a exclamation here?
159
00:06:54,000 --> 00:06:58,000
So in this case, if this is true, if r is true,
160
00:06:58,000 --> 00:07:01,000
it'll give you false, it'll give you a reverse value.
161
00:07:01,000 --> 00:07:04,000
And this is very beneficial when you start working
162
00:07:04,000 --> 00:07:07,000
with the project where you have future, you have a data
163
00:07:07,000 --> 00:07:10,000
and if you wanna do something related to that data,
164
00:07:10,000 --> 00:07:12,000
so based on the data you will change some,
165
00:07:12,000 --> 00:07:14,000
you will do something, you will change the condition,
166
00:07:14,000 --> 00:07:16,000
all those stuff, okay.
167
00:07:16,000 --> 00:07:18,000
This thing looks cool here,
168
00:07:18,000 --> 00:07:21,000
but how will you implement this in the actual code?
169
00:07:21,000 --> 00:07:23,000
Like that's what we're going to see now.
170
00:07:23,000 --> 00:07:24,000
So if you can see,
171
00:07:24,000 --> 00:07:26,000
of course I have to go over the same variables.
172
00:07:26,000 --> 00:07:30,000
I can say int x is equal to let's say 7.
173
00:07:30,000 --> 00:07:33,000
And in fact, you know, yeah, let's do it on your line
174
00:07:33,000 --> 00:07:36,000
and I will say int y is equal to 5.
175
00:07:36,000 --> 00:07:37,000
So we got these two values
176
00:07:37,000 --> 00:07:40,000
and then apart from this, I want two more variables.
177
00:07:40,000 --> 00:07:41,000
Let's say a and b.
178
00:07:41,000 --> 00:07:42,000
So I will say int a is equal
179
00:07:42,000 --> 00:07:46,000
to 5 again and int b is equal to 9.
180
00:07:46,000 --> 00:07:48,000
I'm just getting random numbers here.
181
00:07:48,000 --> 00:07:50,000
And what I want to do is I want
182
00:07:50,000 --> 00:07:52,000
to save the data in a boolean variables,
183
00:07:52,000 --> 00:07:53,000
I will say boolean result
184
00:07:54,000 --> 00:07:57,000
and let's see what the value you're getting.
185
00:07:57,000 --> 00:07:58,000
So first I will declare it here
186
00:07:58,000 --> 00:08:01,000
and I'll also print the value for this.
187
00:08:01,000 --> 00:08:04,000
Okay, so basically we are trying to print result here,
188
00:08:04,000 --> 00:08:06,000
but then I want to perform some operation on this.
189
00:08:06,000 --> 00:08:10,000
So I can say if x is greater than y, okay,
190
00:08:10,000 --> 00:08:12,000
I'm just checking for one thing.
191
00:08:12,000 --> 00:08:14,000
And now you know we have done this multiple times.
192
00:08:14,000 --> 00:08:17,000
I will just compile this code and run
193
00:08:17,000 --> 00:08:20,000
and you can see we got through because the value which of x,
194
00:08:20,000 --> 00:08:24,000
which is 7, is greater than the value of y, which is 5.
195
00:08:24,000 --> 00:08:27,000
But then I want to put one more condition here
196
00:08:27,000 --> 00:08:29,000
and I want both them to be true if I want
197
00:08:29,000 --> 00:08:31,000
to get the outputs true.
198
00:08:31,000 --> 00:08:32,000
So what you can do is you can use ampersand,
199
00:08:32,000 --> 00:08:37,000
double ampersand here and you can say a is greater than b.
200
00:08:38,000 --> 00:08:41,000
Now if you can see the first condition is true, okay,
201
00:08:41,000 --> 00:08:42,000
this will give you true.
202
00:08:42,000 --> 00:08:43,000
But the second one,
203
00:08:43,000 --> 00:08:45,000
the second option here will give you false.
204
00:08:45,000 --> 00:08:48,000
Now, what do you think is the output?
205
00:08:48,000 --> 00:08:50,000
Think about this and let's see.
206
00:08:50,000 --> 00:08:52,000
Now the output, which I'm expecting here is false.
207
00:08:52,000 --> 00:08:54,000
Let's try and yeah, we got false.
208
00:08:54,000 --> 00:08:56,000
So the first one is true, second one is false.
209
00:08:56,000 --> 00:08:59,000
So in this case you both need to be true, example,
210
00:08:59,000 --> 00:09:01,000
if I make this as less than,
211
00:09:01,000 --> 00:09:03,000
of course you should not take change your condition,
212
00:09:03,000 --> 00:09:04,000
you should change your values.
213
00:09:04,000 --> 00:09:06,000
But just we are learning into, we are doing,
214
00:09:06,000 --> 00:09:07,000
we are learning, right?
215
00:09:07,000 --> 00:09:10,000
So it's fine.
216
00:09:10,000 --> 00:09:12,000
Let's compile this code and run.
217
00:09:12,000 --> 00:09:13,000
And you can see we got true.
218
00:09:13,000 --> 00:09:15,000
Since both are true, it's true.
219
00:09:15,000 --> 00:09:19,000
But what if you use a pipe here?
220
00:09:19,000 --> 00:09:21,000
Now pipe simply means OR right?
221
00:09:21,000 --> 00:09:23,000
So even if you are, so let's say
222
00:09:23,000 --> 00:09:25,000
that both the conditions are true now
223
00:09:25,000 --> 00:09:28,000
and both the operations are true, run.
224
00:09:28,000 --> 00:09:29,000
And you can see we got true,
225
00:09:29,000 --> 00:09:34,000
but what if, let me make the first one as false, right?
226
00:09:36,000 --> 00:09:38,000
So you can see we have 7 is greater than 5.
227
00:09:38,000 --> 00:09:42,000
So this will be false. But the second one is true, right?
228
00:09:42,000 --> 00:09:45,000
But still if you compile this and run
229
00:09:47,000 --> 00:09:49,000
and you can see we got true, right?
230
00:09:49,000 --> 00:09:53,000
So this is how you use AND and OR.
231
00:09:53,000 --> 00:09:56,000
When you have multiple relation, of course you can add more.
232
00:09:56,000 --> 00:10:00,000
You can also add one more pipe here by saying the value
233
00:10:00,000 --> 00:10:02,000
of a should be greater than 1.
234
00:10:02,000 --> 00:10:03,000
That's your choice.
235
00:10:03,000 --> 00:10:06,000
How many pipes you want to add, compile,
236
00:10:06,000 --> 00:10:09,000
and run, and you can see it works.
237
00:10:09,000 --> 00:10:12,000
So this is how you use AND and OR.
238
00:10:12,000 --> 00:10:14,000
What about NOT operator here.
239
00:10:14,000 --> 00:10:19,000
So example, let's say I will only say a is greater than b.
240
00:10:19,000 --> 00:10:21,000
Now what do you think what the output is?
241
00:10:21,000 --> 00:10:24,000
So if I compile this code and run
242
00:10:24,000 --> 00:10:26,000
and you can see we got false, that makes sense, right?
243
00:10:26,000 --> 00:10:31,000
Because the value of a is smaller than value of b.
244
00:10:32,000 --> 00:10:34,000
But what happens just before printing result,
245
00:10:34,000 --> 00:10:35,000
I'm saying not.
246
00:10:35,000 --> 00:10:37,000
So whatever result of whatever value
247
00:10:37,000 --> 00:10:41,000
of result is we are just doing the opposite of it.
248
00:10:41,000 --> 00:10:42,000
If this is true, it'll give you false.
249
00:10:42,000 --> 00:10:44,000
If this false, it'll give you true.
250
00:10:44,000 --> 00:10:48,000
And if I compile and run, you can see we got true.
251
00:10:48,000 --> 00:10:52,000
It's because this was false. Other one's false.
252
00:10:52,000 --> 00:10:55,000
And now when I say exclamation result, it'll give you true.
253
00:10:55,000 --> 00:10:57,000
So as I mentioned before,
254
00:10:57,000 --> 00:10:59,000
it's very important once we go forward
255
00:10:59,000 --> 00:11:02,000
towards conditions in if else for loops.
256
00:11:02,000 --> 00:11:04,000
So this multiple conditions
257
00:11:04,000 --> 00:11:07,000
or multiple operations, which will result in true
258
00:11:07,000 --> 00:11:10,000
or false will be very helpful.
259
00:11:10,000 --> 00:11:12,000
Now, apart from this, we have one more which we're
260
00:11:12,000 --> 00:11:14,000
gonna discuss in the next video.