Skip to Main Content

OBJ 2.2: Continue statement

javaMan2012Jul 12 2012 — edited Aug 6 2012
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
Comments
Post Details
Added on Jul 12 2012
3 comments
1,966 views