Lecture 19

IIT Kanpur July 201
20 followers
2 Video Views·Dec 1, 2023
[Music]
[Music]
hello friends welcome to the course
foundations of r software
and in this lecture we are going to
begin with the new topic this is about
the conditional executions
the first question comes here what are
these conditional executions so you know
that
suppose we take a call that in case a
student gets more than
50 percent marks then the student is
going to be declared as pass
and
and in case if the student gets marks
less than 50 percent then the student is
going to be classified as fail
so now in this case how are you going to
execute this command
first you want
that
some condition has to be checked what is
that condition the condition is
to check
whether the student has got
marks
more than 50 percent or is smaller than
50 percent
based on that you are going to take a
call
what is going to be the outcome
whether pass or fail
so
similarly we have many more such
conditions where the complication
increases or the
need of the situation changes so in
order to fulfill those things we have
different kinds of conditional
executions
and essentially we are now beginning
a topic on the control structure control
structure means you are trying to
control the
control different parts of your
programming
so the first part which we are going to
talk about
under the heading of
under the broad heading of control
structure is the conditional execution
so to curry in this lecture we are going
to talk about the if and if else
conditional execution commands and we
will try to see that how these
conditional executions can be done in
the r software and and in particular if
and if else conditions what are these
conditions and how they have to be
executed so in order to explain you i
will try to take here couple of examples
and through those good examples i will
try to show you that how
these conditional executions are
implemented in the r software so let us
begin our lecture so as i said now we
are going to begin with the topic of
control structures in the r software
so first we are going to discuss about
the control statements then functions
and after that loop so this will
continue in the next couple of lectures
and we are going to cover all these
topics one by one right so as i already
have explained you that why do we need
the conditional executions the
conditional executions are needed
when we want to execute a command when
certain conditions are true or they are
satisfied now these conditions can be
one or more than once so based on that
we have different
types of contacts and commands in the r
software and among them we are going to
discuss first the
if condition
right that is the popular name that is
how we try to address it so the syntax
for the conditional execution using if
is like this first we try to write down
here if i f
then inside the parenthesis you can see
here we try to
write the condition
which we want to test
so this condition is to be tested now
this condition that is going to be a
sort of
logical statement
is outcome is going to be either true or
false so
in case if the condition is true then
whatever commands we want to execute
that we have to write inside this curly
bracket right so i will write down here
these brackets and here i will try to
write down all the commands which are
going to be executed
when the condition is
true and
in case if the condition is false
then whatever is written inside this
curly bracket
all these conditions are ignored so this
is how the
if condition works
in the case of
our software so now you can see here
couple of things number one when you
learned about the logical statements now
you can see one application of those
logical statements that you want to
first test and this outcome is going to
be a logical variable in terms of true
and false so for that you would also
like to check
whether the outcome of the condition is
a is logical or not
so all the commands which we have learnt
in the earlier lectures they are now
going to be
useful as we proceed further and after
that i had
discussed in the beginning that in
mathematics we have three types of
brackets this simple bracket then curly
bracket and then a square bracket and i
had explained you that in r software
these brackets have got different roles
and in mathematics i had requested you
to use only this parenthesis this small
brackets this
simple brackets and i had earlier
explain you one utility of this square
brackets and now i am i have explained
you here one utility of this curly
bracket if you recall at that time i had
told you that in r software
this parenthesis curly and
this square bracket they have got
different types of utilities and this is
one of the utility where it is going to
be used
right so
what you have to keep in mind that when
you are trying to use this if condition
try to use it only when there is a
scalar
and this if condition should not be
applied when the condition whatever this
condition here that we want to evaluate
is a vector so this is a
best use only when
we are trying to meet a single element
conditions so what is this single
element condition i will try to show you
that there is only one condition which
has to be met
and so on so the
basic syntax
for this if condition is very simple if
then inside the parenthesis write down
the condition and inside the
curly bracket you try to write down
the statements or the commands which
need to be executed in case if you try
to look at the flowchart of this
if
conditional execution it goes like this
that you start you try to test here the
condition
now there are two option the condition
is true or the condition is false in
case if the condition is false here like
this then
we come and we stop here
and in case if the condition is true
then we try to execute the commands and
then
the program is stop so that is how the
if condition works so now let me try to
take here some example and try to show
you that how it works suppose we want
to multiply all the values in some
program
which are greater than four
and we want to multiply such values by
three right so somebody will enter some
value
the condition
whether the value is greater than four
or smaller than 4 that is going to be
checked
and in case if the
this condition that x is or the number
what you have entered is greater than 4
if this is found to be true
then whatever is the number this is
going to be multiplied by three and if
this is false the input number is not
greater than four then nothing will
happen right so that is the thing which
we want to do it and you can see here
now that using the conditional execution
concept and using the if commands we can
do it very easily
so suppose i give here an input x is
equal to 5 and then i try to write down
if condition here like this if
and inside the parenthesis you try to
write down here
x greater than four and after that you
write here x star three that means the
number is going to be multiplied by
three right here in this case either you
write here curly bracket or not this
will not make much difference because
there is only one condition one an
element but
later on you will see that when we are
trying to increase such conditions then
this curly bracket will be
necessary
so here on the r console i will try to
show you the application of actually
both so in this case since you have
taken the value to be 5 and this 5 is
greater than 4 so the condition is here
true and so this number is multiplied by
5 and you get here 3 into 5 as 15. now
in case if you try to take here another
number here x equal to 3 then what will
happen 3 is less than 4 this is
false so now in this case
no outcome will be obtained if you try
to execute this condition if x greater
than 4 and then x into 3
node no
response will be obtained here so you
can see here when i am trying to take it
x equal to 5 then i get this outcome but
when i try to take here x equal to 3
then you can see here as soon as i enter
on the r console there is no outcome
right so let me try to show you
and now i
would like to show you the same example
in the bracket
for this execution so that you do not
get confused
right
so
you can see here that this is the same
program i take x equal to five this is
the same condition if x is greater than
four but now i use here these curly
brackets and you can see here this gives
you the same outcome and similarly if
you try to take here this x equal to 3
and if you use the same condition here
but now i am using here this curly
brackets then it is giving you no
response you can see here this is the
same condition
but here i have used the curly bracket
and in the earlier example i had not
used here the
curly brackets right so as i said it
will not make much difference
as long as you are working with the
scalars single conditions
now let me try to show you this
execution on the r console so that you
get more confident so you see when you
are trying to execute such statements on
the r console directly
then first you have to give the value of
the variable so for example if i say
here x equal to 5 and then i have to
give here this condition and as soon as
i give this condition you can see here
that
this condition is going to give you the
value
15
right
and here
in case if you
try to
take here the
same condition but if you try to now
put here
the curly brackets you will see here you
get the same outcome
but
in case if you
try to take here x equal to suppose here
2
then you can see here what happens to
this condition this will give you
no output because there is nothing
because the condition is false so there
is no outcome and even if you try to
write down here this curly brackets also
this will not give you anything so this
is what i meant when i was trying to
explain you on this example
so now let us try to take one more
example and try to see
what happens so you know there is a
command here print
so print is used to
print something on the screen
right so well we are going to discuss
about different types of such options in
the forth coming lectures but here i
would like to show you something which
is based on the use of print so now i
want to print a value in case if the
value is more than 3
so
suppose i give here the value x equal to
6 and then i try to write down
this
requirement in the form of r contacts so
i try to give here the condition the
condition here is x greater than three
so it is if
inside the parentheses x greater than
three and now in this curly bracket i am
trying to write down the condition here
so the condition here is
print
and whenever we are trying to
print something which is scalar we try
to give it inside the double quotes so i
try to write down here within double
quotes
the value is more than three
and i try to enclose it inside the
parenthesis so now if you try to
ah
execute it because the value of x is six
so 6 is greater than 3 this condition is
true so whatever is there inside the
curly bracket that will be executed and
you get here this outcome
so you can see here that if you try to
take here x equal to 6
then it gives you here this outcome but
if you try to take here at x equal to 2
then it will give you here no outcome y
because 2 is greater than 3 no this
condition is false right so yeah that is
the same condition i want to show you
here so let me try to show you here what
happened with x x equal to two this two
is greater than three this condition
here is
false and so this condition is false so
whatever is written
inside this curly brackets this will not
be executed so if you try to execute it
here
no outcome is going to be obtained so
let us try to do this example on the r
console and then we try to
see what happens ok so let me clear the
screen and you know the command here is
control l
so now if i try to take here some value
here x equal to suppose six and then i
try to give this statement like is here
you can see here the outcome is the
value is more than three but on the
other hand in case if i try to take here
some value here two
and if i try to repeat the same thing
here you can see here
there is no outcome here no outcome
but in case if you just
change it x to be here 10
and then if you try to repeat the
command you can see here the outcome
comes here once again the value is more
than three right
yeah it is also possible that you can
also write what is the value of three so
these things we are going to learn
in the forthcoming lectures that you can
write that you here that
the value is more than three that is ok
but what is the value which you are
trying to compare so so you can also
print the value of x but
that we leave it at the moment for the
future lectures right so now you have
understood that how this
if condition is going to work
and but now you have screen here that
in this case you have a constraint that
if the condition is true only then
whatever the condition you want that is
executed but
in case if the condition is false then
nothing is happening but
now in case if your
wish or your requirement is that if the
condition is true then
something should be printed or something
should be executed
and if the condition is false then
something
else should be
computed for example
the example which i took in the
beginning that if a student has got more
than 50 percent marks then it should be
for example printed that the student has
passed
and if the student has
received the marks
in less than 50
percent then it should be printed that
the student is failed
like this so these type of conditions
can be executed using the conditional
execution with the help of the
concept of if else conditions so as the
name suggests if else if
it is true
then something should be executed and
else that means if it is not true then
something else should be
computed
so if you try to understand
this situation by the flow chart of this
if else execution then it goes like this
you start with something
then there is a condition which is here
checked now there are two options
that the condition is true or the
condition is false in case if the
condition is true
then it will work just like your if
condition and whatever is the condition
or the code you have written that is
going to be executed and after executing
it the program will stop
but
in case if the condition is false then
in the earlier case
there was no output but now in this case
you are going to
write down here some more here core
that is called as
else code means when the condition is
false then under else these
contacts are going to be executed and
then after its execution the program
will stop so this is how this if else
condition works
so in r
the syntax for this if else condition is
like as follows
you try to write down here if
or in lowercase alphabets and then
inside the parenthesis you try to write
down the
condition now this condition is going to
be either true
or this condition is going to be here
false
in case if the condition is true then
whatever you want to
execute that has to be written inside
the curly bracket just after this so
this will go very true condition so
there can be commands here they will be
executed
and in case if the condition is false
then what will happen so after that you
write here else
e l s e all in
lowercase alphabets
and then within the curly brackets you
try to write down the commands which you
want to execute when this condition is
false
so that is a very simple thing that is a
sort of extension of the if condition
that you are adding here one more here
syntax for the else
so
in this
if else condition
ah you have to be ah little bit watchful
that the condition in the control
statement may not be vector valued
and in this case only the first element
of the vector is used
right and
means ideally if else condition should
not be applied when the condition which
we want to judge or which we want to
evaluate in terms of true and false is a
vector
and
this if else condition is best use when
you are trying to
have a single element
right
and when you are trying to use this
condition
means you want to judge whether this is
true or this is false this condition may
be a simple expression or any complex
expression where you can use all your
logical operator like s and
ah like i say or say or
right whatever logical operators we have
studied they can be used here in
defining the conditions in the proper
way
ok so now let me try to take here
one example and try to show you how the
things are going to work and the more
important part which i always say is
is that you have to understand what and
how are is going to execute it
so now
ah suppose i
try to write down here a function like
this
means i can write down here this
function
takes value x minus 1 if x is equal to 3
and it takes value to x otherwise so
that is the
most
simple
language
of mathematics in which we study
so now in case if you want to understand
what are you going to do
you are trying to say that
in case if the input here x is equal to
3
then
this has to be executed that is x minus
1 and in case if number is something
else
that x is not equal to 3
like this
then
whatever the
number is
there that has to be multiplied by two
so now in case if you want to write this
statement then what you have to do
the first thing is this you when you try
to see here what about this x equal to 3
which i am writing here
you are trying to say that if x is equal
to 3 then execute x as
x minus 1
and in case if x is not equal to 3 then
execute x as twice of x
what is this x this is an outcome
that will be obtained after giving the
input value and after this if you try to
see this
equality operator
this is mathematical
no this is logical
so that is why
when you try to write down the condition
you have to use the
logical operator
which consists of two equality sign and
that is indicating the
exactly equal to sign
under the logical operations so now if
you try to see here how are we going to
execute it so
the inputs here suppose is x equal to 5
and now i write down here if and inside
this parenthesis i try to write here x
equal to 3 that is exactly equal to
using the double equal to operator
and now i am saying
that
in this case
x should be replaced by
x minus one so i try to write down here
this curly brackets and within this
curly bracket i write here x equal to x
minus 1 and
in case it is not true that means x is
not equal to true
then i try to write down here l and then
within the curly bracket i write to i
write down here x is equal to twice of x
one thing i would like to have your
attention here i mean and my advice is
do not misunderstand this statement for
example here i am trying to write down
here x is equal to x minus 1
please understand this is not a
mathematical operation what we are
trying to say is that replace x by
x minus 1 i mean some people may write
it think it mathematically that x is
equal to x minus one and they try to
cancel x x it will be simply rubbish it
has no meaning
and similarly if you try to look at here
this is here x is equal to twice of x
this means
replace x by twice x
this is the meaning it is not like that
you write x is equal to two x and then
try to cancel x and x and then try to
prove that one is equal to two which is
wrong right so this is also wrong this
is also wrong so please ah do not
misunderstand this
expression ok so now if you try to see
here what will be the output so if i try
to take here x equal to 5 then in this
case x is not equal to 3 and so x will
be
operated as 2 2 into x so the value will
come out to be here 10 and you can see
here this is here the 10 and this value
here is coming out to be here 10 right
so let me try to execute this expression
first on the r console and then i will
try to move further
so
let me try to
copy this condition so that i can save
some time
and now if i try to take here x equal to
here 5 remember first you have to give
the value
of x as input and then i try to write
down this condition and then i try to
write down the value of here x so now
you can see here you had given the value
of x to be as five but now the value of
x is transformed to
ten
similarly if you try to take it here x
equal to here nine
so what will happen here this condition
is going to be operated once again and
then if you try to print out the value
of here x this is here eighteen that is
two into nine is eighteen
but now i try to take it here x equal to
three
and then if you try to repeat this
condition and if you try to see what
happened to the value of x this becomes
here x minus 1 that is 3 minus 1 which
is equal to
2 right so this is how you have to
operate with this if else
right so exactly this is the same thing
which i just explained you on the r
console that you try to take here x
equal to 3
and then
if you try to operate because x equal to
3 is now true so this is going to be
operated and x will be replaced by three
minus one is equal to two and then you
get here an outcome two right ok so now
i try to take here one more example
quick what is the same example or a sort
of extension of the earlier example
which we considered that we want to
print
if a value is more than 3 or
less than 3. so earlier we had
printed the value is more than three but
uh what if the value is smaller than
three then there was no outcome
right so now i try to extend it and i
try to show you here how you can do so
you can see here you have here two
condition that you try to give here an
input x
and this condition
is that
that x is going to be checked whether
this value is more than 3 or a smaller
than 3 and based on that there will be
an outcome true or false and based on
that there will be two outcome if the
condition is true then the then you have
to print the value is more than three
right like more than three
and if the value is here false the
outcome here is false then you have to
print here
less than three
ok so now how to get it done
so i try to write down here this
statement if
inside the parenthesis x greater than
three
and then within this curly bracket first
set of curly brackets i write here print
and then within the parenthesis and
within the double quotes i write down
here whatever i want to type the value
is more than three and if this condition
is false because
this x greater than three will have here
two outcomes one is here true so when
this two is there this is going to be
operated and if this outcome here is
false then i have to write down here
else and after that i have to write
another pair of
curly brackets
and i have to write here what we want to
do so here i want to
print the value is less than 3 so i try
to use here this parenthesis and i try
to write down the statement within the
double quotes
so now
in case if you
consider here this x equal to here 6
because 6 is greater than 3 this
condition is true so whatever is the
condition under
the true part that the value is more
than 3 that is going to be printed here
right and similarly if you try to
take here say here one more value here
that x equal to here 2 then what will
happen 2 is greater than 3 this
condition here is false when this
condition is false then you are going to
print whatever is written after the
else command
and it is here print the value is less
than 3 right so you can see here the
outcome will come here like this and you
can see here this is here the screenshot
of both these operations but let me try
to ah
show you these operations on the r
console
to make you more confident right so let
me try to
copy this command so that i can save
some time
and i try to take it suppose here x
equal to five
and if i paste this command you can see
here it is printed here the value is
more than three and
in case if i try to take here x equal to
two then you can see here
it is printed here the
value is less than three
so you can see here it is not a very
ah
difficult command here
and yeah
so this is the screenshot
so now ah we
come to an end to this lecture
and
you can see here i have explained you ah
two types of conditional execution one
is using the condition if and another is
the
if else so
these if and if else now you can very
clearly understand they have got
different types of role
the if condition is going to be used
only when you have one condition that if
the condition is true then this has to
be executed
but in case if you have suppose two
outcome and you want that in case of the
if condition is true then this should be
done and if the if condition is false
then something else should be done then
you have to use the if else condition
and believe me these are very useful
conditions
because whenever you are trying to write
down the programs basically in
statistics at least i know
that you have these types of condition
that this has to be
evaluated
only when the condition is true for
example many times you want to evaluate
a mathematical expression
only in case of the value of that
expression is greater than 0
right suppose you want to find out the
square root of some complicated
expression
and
when you are trying to evaluate it you
would like to find out the square root
of that
the outcome only when the outcome is
positive and suppose the outcome can be
positive or outcome can be negative
so in this case this type of conditional
execution help us and we always say
please try to check this condition if
this condition is greater than 0 only
then you try to find out the square root
so these are the ingredients of a
program
so now my request to all of you is that
you try to take some example try to
create the example yourself because when
you are trying to create an example
yourself you also know the answer
and then you try to see that when you
are trying to execute the same concept
in the r software are you getting the
same outcome
and that will give you more confidence
and after that you will become a good
programmer so you try to practice it and
i will see you in the next lecture with
more details on the conditional
executions till then good bye
[Music]
do
[Music]
[Music]
you
