Forum Stats

  • 3,816,106 Users
  • 2,259,142 Discussions
  • 7,893,390 Comments

Discussions

OBJ 2.2: Continue statement

javaMan2012
javaMan2012 Member Posts: 17
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

Answers

  • EJP
    EJP Member Posts: 32,920 Gold Crown
    Try putting 'age++;' at the end instead of the beginning and you will see a difference.
  • 953811
    953811 Member Posts: 2
    edited Aug 3, 2012 10:10PM
    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
  • 953448
    953448 Member Posts: 1
    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 :)
This discussion has been closed.