Results 1 to 13 of 13
  1. #1
    Join Date
    Apr 2008
    Posts
    2
    Rep Power
    0

    Default Why the output is always zero

    why in the below example the value of i is not incrementing. Output is always zero o.
    The ++ operator have higher precedence. So this should first increment the value of i and then use is the rule.

    class Demo
    {
    public static void main(String args[])
    {
    int i = 0;
    if(i > 0 && ++i < 10)
    System.out.println("Inside if" );
    System.out.println(i);
    }
    }

  2. #2
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    Quote Originally Posted by mehrotra.chitij View Post
    why in the below example the value of i is not incrementing. Output is always zero o.
    The ++ operator have higher precedence. So this should first increment the value of i and then use is the rule.

    class Demo
    {
    public static void main(String args[])
    {
    int i = 0;
    if(i > 0 && ++i < 10)
    System.out.println("Inside if" );
    System.out.println(i);
    }
    }
    it should be

    Java Code:
    class Demo
    {
    	public static void main(String args[])
    	{	
    		for(int i = 0; i < 10; i++)
                    {
                           System.out.println(i);
                    }
    	}
    }
    output:
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9

  3. #3
    Join Date
    Apr 2008
    Posts
    2
    Rep Power
    0

    Default

    This is not what i am asking. See the question again.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Hi mehrotra.chitij,

    Welcome to our community. :)

    You just check a condition, and do some processing. Doesn't loop your process there.

    What actually you want to do, is that Zosden says?

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  6. #6
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Quote Originally Posted by mehrotra.chitij View Post
    The ++ operator have higher precedence. So this should first increment the value of i and then use is the rule.

    if(i > 0 && ++i < 10)

    Hello Pal,

    In if condition : you used logical AND operator and it checks like that..

    If left condition returns false it never execute right condition
    since your right condition is never executed (i is not greater than 0 if you put i >= 0 then it will be executed) and that's why it always prints ZERO.

    If you use bitwise AND (&)......It always executes both left and right.

    Use it and see the difference.......

    if(i > 0 & ++i < 10)




    sanjeev,संजीव

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    And you are make the condition in wrong way pal. There you don't have use {} for if clause. In such a case only the first line enclosed with if clause.

  8. #8
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    Quote Originally Posted by mehrotra.chitij View Post
    class Demo
    {
    public static void main(String args[])
    {
    int i = 0;
    if(i > 0 && ++i < 10)
    System.out.println("Inside if" );
    System.out.println(i);
    }
    }
    What this is doing line by line

    int i = 0; // is setting the variable i equal to 0;

    if(i > 0 && ++i < 10) // is checking if i > 0 and i + 1 < 10

    System.out.println("Inside if" ); // if the above line is true then it prints out "inside if", which it is not because i is not greater than zero it is zero.

    System.out.println(i); // prints out i which is zero.

    this is not a loop but just a check if you give me more details on what you want this to do then be more specific

  9. #9
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    if(i > 0 && ++i < 10)

    Additional detail ( hope this would add )

    the "()" of a if-statement should be true in order to proceed on its subtask....else, dont....

    In logic, here is the truth table...

    Let say, 0 is false and 1 is true.

    @ AND table
    A B Output
    0 0 0
    0 1 0
    1 0 0
    1 1 1

    @ OR table,
    A B Output
    0 0 0
    0 1 1
    1 0 1
    1 1 1

    Now, since i = 0;
    (i > 0) is false, ( ++i < 10 ) is true... bec, @ that moment, i is 1 when compare to 10 and it is always true.....unless it will be looped for about 9 times....

    so now, the if statement is like,

    if ( 0 AND 1 )... by looking at the table, 0 and 1 is 0 means false....

    It will not proceed on its subtask(just my term)....
    Since no statements are inclosed under the if statement, the JVM assumes that the first statement that will be encountered should be part of the if-statement's subtask......
    freedom exists in the world of ideas

  10. #10
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    There are tow types of AND operator in Java.

    bitwise AND (&)
    logical AND (&&)

    Both use the truth table given by sukatoa. but here the difference between is

    If we use (&&) in if statement:
    Then if left condition is false ....compiler will not check for right part

    If we use (&) in if statement:
    Then if left condition is false .... compiler still check for right part


    sanjeev,संजीव

  11. #11
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    If we use (&) in if statement:
    Then if left condition is false .... compiler still check for right part
    Absolutely.....
    freedom exists in the world of ideas

  12. #12
    SCS17 is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default

    Quote Originally Posted by sukatoa View Post
    Absolutely.....
    I see what you guys mean here.. I studied those things in Discrete Math some time ago but in the case of the bit-wise &.. whats the use of checking the right side if the left side is false..???
    The statement will evaluate to false either ways.

    Thanks.

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Logically what you say is right. If the right side is false checking the left side is useless, because result is false whatsoever.

    Ok, say right side is true. Defiantly you have to check the left side. Because the result depends on that.

    In compiler generating such a logic is wasting resources. I mean it deal with all the scenarios meet in the processing.

Similar Threads

  1. Writing an XML output
    By JThangiah in forum XML
    Replies: 2
    Last Post: 03-27-2008, 05:15 PM
  2. Output to excel
    By abhiN in forum New To Java
    Replies: 2
    Last Post: 03-07-2008, 02:19 AM
  3. output
    By Camden in forum New To Java
    Replies: 3
    Last Post: 12-01-2007, 11:34 PM
  4. How to redirect the output
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 10:30 PM
  5. get the output from whoami
    By gary in forum Advanced Java
    Replies: 2
    Last Post: 06-12-2007, 02:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •