Change if-statements

I am working on minimizing the code and improving it. My question is: is it possible to replace if statements above without destroying the logic of the code?

    int c1 = Integer.parseInt(args[0]) ;
    int c2 = Integer.parseInt(args[1]) ;
    int c3 = Integer.parseInt(args[2]) ;

    if (c2-c1==0)
      if ( c1 != c3 )

Since between the two, if there are no operations in the statuses that record variables, I would say yes, but I'm not sure.

Any ideas?

+3
source share
5 answers

I really don't understand why you want (c2 - c1 == 0)insteadc1 == c2

why not go

if ((c1 == c2) && (c1 != c3)){
  do_stuff();
}

or

if ((c1 != c3) && (c1 == c2)){
  do_stuff();
}

way

As a side note, there is no penalty or advantage when switching c1 to c2 anywhere. From the above, at first the most probable failure condition is somewhat more efficient, because the second condition will not be evaluated if the first is not fulfilled.

: -, .

: , , ( ), . c1 c2 , c3, , . . , . - , .

+10
int c1 = Integer.parseInt(args[0]) ;
int c2 = Integer.parseInt(args[1]) ;
int c3 = Integer.parseInt(args[2]) ;

if (c2 == c1 && c1 != c3 ) {
   ...
}
+3

. if ((c2-c1==0) && (c1 != c3)).

+1

if ((c1 != c3) && (c2-c1==0))

- , c2-c1==0 .

+1

If there are no other blocks, I suggest you write it as:

if (c2-c1==0 && c1 != c3 )

or, if you want to change them,

if (c1 != c3 && c2-c1==0)
+1
source

Source: https://habr.com/ru/post/1772332/


All Articles