Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: A touchy subject (good for beginners)

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default A touchy subject (good for beginners)

    This is oddly enough the reason I stopped using some other forum. Of course this was when I first started with java long ago. Well here is the subject first:

    A snippet from my infinite textbox code:

    do {
    g.drawString(content[iii],x+letterW*ii,y+letterH*i);
    ++ii;
    if(boundX < ii) {
    ii = 0; ++i;      }
    ++iii;
                   }while(iii < Array.getLength(content) || i > boundY);

    Now the thing that got me stuck - keep in mind this is not the exact example because in this case as simple && would suffice where as my original (can't remember that far back) would have been more specific - is this could easily give you a run-time error of ArrayIndexOutOfBounds. If I can just cut to the end ..

    The logic of the do while is:
    Call this option A)
    "continue while true"

    And it is not:
    we'll call this option B)

    "Break when false"

    Believe it or not this was the start of never ending debate. The simple fact is java at runtime analyzes conditions between the logical or operator individually for efficiency so java does this.

    while( something || somethingElse )

    it first say's is something true?

    CASE 1 something is not true

    A) continue to the next condition

    B) break

    CASE 2 something is true

    A) loop back up

    B) check the next condition



    I bring this up for two reasons, One, it is a good thing for new programmers to know it will help them avoid some serious mishaps, Two, I am kind of curious how you fellows take this.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: A touchy subject (good for beginners)

    I think you're saying that in a multi-condition conditional statement, Java only evaluates those conditions necessary to make a decision. I think a multi-AND situation demonstrates the idea more simply. In a multi-AND conditional statement, all conditions must be TRUE in order for the entire statement to be true. If any of the conditions are found false, the rest of the conditions are not evaluated. As an example:

    if ( case1 && case2 && case3 )

    If case1 is found to be false case2 and case3 are not evaluated.

    Is that your point? If so, what was the controversy (I don't really care, but you're welcome to vent if you'd like), and are you expecting some controversy now?

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: A touchy subject (good for beginners)

    Quote Originally Posted by KAJLogic View Post
    Believe it or not this was the start of never ending debate.
    I do not see how this can be a debate, especially not a long one...
    Give values to the variables in a SSCCE: ~end of discussion, winner takes all.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: A touchy subject (good for beginners)

    Exclusive-and (&&) always short-circuit on the first condition found to be false (evaluation order is from left to right).

    Exclusive-or (||) always short-circuit on the first condition found to be true (evaluation order is again from left to right).

    public class Test
    {
    	public static boolean get(boolean val)
    	{
    	    System.out.println("get(" + val + ")");
    	    return val;
    	}
     
    	public static void main(String[] args)
    	{
    		System.out.println("res1");
    	    boolean res1 = get(false) && get(true);
    	    System.out.println("res2");
    	    boolean res2 = get(true) || get(false);
    	}
    }

    Output:

    res1
    get(false)
    res2
    get(true)
    This means code like this is perfectly safe:

    if(str != null && str.length() > 5)
    {
        // do something
    }

  5. #5
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Re: A touchy subject (good for beginners)

    The guts of this post are that there is a BIG difference between break if false and continue while true. And, I was just curious if this was an infectious argument or something. The fact that there needs to be no debate is the reason I hope to be apart of this community for a long time. Honestly if a new comer asked me about the difference between the two I would say obviously it is not break when, because the statement is do WHILE. So to sum up the whole point is, there is very simple, there is a HUGE logical difference between continue while true is found or break when false is found. That is what they others refused to/couldn't understand, and the reason I lost trust in that forum (this was long ago).

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: A touchy subject (good for beginners)

    I hope you can be happy here. You obviously have studied the details and like to share them.

Similar Threads

  1. Problem with the PDF file attachment subject
    By Tim84 in forum Java Theory & Questions
    Replies: 1
    Last Post: June 17th, 2013, 08:16 AM
  2. Today is a very good day and I'm in a good mood
    By AHefphern in forum The Cafe
    Replies: 0
    Last Post: May 15th, 2013, 03:10 AM
  3. what project would be good for beginners? [opinions needed]
    By thisbeme in forum Java Theory & Questions
    Replies: 2
    Last Post: March 4th, 2012, 10:14 AM
  4. How to get File Properties like Subject , commnet
    By MVK in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 26th, 2011, 12:59 PM
  5. For loop with a string as the subject
    By Fluidz in forum Loops & Control Statements
    Replies: 4
    Last Post: June 20th, 2011, 07:50 AM