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: Question about do loops?

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Question about do loops?

    in the case of:

     
    int low = 10 , high =0 
     
    do 
    { 
     
        System.out.println( low); 
        low++; 
    }
    while (low <= high );


    answer says that the loop is infinite, and will continue to produce values until int low cannot hold a big enough value, but my question is, why would the loop continue to be evaluated if low is 10 and high is 0, 10 is not <= 0, so wouldn't the loop stop after the first execution, and not continue to be evaluated?


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

    Default Re: Question about do loops?

    What answer says that? Provide a link to a reference if you can.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question about do loops?

    this is the answer in the back of the book:

    6.10:

    the code contains an infinite loop. The numbers 10, 11, 12 and so on will be printed until the program is terminated or until the number gets to large to be held by the int variable low.

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

    Default Re: Question about do loops?

    Books can contain mistakes.

  5. #5
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Question about do loops?

    Quote Originally Posted by chakana101 View Post
    this is the answer in the back of the book:

    6.10:

    the code contains an infinite loop. The numbers 10, 11, 12 and so on will be printed until the program is terminated or until the number gets to large to be held by the int variable low.
    As what you have said, loop should start after the first execution. so I guess you understand what the above program really do.
    and if you have doubts on your observation just copy it and paste it in your main method, compile and execute.
    actually it will not compile since the declaration of int does not end with semicolon, but fixing that I'm sure you will noticed that it only execute just once not infinite and prints 10. Maybe the book you are reading is not a good book. you might want to throw it in trash can LOL

    The numbers 10, 11, 12 and so on will be printed until the program is terminated or until the number gets to large to be held by the int variable low.
    that statement is another proof that the book you are reading is not a good book to be read in java.
    When the number gets to large to be held by the int data type, it will not make your program terminated or crashed. Actually it can still hold by the int variable, but it will have negative values, (I'm not sure if it is the two's compliment)
    try to do this:
    // putting the maximum value of int into max variable
    int max = Integer.MAX_VALUE;
    // then print it
    System.out.println(max); // it prints the maximum integer an int can hold
    //increment the max variable
    max++;
    /* as what the book says, it would terminate your program right? but actually, it would not. */
    //print it
    System.out.println(max);
    /* this print statement will print the max variable, but you will notice that it has negative value

    I'm not if it is the the two's compliment of the value, but you can do some experimentation if you are curious about it.

  6. #6
    Junior Member
    Join Date
    Mar 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question about do loops?

    I see lol glad to know i'm not crazy haha, thanks for the help guys! and it's actually my textbook haha

Similar Threads

  1. Question about do-while loops?
    By Scorks in forum Java Theory & Questions
    Replies: 2
    Last Post: April 11th, 2013, 07:08 AM
  2. Java Loops Question
    By ThatBeast in forum Java Theory & Questions
    Replies: 1
    Last Post: December 6th, 2012, 11:42 PM
  3. Question on do-while loops
    By Sean448 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 24th, 2012, 01:06 AM
  4. Beginner question about for loops...
    By ninjaBob in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 29th, 2012, 03:57 PM
  5. Loops & Series Question
    By nuttaay in forum Loops & Control Statements
    Replies: 5
    Last Post: November 29th, 2011, 01:46 PM