Computer Science Canada

Structured break; and continue; statements

Author:  Naveg [ Fri Sep 23, 2005 5:44 pm ]
Post subject:  Structured break; and continue; statements

I've been reading through a few pages of Java exercises and have come across this one:

code:
A common criticism of the break statement and the continue statement is that each is unstructured. Actually, break statements and continue statements can always be replaced by structured statements, though doing so may be awkward. Describe in general how you would remove any break or continue statement from a loop and replace that statement with some structured equivalent.


Now, I've though of just simply failing the loop continuation condition, but of course that would only exit the loop when the condition is next tested. What if the break is in the middle of the loop? How would one go about using structured statements to exit the loop in such a case?[/code]

Author:  rizzix [ Fri Sep 23, 2005 6:13 pm ]
Post subject: 

fill me in... what do they mean by "structured statements"?

Author:  wtd [ Fri Sep 23, 2005 6:25 pm ]
Post subject: 

Well, let's say you're looping over an array of strings, which represent names. Let's use "continue" to skip over the name "Clarence".

code:
for (int nameIndex = 0; nameIndex < names.length; nameIndex++)
{
   if (names[nameIndex].equals("Clarence"))
      continue;

   System.out.println(names[nameIndex]);
}


Or we could avoid the "continue" with:

code:
for (int nameIndex = 0; nameIndex < names.length; nameIndex++)
{
   if (!names[nameIndex].equals("Clarence"))
      System.out.println(names[nameIndex]);
}


If you'd like to see a "break" example, I can work on that in a bit.

Author:  rizzix [ Fri Sep 23, 2005 6:33 pm ]
Post subject: 

oh ic.. yea.. break will be a little messy... one way is to modify the variable i, but that's very ugly (although efficient)... the other way involves a one-time-set boolean flag which basically tests the whole block of code to be executed against that flag for each iteration of the for-loop... well something like this:
Java:
boolean cont = true;
for (int i = 0;  i < 10;  i++) {
    if (cont) {
        // all code goes here
        // (if you need to break just set cont to false)
    }
}


edit: if the break is somewhere in the middle...

Java:
boolean cont = true;
for (int i = 0;  i < 10;  i++) {
    if (cont) {
        // all code goes here
        // some more code here....
        // a cont = false here... (the break)
        if (cont) { // the rest of the code is skipped (since we test it again)
            //some code here
        }
    }
}

Author:  rizzix [ Fri Sep 23, 2005 6:42 pm ]
Post subject: 

a not so recommended solution.. (more like a hackish way of going about with things [not cool, not cool at all])

Java:
try {
    for (int i = 0;  i < 10;  i++) {
        // code goes here
        if (some_condition)
             throw new Exception(); // the break
        // code goes here
    }
} catch (Exception e) {}

Author:  wtd [ Fri Sep 23, 2005 6:46 pm ]
Post subject: 

rizzix wrote:
Java:
boolean cont = true;
for (int i = 0;  i < 10;  i++) {
    if (cont) {
        // all code goes here
        // (if you need to break just set cont to false)
    }
}


Or possibly:

Java:
boolean cont = true;
for (int i = 0;  i < 10 && cont;  i++) {
     // all code goes here
     // (if you need to break just set cont to false)
}

Author:  rizzix [ Fri Sep 23, 2005 6:47 pm ]
Post subject: 

yep cleaner.. (and efficient) Smile

Author:  [Gandalf] [ Fri Sep 23, 2005 9:11 pm ]
Post subject: 

wtd wrote:
Java:
boolean cont = true;
for (int i = 0;  i < 10 && cont;  i++) {
     // all code goes here
     // (if you need to break just set cont to false)
}

Interesting for loop structure, good to know, good to know Smile.

Author:  wtd [ Fri Sep 23, 2005 9:14 pm ]
Post subject: 

[Gandalf] wrote:
wtd wrote:
Java:
boolean cont = true;
for (int i = 0;  i < 10 && cont;  i++) {
     // all code goes here
     // (if you need to break just set cont to false)
}

Interesting for loop structure, good to know, good to know Smile.


The second part of the "for" loop is simply a boolean expression. It can be any boolean expression.

Author:  Aziz [ Fri Oct 07, 2005 10:26 pm ]
Post subject: 

wtd wrote:
[Gandalf] wrote:
wtd wrote:
Java:
boolean cont = true;
for (int i = 0;  i < 10 && cont;  i++) {
     // all code goes here
     // (if you need to break just set cont to false)
}

Interesting for loop structure, good to know, good to know Smile.


The second part of the "for" loop is simply a boolean expression. It can be any boolean expression.


I was actually thinking about something like that earlier, with file reading, either read a set amount of lines, or until a certain line is reached (such as "$$$END"). I didn't use it, but it was interested because most people wouldn't think about it. I remember from reading The Java Tutorial that the second part is the "Continuation Condition" so you could put anything that would go into a loop in there. In fact

Java:
for (int i = 0; i < 10; i++) {
     //some code
}


would be the same as

Java:
for (int i = 0; cont; i++) {
     cont = true;
     if (i < 10) {
          //some code
     } else {
          cont = false;
     }
}


Sorry, getting carried away Razz

Author:  [Gandalf] [ Fri Oct 07, 2005 10:31 pm ]
Post subject: 

Ah, I remember Cervantes mentioning that Turing only checks the bounds of a for loop once. I guess the same isn't true for Java.

Author:  wtd [ Fri Oct 07, 2005 10:39 pm ]
Post subject: 

[Gandalf] wrote:
Ah, I remember Cervantes mentioning that Turing only checks the bounds of a for loop once. I guess the same isn't true for Java.


Of course not, since Turing's for loops and Java's for loops work completely differently.


: