Articles : The best way to write multiple IF statements into a single statement in Java,C++,Android-Java,etc
full Link : The best way to write multiple IF statements into a single statement in Java,C++,Android-Java,etc
Article android-java, Article featured, Article how-tos, Article java, Article programming,
The best way to write multiple IF statements into a single statement in Java,C++,Android-Java,etc
Hi everyone,
Today iam going to give you a wonderful trick a programmer facing off while dealing with IF statements.
Introduction
The most important phase of a program is its dynamicity.So without an IF ELSE statement,merely no programmer can write a program with dynamic content.So whats the big problem comes upon is the source file,its size is increasing according to the number of IFs are increasing.So why not just make multiple IFs to a single statement.And be a smarter programmer while others reviewing your source code.
How to write multiple IF statements into a single statement?
Here comes the saviour CONDITIONAL operator.
?: operator
The value of a variable often depends on whether a particular boolean expression is or is not true and on nothing else. For instance one common operation is setting the value of a variable to the maximum of two quantities. In Java you might write
Example
Unmodified Code
How easy it is?
11 lines of code into a single line code.Which is better?.Hm.Feel free to use the above trick anywhere in any language that support conditional operator and always be a smarter developer.
And don't forget to use the comment box :).
Today iam going to give you a wonderful trick a programmer facing off while dealing with IF statements.
Introduction
The most important phase of a program is its dynamicity.So without an IF ELSE statement,merely no programmer can write a program with dynamic content.So whats the big problem comes upon is the source file,its size is increasing according to the number of IFs are increasing.So why not just make multiple IFs to a single statement.And be a smarter programmer while others reviewing your source code.
How to write multiple IF statements into a single statement?
Here comes the saviour CONDITIONAL operator.
?: operator
The value of a variable often depends on whether a particular boolean expression is or is not true and on nothing else. For instance one common operation is setting the value of a variable to the maximum of two quantities. In Java you might write
Setting a single variable to one of two states based on a single condition is such a common use ofif (a > b) {
max = a;
}
else {
max = b;
}
if-else
that a shortcut has been devised for it, the conditional operator, ?:. Using the conditional operator you can rewrite the above example in a single line like this:max = (a > b) ? a : b;
(a > b) ? a : b;
is an expression which returns one of two values, a
or b
. The condition, (a > b)
, is tested. If it is true the first value, a
, is returned. If it is false, the second value, b
, is returned. Whichever value is returned is dependent on the conditional test, a > b
. The condition can be any expression which returns a boolean value.Example
Unmodified Code
String mode="";Modified Code with conditional operator
if(age<=10)
mode="child";
else if(age>=11&&age<=19)
mode="teenager";
else if(age>=20&&age<=45)
mode="adult";
else if(age>=55&&age<=101)
mode="old man";
else
mode=null;
String mode=age<=10?"child":age>=11&&age<=19?"teenager":age>=20&&age<=45?"adult":age>=55&&age<=101?"old man":null
How easy it is?
11 lines of code into a single line code.Which is better?.Hm.Feel free to use the above trick anywhere in any language that support conditional operator and always be a smarter developer.
And don't forget to use the comment box :).
Articles The best way to write multiple IF statements into a single statement in Java,C++,Android-Java,etc has been discussed
A few gadget information The best way to write multiple IF statements into a single statement in Java,C++,Android-Java,etc, hopefully can provide benefits to you all.
You're reading an article The best way to write multiple IF statements into a single statement in Java,C++,Android-Java,etc and this article url is https://androidtabletgadgets.blogspot.com/2015/04/the-best-way-to-write-multiple-if.html?m=1 you can bookmark , Hopefully this article could be useful and do not forget to always to this blog to find information on gadget.
Tag : android-java, featured, how-tos, java, programming,
0 komentar:
Posting Komentar