Conditional Statement and Logical operator

Conditional Statement and Logical operator


okay in this video we’ll be looking at
another variation of the conditional statement in so let’s take a scenario
where we talked about the student grade student score to print conversion so
before we just set pass/fail what if we were to like give them actually let’s
take a letter grade so a letter grade would be a if the score is between ninety and a hundred it’s a be if this
poorest between 80 and 89 it’ll be a see if score is between 70 and 79 and D if
the score is between 60 and 69 F otherwise if all of the other ones fail
then it’ll be so we just have to go one continue this logic we have done this
before but we just did the pass/fail these twenty seventy percent so again
the first step is to declare a variable to hold the score in score then we have
to declare a variable that look a whole the text value then we’ll be reading the
score using the console dot readline so this will be a text value sorry
it’s a string score as text will be a string variable first we read that next
is code will be equal to int 32 dot parse of scoreastext so the next part
is now we got the input input has been direct just we have the score in hand
now it is stored inside this variable false cool
the next step is to evaluate so if condition so if something so this was
the pattern I taught you right and then I said else so this would be perfect if
we just had a condition that branched into if for else now we have all these
variations right so let’s write the first condition so this is where we have
to do instead of else we’ll say else if the final statement will be called else
if else if else okay you can have multiple elsif depending on our
condition so let us see this this will be the if condition this will be the
else condition so we have 1 2 3 elsif conditions so we have to add 3 tell us
of conditions so that’s 2 and here’s third one so the reason why I
write out like this is so we don’t miss any closing parenthesis here now all
that now we can come to the condition so my condition is going to be if score is less than or equal to 90 and oh wait
till then sorry less than or equal to 100 and score greater than or equal to
90 okay so this is called as a logical combination when you say and so this is
like this condition should be true and this condition should be true
when you have two such conditions you could have. these are all logical
operators so you could have the “and” which is “double ampersand”, or
will be “double pipeline” and then not which will be the exclamation so those
are the three logical operators. we can use so logically it will say if the
score is less than or equal to hundred and score is greater than or equal to 90
is that right between 90 and 100 yeah so we will use that as a condition so you
in order to say and if this and this we use the double ampersand okay so what
does this mean console dot write line I’m going to say you score and a this
would be a B this would be a C this would be a D or this is not an E it’s a
F and let’s adjust these okay so now let me just have to have the expression so
the next one will be between 80 and 80 and 89 score less than or equal to 89
and to score greater than or equal to KKP for less than or equal to 79 and for
greater than equal to 70 or less than or equal to 69 and score
greater than equal to 60 whoops a cursor went somewhere so that is how
if else if else looks so this is when there are like so
many different options to do then we have to go through and we can do like
this kind of multiple conditions so the one thing you have to be careful about
is that that you just don’t have like an orphan if or an orphan else somewhere so just make sure like how I did it right the if – else–if and then the “else”
make sure all the parenthesis are closed and then you’ll be good to go.
so console dot readkey okay so let’s run this program see you test all the
conditions okay so it is waiting for my score so I
have a 98 it says I got an A okay I want to give a prompt here so let’s do a
prompt here also dot write line enter or that way it’s easier always make sure you test for every
single condition so in this video I won’t be doing all of
them but let’s just do like a beam and then we will do a fail, fail condition so that would be eighty six will be a
B there you go then one more condition let’s test the fail condition you fifty six would be a fail okay so I my
code went through the if-elsif and also the else conditions so in this video
you’ve learned about the if-elsif-else you’ve also looked at the logical
operator and hope you found this video useful thank you

Leave a Reply

Your email address will not be published. Required fields are marked *