This discussion is archived
3 Replies Latest reply: Aug 6, 2012 5:46 AM by 953448 RSS

OBJ 2.2: Continue statement

javaMan2012 Newbie
Currently Being Moderated
Hi folks...

I was trying out the exerise 5.2 from the Kathy Serirra's SCJP 6 study guide. It is about the labelled continue statement. I seem to have run into some trouble. Following is the question from the book:

" Try creating a labeled while loop. Make the label outer and provide a condition to
check whether a variable age is less than or equal to 21. Within the loop, increment
age by one. Every time the program goes through the loop, check whether age is 16.
If it is, print the message "get your driver's license" and continue to the outer loop. If
not, print "Another year."
■ The outer label should appear just before the while loop begins.
■ Make sure age is declared outside of the while loop. "

Following is my code:
public class LabelledWhile {

    public static void main(String[] args) 
    {
        int age = 0;

        outer:
        while (age < 21)
        {
            age++;
            if (age == 16) 
            {
                System.out.println("get your driver's license");
               
                continue outer;
            } 
             
            else 
            {
                System.out.println("Another Year");
            }

        }

        
    }
    
}
This is the result:

italics
Another Year
Another Year
Another Year
Another Year
Another Year
Another Year
Another Year
Another Year
Another Year
Another Year
Another Year
Another Year
Another Year
Another Year
Another Year
get your driver's license
Another Year
Another Year
Another Year
Another Year
Another Year
italics


This is the result without the labelled outer:

italics
Another Year
Another Year
Another Year
Another Year
Another Year
Another Year
Another Year
Another Year
Another Year
Another Year
Another Year
Another Year
Another Year
Another Year
Another Year
get your driver's license
Another Year
Another Year
Another Year
Another Year
Another Year
italics

Both the results are the same. Is there anything wrong with my code? Has my code properly addressed the question? Hope someone can help? Thanks.

regards
John
  • 1. Re: OBJ 2.2: Continue statement
    EJP Guru
    Currently Being Moderated
    Try putting 'age++;' at the end instead of the beginning and you will see a difference.
  • 2. Re: OBJ 2.2: Continue statement
    953811 Newbie
    Currently Being Moderated
    Yes, your observation is right. You should be getting the same result with and without the 'continue 'statement since there is only one 'if and else' condition in the while loop. So, when age = 16, 'if ' condition will be executed and will come out of the loop even without the 'continue '. Hence continue statement is redundant in this case.

    Edited by: howler on Aug 3, 2012 7:10 PM
  • 3. Re: OBJ 2.2: Continue statement
    953448 Newbie
    Currently Being Moderated
    Hi java-Man ,

    There is nothing wrong with your code except it seems you didn't understand the concept of labels well. Labels are use to denote the boundaries for blocks of code. Labels are usually used in case you have multiple nested loops and you want to start(continue) or break the particular loop nestead loops. Example

    Outer:
    for(int i=0; i < intArray.length; i++)
    {
    for(int j=0; j < intArray.length ; j++)
    {
    if(intArray[i][j] == 3)
    continue Outer;
    System.out.println("Element is : " + intArray[i][j]);
    }
    }

    Hope this helps.

    Thanks,
    Happy Coding :)

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points