Nested if vs. if with double ampersand?

When is the right time to use a nested if loop

if(a){
  if(b){

      

over if statement with && operator inside curly braces?

if(a && b){

      

Both problems are used to test two conditions and are true if both meet the conditions.

Is a nested if statement for iterating over arrays, for example nested?

+6


source to share


4 answers


Look at this

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

      

would be equivalent



if(a && b) {
    ... 
}

      

i.e. when a

- true

, test b

, and if it's also true

startup code.

Both codes will have the same difficulty.
If you don't need to do anything after being a

true, then there will definitely be a second code and vice versa.

-2


source


When is the right time to use a nested if loop

if(a){ if(b){

      

over if statement with && operator inside curly braces?

if(a && b){

      

if(a){ if(b){

Not a "nested if loop" for starters . This is a nested if statement. (Or at least part of it if (b) { ...

is a nested operator.)

The word "loop" in IT means repeating something. There is no repetition here.

(Sorry to be pedantic, but if you misuse basic terms, this strongly suggests that either you don't know what the term means, or you don't understand what is wrong, the deadline is up.)


As other answers have explained, two forms

 if(a){ if(b){

      

and

 if(a && b){

      

do the same. So which one should you use? It boils down to two things:



  • Readability: Depending on what a

    and b

    (i.e. in a real program), one form is likely to be more readable than the other for the average Java programmer. The difference in readability may or may not be significant.

  • Individual taste: Some programmers prefer one form over another, for ... personal reasons.

There are several reasons:

  • Style guides: As far as I know, the Java style guide does not tie one form over another.

  • Efficiency: As far as I know, there is no systematic difference in performance between the two versions.


Finally...

Is it a nested if statement for iterating over arrays, for example nested for

Not. Nested if statements are not loops. They are not repeated.

Of course, you can use nested if statements as part of some larger piece of code that iterates ... but there is nothing magical about that.

The bottom line is that such constructions as if

, while

, for

, blocks, declarations, assignments, etc. are general purpose constructs that can be used for an infinite number of different things. The most important thing is to understand what they are doing. Once you know this, the various things that you can use for them become obvious ... if you think about the algorithms that you are trying to implement.

+5


source


I take, for example, checking if the date in the year has already passed.

Assume that a

a month and b

a day of testing,
inspection date 5/20



a2

, and b2

this date is today,
and today 8/13




if you want to check it out, you should do this:

if(a <= a2) {
    if(a == a2) {
        if(b < b2) {
            //true
        } else {
            //false
        }
    } else {
        //true
    }
} else {
    //false
}

      

Not this:

if((a <= a2) && (b < b2)) {
    //true
}

      

If the first look to the second method, the first value should be true

as a (5) is smaller than a2 (8);
but the second value should return false

because b (20) is not less than (13);
so the whole expression is false, but it must be true, because 5/20 must be passed if today is 8/13.

SO THIS IS A METHOD TO CHECK THESE WRONG

Taking a look at the first method, the first value should be true instead, because a (5) is less than a2 (8);
the second value must be false, because a (5) is not a2 (8);
so the result true

.

Returning to the second value, if the months were equal (5), then the third value should be false, because b (20) is not less than b2 (13),
in fact, if the date was 5/20 and today was 5 / 13, this date has not yet been passed.

0


source


If you want to check both conditions together, do a || b

. If you want to check each condition separately, do if(a) { if (b) { } }

.

For example:

if (a) {
    if (b) {
        // Both true.
    } else {
        // Only a.
    }
} else {
    if (b) {
        // Only b.
    } else {
        // Neither true.
    }
}

      

-1


source







All Articles