Linux - If else statement
4K views
Nov 28, 2024
Linux - If else statement
View Video Transcript
0:00
now let us check how we can use this if
0:02
L statement into our programs so
0:05
generally as I said if a statement can
0:07
be used in whenever we are going for
0:09
making any particular decision so as for
0:12
the you know the programs or ask for the
0:14
cases generally you will have or as per
0:16
the conditions you have you have to take
0:18
multiple decisions a program basic
0:21
offense condition will have if standard
0:24
with else called as I fi that is means
0:27
if-else phoenician we are using so if
0:30
suppose we have multiple condition
0:32
correct like if else and if again if or
0:35
else that is true value again I have
0:38
some program if not then also I have
0:41
program than the other now I can take an
0:43
example there is a general case in
0:45
general case a person can be a male or a
0:48
female if not female that will be a
0:51
transgender that is out generally we
0:53
should have right so he can be a male or
0:56
a female or a transgender so if he is
0:58
not a male or female so automatically it
1:00
will go to the default value that is a
1:02
transgender so the same thing we can use
1:04
into this kind of statement means we
1:06
have a third output if any of the cases
1:09
is not true one and case two is false so
1:12
the condition three will automatically
1:14
default Li accepted that is the if as a
1:17
Phi statement the next we have that is
1:20
if else is else fi means is this a
1:25
nested statement we have like if suppose
1:28
we have statement inside a statement
1:30
this is called as a nested statement so
1:33
a program contain you know values inside
1:35
that also we have multiple values for
1:37
example I can say what you want to eat
1:40
veg or non-veg so this is it to
1:43
condition you have either you can go for
1:45
an option one or option two again in
1:47
option two like which we have multiple
1:49
options whether you want to have some
1:51
item related to the rice or any
1:53
particular chapatis so this is again the
1:55
two of the options you have so whenever
1:58
you have options under an option that is
2:00
called as an nested statements so if you
2:03
are using a nested kind of program if
2:05
you have nested statements you can go
2:06
for if else--if as fi statements so
2:11
these are the three type of statements
2:12
under levy
2:13
while creating a program depending upon
2:15
the conditions you have into the program
2:17
so let us check our particular examples
2:20
all of these three programs which we are
2:22
going to use into the shell so here I'm
2:25
going to start with your decision-making
2:28
and I'm going a simple if' statement
2:34
where I'll begin creating on a search
2:38
file and here we are going to start with
2:40
your comment I'm going to add so that it
2:44
will come to know which now come on down
2:46
using so that is if I have statement I'm
2:51
going to use ok and here a keen what I'm
2:56
going to do is here I'm going to define
2:58
a variable and I'm going to add a value
3:00
into it so I have added a value like a
3:03
equals to 10 same thing I can also add
3:06
the value for B so I have added the
3:08
value of B equal to 20 and I want to
3:11
check whether this values are equal or
3:13
not so a simple program I'm writing here
3:16
so for that first I'll define the
3:19
condition in such a way I'll define if
3:21
under the closed and open square
3:24
brackets you need to define dollar a
3:29
that is my first variable equals 2
3:33
equals 2 we can use anybody color no
3:36
variable commands we can be and again
3:40
you can define the space that is done so
3:45
if this is true then you will be
3:51
refining echo what values are
4:04
Iker right so either you can define in
4:09
such a way or you can even call the
4:11
values first I'll define in such a way
4:12
and for ending this I'll be using
4:16
right
4:22
and I'll save this and we'll come out of
4:25
it
4:28
and again I'm going to run the same flow
4:33
so as those files are not equal orthopod
4:37
the variables are not equal that's the
4:39
reason we haven't got anything over here
4:41
now if both the lines or body variables
4:44
are equal not for example I'll just
4:47
change it any one only variable this one
4:53
to 20 and it cannot save it and again we
4:57
can check so you are getting the message
5:00
both variables are
5:03
we can we can use this in such a way we
5:07
can also call the very poor we can also
5:08
get the variables from the users as well
5:10
like we have used into our earlier
5:13
programs like if as such I'll also
5:17
define it in tubby
5:24
I want to take the data from the user so
5:27
echo first number okay read a and echo
5:38
the second number rich be okay then we
5:51
can save this and mode of it
5:56
I've just defined the the value so I
5:59
just need to remove that as well so I'm
6:01
just going to remove the thing okay so
6:06
save it and now I'm going to run the
6:08
same one if start as such first value is
6:12
two second value is 2 I'll get beads
6:14
message if I can I run the same and if I
6:17
mean I get the different values
6:21
I won't give get any particular message
6:23
that means they are not equal but see we
6:27
have used a condition but if you are
6:28
using the simple kind of condition I
6:30
won't be able to get any message if I do
6:32
do not have the same values or you know
6:35
my equation or the condition which I
6:38
have that is true
6:40
I'll get the you know output but if
6:42
there is no true so I won't get any
6:45
particular message on the screen so for
6:48
that we can again move for your
6:49
different variable sorry different kind
6:52
of your we can say like decision making
6:56
which is called as if else know for
6:59
example we are defining the value for
7:01
true value as well as will define thee a
7:03
nation for default values as well so for
7:06
that again I can use a different command
7:10
or you know I can use the same file as
7:15
such we have defined that if a equals to
7:22
B then we should print echo what values
7:25
are equal if suppose this is not true
7:28
condition so what you will have now what
7:31
I can do is I can insert here I just
7:34
change this a file and I'll add else if
7:41
it is not true
7:43
then we should get a message called as
7:47
let's just define tap echo
7:52
what
7:55
values
7:58
are not equal right so this is the
8:03
message I should get and if you get this
8:06
message that means this message is your
8:09
aunt in Dubai or we can also add it here
8:13
that will come to know this is the now
8:17
statement we are adding and due to which
8:20
this complete you know the statement we
8:24
have a decision making we have we are
8:26
using here this EF else
8:36
statement okay by just by adding the
8:38
else value so once we have done with
8:41
this once we have added default value
8:43
again we need to define FY once we are
8:46
handing over here just save it and again
8:49
I'm going to run the same file as such I
8:52
have taught essage to three now you can
8:56
see it is showing you what values are
8:58
not equal
8:59
now same way if I run I have not a set
9:02
and I give the same values if I give the
9:07
same values three and three it will give
9:12
me both values are equal so it is
9:14
accepting both t you know true and for
9:16
all conditions and giving the output as
9:18
per thee decisions you have so this we
9:21
have seen about here if-else statement
9:24
but sometimes your inner programmers but
9:27
lengthy and you need to define multiple
9:29
and L statement that's called as your
9:32
nested statement that we are going to
9:34
see here so for that altogether I'll be
9:36
using some other file or we can say some
9:41
other particular file so I'm just going
9:43
to take other example like 40 nestor
9:47
decision-making or we can see um lost
9:52
position thank you right not as such now
9:59
for that I'll be using a variable that
10:03
is a I'll be using I'll add some word to
10:07
a
10:14
or what I can do is I can add something
10:17
like word
10:21
variable I'm going to add it with E now
10:26
I'm going to set a condition if
10:31
this variable which I have inserted it
10:35
is word is equal to equal to the B if it
10:45
is not a if it is equal to B then what
10:50
we are going to do is
10:55
okay so I have used a double bracket so
10:58
I'll closing with double square bracket
11:00
itself then what the output I should go
11:03
I should get get the output as echo the
11:08
condition is true for example and then
11:16
if not then if else or else--if we can
11:21
use self if this is not true then this
11:29
can be a case where your unified space
11:32
your space your word is equal to equal
11:38
to your letter a right and then close
11:44
just brackets if it is fine then the
11:48
output what we should get that is the
11:53
condition as
12:01
this was because it is not same as your
12:04
be sorry or equal to your
12:12
here we can see if it is true okay this
12:16
is the false condition because in this
12:19
case this is matching with your a so
12:24
here we have set the other condition
12:26
that if it is equal to a the condition a
12:29
is true I'll just define so that I can
12:30
come to know which condition is correct
12:32
or which condition is having a true
12:34
value so for that I'll just define the
12:37
condition matches with B right well B so
12:51
it's true right so if here again I'll
12:56
define the condition matches to a value
13:05
so is it true now we have to find two
13:08
values like you know it is a gues
13:09
matches with two a so again it will be
13:12
true it matches to pre again it is true
13:14
now whatever doesn't matter anything
13:17
correct so for that I can define else if
13:24
both are not matched so we should you
13:27
find the condition what condition are
13:35
not matched what conditions a and B are
13:44
we can define any you know what are they
13:46
you know the output you want you can
13:48
define so both end behavior the
13:50
conditions are not matched
13:56
and so it's false right so this is what
14:01
exactly I have now cane I'll end it by
14:04
using the f5 so this kind of statement
14:06
where you have multiple statement
14:08
multiple conditions this can be called
14:10
as your
14:17
if
14:20
as if that is you can define as e.l.f
14:27
and yer else and then again you're a
14:33
farm right so which we have seen now let
14:36
us check the output of this again I'm
14:40
going to run it with your wise decision
14:42
rod and such a is true right so as we
14:48
have seen like the value which I have
14:50
added to my world
14:55
the value which I've added to my world
14:59
variable was a now if I make any changes
15:02
to it like if I make it to support
15:05
called as P
15:14
so we can check it by giving the command
15:18
decision so it's showing you the B is
15:21
true but if I change and I'll change the
15:24
world as C so it should not more match
15:31
body conditions and it should give me D
15:33
this particular message that both are
15:36
not met and it's false now button exists
15:42
false so this is how generally we create
15:45
a multiple statements multiple decisions
15:47
then we can we can also do entity while
15:52
which we have created earlier F 1 F 1
15:54
and I'm going to do as such so we have
15:59
now here
16:00
else if statement right if suppose I
16:03
have multiple conditions like I have a
16:05
condition like
16:13
you can see a co first number is say B
16:17
number is equals to B then the values
16:20
are equal and then I can I'm going to
16:24
add one more line here now for this I
16:27
can just what I can do is I can save as
16:29
copy of this file itself so I just copy
16:31
the PI F dot s such file with the name I
16:35
have to dot H file let us see here VI I
16:41
have to roll as such so I'm going to
16:44
make the changes so that this particular
16:45
whole you know the decision-making is
16:49
done by using AF LIF and
16:56
else right so for that I just remove
17:03
this line now I'll define multiple
17:08
conditions to it where I have also
17:11
defined the condition I have I'm going
17:15
to add one more
17:23
you
17:26
nation here I'm going to add echo
17:35
thar the number right I'll define dark
17:39
number here
17:44
read see alright so again equals two
17:50
feet and both values are equal F then we
17:58
can get the Pokemon then we can define
18:03
LF you can get echo
18:10
can also set the condition for your
18:12
elsif and less as we have done into the
18:16
earlier one you can also define it as
18:18
else if again the conditions we need to
18:21
define
18:26
if dollar a value equals $2 see
18:36
if it is the same then what we need to
18:39
type is then we should get in message
18:43
called as echo
18:48
what value
18:53
a and C are equal
19:00
and here I will make a changes
19:05
what values of a n
19:11
we are equal right so if it's not so
19:16
both values are not equal this will be
19:19
the case I'm going to add hit enter as
19:22
such I F 2 dot Sh first number 2 second
19:26
number 3
19:27
third number 4 so I just showing you 3
19:30
are not equal again if I use the I have
19:37
to not as such first value is 2 second
19:41
value is 3 hard values again 2 so it is
19:44
showing you a and C are equal right same
19:47
way if I give come on
19:48
as I of 2 dot SH and first value 2
19:53
second value 2 third value is different
19:56
it will show you a and B are equal but
19:59
this is the way how we can create nested
20:01
you know if conditions into the arm your
20:06
decision making so this is how generally
20:08
vision program into the script so the
20:12
last option generally we have that is
20:14
how we can use a case a set so the case
20:17
as such we are going to see into the
20:19
next video
#Programming
#Scripting Languages
#Software