# Help with Algorithms "ContinueWithLabeledBreakDemo"

Page 1 of 1

## 6 Replies - 785 Views - Last Post: 29 July 2009 - 05:37 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=117322&amp;s=46eae5e97a34aec41195d5adde81f1a7&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 bigNasir

Reputation: 1
• Posts: 13
• Joined: 10-June 09

# Help with Algorithms "ContinueWithLabeledBreakDemo"

Posted 28 July 2009 - 07:06 PM

I am new to java programing and algorithms I was going through the the java tutorial and was quite proud of myself as I was surviving chapt. 3 then I came across this demo and began to lose my footing
Can you please help me by explaining these few lines of code in the ContinueWithLabelDemo?(from the Java Tutorial 4th Edition) correct me if i am wrong thanks . Oh yeah the (?) means if my note is incorrect, then please explain or clarify else comment if you wish.

class ContinueWithLabelDemo {
public static void main(String[] args) {
String searchMe = "Look for a substring in me"; //String literal declaration instantiated
String substring = "sub"; //String literal declaration instantiated
boolean foundIt = false; //Boolean Primitive data type instantiated

int max = searchMe.length() - substring.length(); // This evaluates to a numerical integer value of 23 (Y/N)?
test:
for (int i = 0; i <= max; i++) { // This means: at this time integer i's numerical value is 0 and it will remain so until the next iteration of this loop then the value for i will be 1 and so on (Y/N)?
int n = subString.length(); // This means that integer n evaluates numerically to 3. (Y/N)?
int j = i; //This means integer J evaluates to the numerical value of 0 and will not evaluate to 1 until the next iteration of this loop. (Y/N)?
int k = 0;
while (n-- != 0) { //This means that while integer n-- (meaning 3=3-1) evaluates to not equal to 0 (Y/N)?
if (searchMe.charAt(j++) != substring.charAt(k++)) { // Meaning search for particular substring of text to match the it's parent as long as the test returns in-equal increment the variable J & K by 1(Y/N)?
continue test; // Continue the test if the previous statement evaluates to true(Y/N)? Can you reword the statement "skips the current iteration of the outer loop marked with the given label to describe what is happening here"?

}
}
foundIt = true;
break test; // Discontinue the test ?? How does this effect the outer loop?

}

System.out.println(foundIt ? "Found it" : // If then else construct (Y/N)?
"Didn't find it");

}

}

OUTPUT: Found it

Is This A Good Question/Topic? 0

## Replies To: Help with Algorithms "ContinueWithLabeledBreakDemo"

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: Help with Algorithms "ContinueWithLabeledBreakDemo"

Posted 28 July 2009 - 07:45 PM

first Welcome at DIC
second:

An use indentetion

These are disguise goto
The break statement is used to exit a loop
The continue statement is used to go to to the next iteration of a loop

```for(int i = 0; i < 10; i++) {
if(certain condition)
break;					  // that will exit from the loop even if i != 9
if(other condition)
continue;				  // that will go to the beginning of the loop with i++
do other stuff here
}

```

now suppose you have 2 loops one within the other but you want to break and/or continue the outher loop

```test_loop:
for(int i = 0; i < 10; i++) {
if(condition1) break;		// that will exit the for loop
if(condition2) continue;	// that will go to i++ of the for loop
while(otherCondition) {
if(condition3) break;		// that will exit the while loop... the for loop will continue
if(condition4) continue;	// that will go back to the while
....
if(condion5) break test_loop;	   // that will break the loop that follow test_loop: so exit from the for loop
if(contion6) continue text_loop;	// tha will continue the loop following test_loop: so next iteration of the for

```

This is considered by a majority of people as a very bad practice and you will rarely see it in real code

The preferred way of doing that is to set a boolean

```for(int i = 0; i < 10; i++) {
boolean exitLoop = false;
for(int j = 0; j < 10; j++) {
...
if(condition) {
exitLoop = true;
break;
}
....
}  // end of inner loop
if(exitLoop)   // if inner loop asked me to break
break;	  // do it
}

```

### #3 NeoTifa

• Whorediot

Reputation: 2971
• Posts: 16,312
• Joined: 24-September 08

## Re: Help with Algorithms "ContinueWithLabeledBreakDemo"

Posted 28 July 2009 - 07:53 PM

Ha! He has a method call searchme Epic win.

### #4 Locke

• Sarcasm Extraordinaire!

Reputation: 526
• Posts: 5,604
• Joined: 20-March 08

## Re: Help with Algorithms "ContinueWithLabeledBreakDemo"

Posted 28 July 2009 - 07:54 PM

NeoTifa, on 28 Jul, 2009 - 08:53 PM, said:

Ha! He has a method call searchme Epic win.

No no...just a String.

No functions are there.

### #5 bigNasir

Reputation: 1
• Posts: 13
• Joined: 10-June 09

## Re: Help with Algorithms "ContinueWithLabeledBreakDemo"

Posted 29 July 2009 - 09:38 AM

Locke, on 28 Jul, 2009 - 06:54 PM, said:

NeoTifa, on 28 Jul, 2009 - 08:53 PM, said:

Ha! He has a method call searchme Epic win.

No no...just a String.

No functions are there.

```class ContinueWithLabelDemo {
public static void main(String[] args) {
String searchMe = "Look for a substring in me";			//String literal declaration instantiated
String substring = "sub";													//String literal declaration  instantiated
boolean foundIt = false;												   //Boolean  Primitive data type instantiated

int max = searchMe.length() - substring.length();	// This evaluates to a numerical integer value of  23 (Y/N)?
test:
for (int i = 0; i <= max; i++) {								   // This means: at this time integer i's numerical value is 0 and it will remain so until the next iteration of this loop then the value for i will be 1 and so on (Y/N)?
int n = subString.length();									   // This means that integer n  evaluates numerically to  3. (Y/N)?
int j = i;																	  //This means integer J evaluates to the numerical value of 0 and will not evaluate to 1 until the next iteration of  this loop. (Y/N)?
int k = 0;
while (n-- != 0) {												 //This means that while integer n-- (meaning 3=3-1)  evaluates to not equal to 0 (Y/N)?
if (searchMe.charAt(j++) != substring.charAt(k++)) { // Meaning search for particular substring of text to match the it's parent as long as the test returns in-equal increment  the variable J & K by 1(Y/N)?
continue test;  // Continue the test if the previous statement evaluates to true(Y/N)? Can you reword the statement "skips the current iteration of the outer loop marked with the given label to describe what is happening here"?

}
}
foundIt = true;
break test;   // Discontinue the test ?? How does this effect the outer loop?

}

System.out.println(foundIt ? "Found it" : // If then else construct (Y/N)?
"Didn't find it");

}

}

```

```class ContinueWithLabelDemo {
public static void main(String[] args) {
String searchMe = "Look for a substring in me";			//String literal declaration instantiated
String substring = "sub";													//String literal declaration  instantiated
boolean foundIt = false;												   //Boolean  Primitive data type instantiated

int max = searchMe.length() - substring.length();	// This evaluates to a numerical integer value of  23 (Y/N)?
test:
for (int i = 0; i <= max; i++) {								   // This means: at this time integer i's numerical value is 0 and it will remain so until the next iteration of this loop then the value for i will be 1 and so on (Y/N)?
int n = subString.length();									   // This means that integer n  evaluates numerically to  3. (Y/N)?
int j = i;																	  //This means integer J evaluates to the numerical value of 0 and will not evaluate to 1 until the next iteration of  this loop. (Y/N)?
int k = 0;
while (n-- != 0) {												 //This means that while integer n-- (meaning 3=3-1)  evaluates to not equal to 0 (Y/N)?
if (searchMe.charAt(j++) != substring.charAt(k++)) { // Meaning search for particular substring of text to match the it's parent as long as the test returns in-equal increment  the variable J & K by 1(Y/N)?
continue test;  // Continue the test if the previous statement evaluates to true(Y/N)? Can you reword the statement "skips the current iteration of the outer loop marked with the given label to describe what is happening here"?

}
}
foundIt = true;
break test;   // Discontinue the test ?? How does this effect the outer loop?

}

System.out.println(foundIt ? "Found it" : // If then else construct (Y/N)?
"Didn't find it");

}

}

```

### #6 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: Help with Algorithms "ContinueWithLabeledBreakDemo"

Posted 29 July 2009 - 04:34 PM

Ignore that chapter in your training manual
No OOP programmer would use thisIsALabel: in real code but:

Steeve McConnell Code Complete 2nd Edition Chapter 16.2 Controlling loop

Use the labeled break structure if your language supports it Java supports use of labeled breaks to prevent the kind of problem experienced with the New York City telephone outage. A labeled break can be used to exit a for loop, an if statement, or any block of code enclosed in braces (Arnold, Gosling, and Holmes 2000).

Here's a possible solution to the New York City telephone code problem, with the programming language changed from C++ to Java to show the labeled break:

Java Example of a Better Use of a Labeled break Statement Within a

```do-switch-if Block

do {
...
switch
...
CALL_CENTER_DOWN:
if () {
...
break CALL_CENTER_DOWN;	   <-- 1
...
}
...
} while ( ... );

(1)The target of the labeled break is unambiguous.

```

Use break and continue only with caution Use of break eliminates the possibility of treating a loop as a black box. Limiting yourself to only one statement to control a loop's exit condition is a powerful way to simplify your loops. Using a break forces the person reading your code to look inside the loop for an understanding of the loop control. That makes the loop more difficult to understand.

Use break only after you have considered the alternatives. You don't know with certainty whether continue and break are virtuous or evil constructs. Some computer scientists argue that they are a legitimate technique in structured programming; some argue that they aren't. Because you don't know in general whether continue and break are right or wrong, use them, but only with a fear that you might be wrong. It really is a simple proposition: if you can't defend a break or a continue, don't use it.

### #7 bigNasir

Reputation: 1
• Posts: 13
• Joined: 10-June 09

## Re: Help with Algorithms "ContinueWithLabeledBreakDemo"

Posted 29 July 2009 - 05:37 PM

pbl, on 29 Jul, 2009 - 03:34 PM, said:

Ignore that chapter in your training manual
No OOP programmer would use thisIsALabel: in real code but:

Steeve McConnell Code Complete 2nd Edition Chapter 16.2 Controlling loop

Use the labeled break structure if your language supports it Java supports use of labeled breaks to prevent the kind of problem experienced with the New York City telephone outage. A labeled break can be used to exit a for loop, an if statement, or any block of code enclosed in braces (Arnold, Gosling, and Holmes 2000).

Here's a possible solution to the New York City telephone code problem, with the programming language changed from C++ to Java to show the labeled break:

Java Example of a Better Use of a Labeled break Statement Within a

```do-switch-if Block

do {
...
switch
...
CALL_CENTER_DOWN:
if () {
...
break CALL_CENTER_DOWN;	   <-- 1
...
}
...
} while ( ... );

(1)The target of the labeled break is unambiguous.

```

Use break and continue only with caution Use of break eliminates the possibility of treating a loop as a black box. Limiting yourself to only one statement to control a loop's exit condition is a powerful way to simplify your loops. Using a break forces the person reading your code to look inside the loop for an understanding of the loop control. That makes the loop more difficult to understand.

Use break only after you have considered the alternatives. You don't know with certainty whether continue and break are virtuous or evil constructs. Some computer scientists argue that they are a legitimate technique in structured programming; some argue that they aren't. Because you don't know in general whether continue and break are right or wrong, use them, but only with a fear that you might be wrong. It really is a simple proposition: if you can't defend a break or a continue, don't use it.