-1

I suddenly got a strange doubt..

I want to know whether

if(a && b) {
}

is same as

if(a) {
  if(b) {
  }
}

or not..

Both the cases giving me the same result. But, I'm still not feeling comfort to use the first method in my project.

Any helps??

Gugan
  • 1,625
  • 2
  • 27
  • 65

8 Answers8

2

If using If-else statement then if loop will execute only condition is true:---

if(condition){
//execute when it is true
}

In your case you are using Two variable a and b with AND OPERATOR. The property of AND OPERATOR is If All giving value is true then it will return true otherwise false.

If you want to use your method then

/*
check for your in a and b
both are true
*/
 then your method will be execdute

 if(a && b) {
 //if a and b both return true(a=true,b=true)
 }
Shailendr singh
  • 686
  • 6
  • 19
1

Yes they are the same as far as functionality is concerned. && is a short-circuit operator. So, in a && b, b will be evaluated only if a is evaluated to true.

Same is the case with nested if. The inner if will be executed only when outer if is evaluated to true. But the 1st one shows your intents better if you want to execute the code only when both a and b are true.

But, one difference is that, you can implement the functionality of a = true, b = false, in 2nd case by adding that functionality before the nested if starts. But, you can't do that in 1st

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
1

It is same. In the first case compiler guarantees that second condition will not be executed if first condition returns false.

Sergey Makarov
  • 2,491
  • 2
  • 19
  • 23
0

a && b means if a is tue and so is b, so:

if(a && b) {
}

does not leave space for indecision, it's all or nothing, only if a && b the if body will be executed, while

if(a) {
  if(b) {
  }
}

leaves you space to act if a, without considering b for the moment. So if you want to do some actions before checking b you can.

If you do not have anything to do just when a then the two are equal and imho the first is to prefer because it is more readable.

Alberto Zaccagni
  • 30,779
  • 11
  • 72
  • 106
0

Both conditions are same as in first one: if(a && b){ }, you are checking with && operator So, in a && b, b will be evaluated only if a is evaluated to true.

while in second case, first of all it will check value of a in outer if condition and if it satisfies then it will check value of b in inner if condition.

But fist one is more preferable.

Manish Doshi
  • 1,205
  • 1
  • 9
  • 17
0

Yes, it's the same thing. The && operator checks if both conditions are true. If you nest your if statements like that, it amounts to the same thing since the second one will only be checked if the first one is true. Only separate them if you need something done that involves a being true but not b.

Mark M
  • 1,580
  • 10
  • 22
0

In both cases It's just short-circuiting.

in which the second argument is only executed or evaluated if the first argument does not suffice to determine the value of the expression:

The short-circuit expression x Sand y (using Sand to denote the short-circuit variety) is equivalent to the conditional expression if x then y else false; the expression x Sor y is equivalent to if x then true else y.

Coming to the syntax,The first thing (if(a && b) )which beautifies your code and and more readable.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

As many have stated before me: they are technically equivalent (right down to the short-circuit semantics).

But I dare say the && operator is preferred in any situation, and that it's not just a matter of taste. The nested if is harder to read in day-to-day code with more than just control statements, and leaves more room for error.

Mostly because if(b) communicates: "the following block is only executed if b is true".

But that's not what happens! The block is only executed if a and b are true (which is exactly what the first method communicates rather elegantly). It's very easy to unintentionally pull it out of the if(a) context (any IDE has plenty of ways to shuffle code around) and create a bug.

The nested if also leaves a lot of room for horrible code later on. Wedging code between the inner and outer can make it tricky to understand which code is executed when - especially if a and b are complex expressions. And things become really nasty when somebody decides to toss in a few else-statements.. :-) There's simply no way to get into that mess with a single && operator.

Some might argue this "flexibility" is a reason to use the second method, but I'd argue there is almost always a way to rewrite that cleanly using explicit conditions. Coding is hard enough without spending all your brain cycles on control logic.

Bottom line: every Java programmer understands the semantics of the conditional-AND operator. Relying on built-in language constructs instead of rolling your own equivalents goes a long way towards maintainability and correctness.

Hope this helps.

Jeroen K
  • 145
  • 1
  • 7