Java Tutorial – How to use Conditional Operator ( ? : )

Java Tutorial – How to use Conditional Operator ( ? : )


hello and welcome. In this video tutorial I’m going to introduce the conditional
operator in Java I going to explain exactly how it works and I’m going to illustrate with a very clear example. Let me begin by motivating why we would use to conditional operator so So, I have a very simple program here called
conditional operator demo It consists of a simple main methods and I’ve initialize a variable a to be minus ten. The whole point of this program is to compute the absolute value of this variable So A is initiallised to 10 as we said We declared a variable called ABS absolute value, an integer, and then we simply check if a is greater than 0 then the absolute
value of a is equal to a because it is positive – if we recall that the absolute value of a number is the positive representation of that number – The absolute value of an integer is the positive representation of that integer So an integer can be either a positive number of a negative number or zero So the absolute value of minus 10 would be 10 So, if a is greater than zero, which means it is already positive the absolute value is simply equal to a If a is not greater than zero, it must be either zero or negative and we say the absolute value is minus a So, if it is less than zero, minus minus is a plus this will return a positive number a positive number and if it’s zero
itself well minus are up to Sir to say so bottom line these four lines this five
five lines to pick apart determined absolute value a niche I can now introduce to conditional
operation Java so here it s first thing I can tell you
about conditional operator shot just one line of code allows us true place the following six lines court
trucks for Scotch restrict she like a conditional operator it allows us to express more coherently and more concisely functionality that we
wished so what exactly is the conditional operation job well
let’s have a look sex Russian this conditional operator
consists up tree parts the first part is to conditional
expressed just a clear line between us the
conditional expression and thus expression always evaluates to IDR true our faults so this is it can tickle to conditional
expression Halfacre touched first part the second part is always followed by gail was Kahne’s after question mark second part is an expression are variable comes
after a question mark and desist valued at its return trip
that is returned from the condition operator plan to conditional expression evaluates
to true so this value here is returned from the conditional
operator plan to conditional expression evaluates
to true and lastly a third part is to expression
are valued at is returned when the conditional operator he finally
reached faults and expression of variables that is
returned for the conditional up if I expression if I was a false always comes after called so simple English we have our conditional
expression here we have result status that is returned when the conditional
expressions true is here and the result is returned from the
conditional expressions faults is here so at me show you an example to illustrate
with an example so I shall come to the code longer need so
we have declared and ASIC 10 send the next line of code to green coat has come to terms is the
absolute value sequel to the following conditional operator conditional
expression a greater 0 well is a greater is 10 greater than 0 yes it is hence devalue its a true hence we turned the
file you to come to me she asked question mark returned a sell it end absolute value
secret a in fact value secret 10 print ad protect to chemistry compiling Ron and a printer 10 that is correct okay let me demonstrate but -10 now let’s have a look at Camp are
conditional expression a greater Sarah is touch or false well -10 is not greater than 0 there for
two conditional expression if I which the faults hence it returns minus a the false expression value the
constant each day after their call so and absolute value is equal to minus a sequel to minus -10 which is placed hence print up list which of course is
the absolute value what we’re looking for to compile on an apperance attack just
cracked school just exactly a conditional
operators we can now simply take these lines of
code and a liter as you can see we have much shorter much more concise much more easy
readable caught I know you might say it first time
missus might okay I’m just not he’s readable the trust me just use it
frequently is just consists conditional operator
consists tree parts the first part a conditional expression
the second part it’s a value that is returned and a
conditional expressions true a third part is the value to return
conditional expressions faults not all stressed so thank you very much watch this video
she any feedback or comments please return to cum sex thank you very
much

2 thoughts on “Java Tutorial – How to use Conditional Operator ( ? : )”

Leave a Reply

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