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 4 of 4

Thread: While (return value will terminate an iteration or loop?)

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default While (return value will terminate an iteration or loop?)

          while (true) {
     
                System.out.print("Your Guess: ");
                input = scanner.nextInt();
     
                if (LOWER_BOUND <= input && input <= UPPER_BOUND) {
     
                    return input;
                }

    this is a chunk of the whole code that i've written, a simple development exercise in the book

    my question is....is this part will terminate a loop?

      if (LOWER_BOUND <= input && input <= UPPER_BOUND) {
     
                    return input;

    im a bit curious about a returning value... when this statement is statisfied? will the loop stop?


    if there something unclear about my codes. let me know , ill post the whole code for you...


  2. #2
    Junior Member
    Join Date
    Dec 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: While (return value will terminate an iteration or loop?)

    No it will not terminate the loop unless you insert a "break;"

      if (LOWER_BOUND <= input && input <= UPPER_BOUND) {
     
                    return input;
                    break;
    Thanks,
    Saula

    Need more help?
    http://homeworkjava.com/

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

    Default Re: While (return value will terminate an iteration or loop?)

    Return statements do return out of "infinite loops" (though, technically this makes those loops not infinite loops anymore )

    However, more than the loop will be escaped from. The method will be terminated and the value immediately returned.

    As a side note, throwing exceptions can break out of loops/methods so long as they are caught outside the loop

    try
    {
         while (true)
         {
              // break out of this while loop
              throw new Exception();
         }
    }
    catch(Exception e)
    {
         System.out.println("The loop was escaped from!");
    }

    As a sidenote, that logic is inefficient.

    Assuming that UPPER_BOUND is always bigger than LOWER_BOUND, it's more efficient to only check if the value is lower than LOWER_BOUND.
    Last edited by helloworld922; April 26th, 2010 at 11:58 PM.

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: While (return value will terminate an iteration or loop?)

    Yes, whenever you call return you end the method stack and return the value specified, no matter where you are.

    If you wish to break out of loops but stay in the method, have a look at the "break" keyword and labels.

    // Json

Similar Threads

  1. Help - Return to menu screen
    By throzen in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 29th, 2009, 01:44 PM
  2. tax return program..help finish please!!
    By dscrudato21xo in forum Loops & Control Statements
    Replies: 2
    Last Post: November 5th, 2009, 03:23 PM
  3. Prime number generator program missing return statement
    By 03EVOAWD in forum What's Wrong With My Code?
    Replies: 13
    Last Post: September 10th, 2009, 09:17 AM
  4. Error of "Class has no return statement"
    By mdstrauss in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 2nd, 2009, 12:00 PM
  5. Error of missing return statement while implementing Array
    By MS_Dark in forum Collections and Generics
    Replies: 1
    Last Post: December 10th, 2008, 03:18 PM