if statement( this1 cannot be that1 or this2 cannot be that2 && value == 0) how does this work?

edited October 2013 in How To...

if j is the same as row then i camnnot be same as cel if i is the same as cel then j cannot be same as row

this in 1 if statment

Answers

  • Answer ✓

    (1) if j is the same as row then i cannot be same as cel (2) if i is the same as cel then j cannot be same as row

    boolean condition1 = (j==row) && (i!=cel)
    boolean condition2 = (i==cel)  && (j!=cel)
    if (condition1 && condition2) {
    println ("true");
    }
    

    you can replace condition1 and condition2:

    if ( ((j==row) && (i!=cel))&& 
         ((i==cel)  && (j!=cel)) ) {
    println ("true");
    }
    

    Greetings !

  • Answer ✓

    You explanation is about as clear as mud - but if I understand it you want the result to be false when j == row and i == cel at the same time in which case it is

    if(!(j == row && i == cel)) {
    
    }
    
  • oehh nice! thnx .. i was thinkin way to hard :)

  • edited October 2013

    W/o wrapping the expression condition w/ ! operator: <:-P

    if (j != row | i != cel) {}
    
  • edited October 2013

    use logical OR (||) rather than bitwise OR (|) when doing this. with bitwise OR both parts are evaluated before the answer and this can trip you up. the above is safe but something like

    if (a == null | a.something() == null)

    will throw an exception if a is null whereas

    if (a == null || a.something() == null)

    won't. the bugs this'll cause isn't worth the 1 letter's worth of typing you save.

  • edited October 2013

    The bugs this'll cause isn't worth the 1 letter's worth of typing you save.

    Alas! Once more I gotta explain and defend its usage! /:)

    I believe most humans would naturally expect that any expression would be evaluated to its fullest.
    What would trip a programmer up is the opposite of it! @-)

    Indeed, shortcut evaluation is an advanced feature that most programming languages nowadays have.
    Being an advanced trick, there's a slightly performance execution hit for setting that up.
    But of course, that'd pay off later on if redundant (and possibly slower) term evaluations would be avoided completely! :bz

    However, for an expression entirely consisting of variables and literals, the time spent to set up a shortcut
    could be surprisingly longer than merely evaluating everything raw! $-)

    Besides, if (a == null || a.something() == null) is a special case and demands the usage of advanced techniques.
    As a programmer, I'll choose what is best to use for each case!!! >:)

  • If you really want to get rid of the ! operation then use this

    if(j != row || i != col) {
    
    }
    

    it performs exactly the same as my previous post.

    I have used the logical OR because as a programmer, I'll choose what is best to use in each case!!! ;)

  • And that choice is always the same! >:)

  • edited October 2013

    And that choice is always the same!

    No my choice would not always the same! I would use the bitwise operator if, and only if, I needed both sides to be evaluated. In this case I don't so I use the logical operator.

    Consider the statement

    if(j != row || i != col) {
    }
    

    This is probably in a double loop processing a 2D array.

    To simplify the maths lets assume it is a 10x10 array. Now the j != row will be true 90% of the time which means that the second part i != col has only to be evaluated 10% of the time. If I use your code

       if(j != row | i != col) {
       }
    

    then the second part has to be evaluated 100% of the time.

    Of course as a programmer I know that if the number of cols > number of rows then it would be better to do

    if(i != col || j!= row) {
    }
    

    Since shortcut evaluation is a compiler optimisation, any setup time would be done at compile-time not at run-time. At run-time any benefit of evaluating all the terms (and I have not seen evidence of any performance benefit) would be swamped by the evaluation of unnecessary terms.

Sign In or Register to comment.