Discussions
Categories
- 196.7K All Categories
- 2.2K Data
- 235 Big Data Appliance
- 1.9K Data Science
- 449.8K Databases
- 221.6K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 549 MySQL Community Space
- 477 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 532 SQLcl
- 4K SQL Developer Data Modeler
- 186.9K SQL & PL/SQL
- 21.3K SQL Developer
- 295.4K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.1K Development Tools
- 104 DevOps
- 3.1K QA/Testing
- 645.9K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 153 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 17 Java Essentials
- 158 Java 8 Questions
- 85.9K Java Programming
- 79 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.2K Java SE
- 13.8K Java Security
- 203 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 395 LiveLabs
- 37 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.6K Other Languages
- 2.3K Chinese
- 170 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 230 Portuguese
OBJ 2.2: Continue statement

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:
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
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
-
Try putting 'age++;' at the end instead of the beginning and you will see a difference.
-
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 -
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.