
-----------------------------------
Naveg
Fri Sep 23, 2005 5:44 pm

Structured break; and continue; statements
-----------------------------------
I've been reading through a few pages of Java exercises and have come across this one:

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]

-----------------------------------
rizzix
Fri Sep 23, 2005 6:13 pm


-----------------------------------
fill me in... what do they mean by "structured statements"?

-----------------------------------
wtd
Fri Sep 23, 2005 6:25 pm


-----------------------------------
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".

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:

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.

-----------------------------------
rizzix
Fri Sep 23, 2005 6:33 pm


-----------------------------------
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: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...

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
        }
    }
}

-----------------------------------
rizzix
Fri Sep 23, 2005 6:42 pm


-----------------------------------
a not so recommended solution.. (more like a hackish way of going about with things 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) {}

-----------------------------------
wtd
Fri Sep 23, 2005 6:46 pm


-----------------------------------
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:

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)
}

-----------------------------------
rizzix
Fri Sep 23, 2005 6:47 pm


-----------------------------------
yep cleaner.. (and efficient) :)

-----------------------------------
[Gandalf]
Fri Sep 23, 2005 9:11 pm


-----------------------------------
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 :).

-----------------------------------
wtd
Fri Sep 23, 2005 9:14 pm


-----------------------------------
"]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 :).

The second part of the "for" loop is simply a boolean expression.  It can be any boolean expression.

-----------------------------------
Aziz
Fri Oct 07, 2005 10:26 pm


-----------------------------------
"]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 :).

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

for (int i = 0; i < 10; i++) {
     //some code
}

would be the same as

for (int i = 0; cont; i++) {
     cont = true;
     if (i < 10) {
          //some code
     } else {
          cont = false;
     }
}

Sorry, getting carried away :P

-----------------------------------
[Gandalf]
Fri Oct 07, 2005 10:31 pm


-----------------------------------
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.

-----------------------------------
wtd
Fri Oct 07, 2005 10:39 pm


-----------------------------------
"]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.
