Notice look at Donati operator if you recall we touched upon it well into using operators at the very 2
beginning of the chapter so does an operator. 3
But we are discussing it as part of control flow statements as it can be used as an alternative. 4
If a statement we honor already seen that in certain scenarios. 5
A switch statement can be used as an alternative to an IF statement. 6
Similarly when it comes to Dunalley operator it can be used as an alternate to an IF statement in only 7
one scenario. 8
It let the operator can be used as a shorthand for if else statements that include only single statements 9
that is both f block and as block cannot how more than one statement each hit is the syntax of an assignment 10
statement that involves a Donati operation on the left side of the assignment. 11
We have a variable on the right. 12
We have an expression involuntarily operator that then any operator in good two symbols a question mark 13
and a call in as I mentioned at the beginning of the chapter A eternity operator as the name suggests 14
in most three options the three options are the boolean expression true expression and false expression 15
if boolean expression evaluates to true then true expression is evaluated. 16
Otherwise the false expression is evaluated the final expression that gets evaluated right into value 17
and that value is assigned to the variable on the left side. 18
Not that I that boolean expression is optional and can be used for readability. 19
Unhorse are the same Donati operation can be represented using an if else statement. 20
So if the boolean expression is true then the statement results equal to true expression gets executer 21
otherwise the statement result equal to false expression gets executed. 22
Here is a simple Donati example for computing a minimum of two numbers x and y. 23
If x is less then right then the variable man gets assigned the value of X otherwise it gets this and 24
the value of y so the next question is then do you study the operation in preference to an IF statement 25
. 26
Just like in the case of switched it meant you can use Donati if you think it improves readability. 27
One example will be when we need to construct smart strings. 28
For example let's say we have a program that sends out emails and the e-mail message must start with 29
a customized greeting based on user's gender. 30
So the gender is men then the greeting would be the text Hello followed by salutation Mr. which is then 31
followed by the user's name. 32
Similarly if the gender is female then the salutation has to be missed. 33
So the salutation changes depending on the user's gender and for that we can make use of binary operator 34
as we can see here. 35
So if the metal is merely returns true then the Donati operation would read under text Mr. 36
Otherwise the next Miss would be written plus operators are used to concatenate the strings the final 37
string is assigned to the variable current grouping. 38
Know here is the same representation using an if else statement. 39
So initially the variable grading is initialized to hello and depending on the outcome of the if condition 40
appropriate salutation will be appended. 41
Finally name is up and that with a variable grading as you can see the Donelly version is much more 42
elegant with only a single line of code. 43
Similarly and they're good for using Donati is unlock statements statements print important messages 44
during program exudation and they can be very useful for fixing bugs. 45
Although here we are using a print aland statement in professional projects you would be using some 46
kind of cataloging framework it here is an important thing to note. 47
If you recall when learning about statements we mentioned about three types of statements and one of 48
them was expression statements an expression statement is simply an expression appearing as a statement 49
that is the expression followed by a semicolon. 50
Know even a dirty operation is an expression. 51
However it cannot be expressed as an expression statement that is only certain kinds of experience can 52
be expressed as expression statements and not all of them. 53
For example at post-road a pre-increment or a post predicament operation which is also an expression 54
can be an expression statement. 55
Similarly any method in Kishan can be an expression statement so basically you cannot do this and you'll 56
get a compiler error if you do this. 57
It needs to be part of an assignment statement or it can also be part of other kind of statements like 58
metheglin location statements or even a method to that statement. 59
Let's act will it be a short term goal of using it as a written statement. 60
OK let's just go ahead and write a simple method call men which returns minimal off to numbers and it 61
will make use of the or the operator. 62
First let's define a variable called Min. 63
We are in the basic democracy and it simply invokes the method meant. 64
And let's just pass two parameters say nine and three. 65
And let's just print this value let's call it min k now let's just go ahead and define the method also 66
. 67
So it's going to be a static method and men that say and X pain why so let's just define what it is 68
that and it's have there been any operation 69
less then by then you are done X otherwise and Y and are done it is at so let's just compile and run 70
this 71
as you can see to spending three. 72
So we were passing nine and three and spending the the minimum value with just three. 73
Now just remember that you cannot do this. 74
That is eternity. 75
Expression cannot be used as an expression statement. 76
So let's just try to compile this. 77
See it says error. 78
Not a statement. 79
OK so you cannot do that. 80
Let's put the written statement directly because we said that it can be used as a written statement 81
as part of a written statement. 82
So I may just compile and run this once again. 83
A search still returns three. 84
So that's the only operator. 85
So you can mainly use it if you think it will improve readability. 86
So in that case just go ahead and use it. 87
OK. 88
So just go ahead and you can practice on audio operator. 89
So just play around with it and that's about it. 90
Thank you. 91
And happy coding.