why am i not getting output when I make "j<0" at line 1 in a below code?

when I make "j<=0" output is shown, but why am I not getting when I make "j<0" at line 1.

public class Test {


public static void main(String[] args) {
    // TODO Auto-generated method stub
     int i,j;

    for( i=0,j=0;i<3 & j<0;i++,j--)//line 1
    {

        System.out.println(i+"..."+j);
    }


}
}

3 answers

  • answered 2017-06-17 20:01 shiftpsh

    j < 0 means that j has to be smaller than 0, not equal. j <= 0 is okay with equals.

    For the loop to succeed j < 0 has to be true but since j is initialized with value 0, j < 0 is not true, so the loop will basically just won't do anything.

  • answered 2017-06-17 20:01 Meikiem

    because your condition by j<0 is always false. you are starting j from 0, so it will never be true to decrease j even for onetime.

  • answered 2017-06-17 20:01 chocksaway

    This code allows me to test your logic (i'm guessing that this is what you want to do) "correctly:"

    public class Loop {
        public static void main(String[] args) {
            int i = 0;
            int j = 0;
    
            for (i = 0; i < 3; i++) {
                if (i < 3 && j < 0) {
                    System.out.println(i + "..." + j);
                }
                j--;
            }
        }
    }
    

    When I run this I get:

    1...-1
    2...-2
    

    j is less than 0 (-1) when i is 1

    j is less than 0 (-2) when i is 2