Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Structured break; and continue; statements
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Naveg




PostPosted: 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]
Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: Fri Sep 23, 2005 6:13 pm   Post subject: (No subject)

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




PostPosted: Fri Sep 23, 2005 6:25 pm   Post subject: (No 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.
rizzix




PostPosted: Fri Sep 23, 2005 6:33 pm   Post subject: (No 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
        }
    }
}
rizzix




PostPosted: Fri Sep 23, 2005 6:42 pm   Post subject: (No 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) {}
wtd




PostPosted: Fri Sep 23, 2005 6:46 pm   Post subject: (No 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)
}
rizzix




PostPosted: Fri Sep 23, 2005 6:47 pm   Post subject: (No subject)

yep cleaner.. (and efficient) Smile
[Gandalf]




PostPosted: Fri Sep 23, 2005 9:11 pm   Post subject: (No 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.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Fri Sep 23, 2005 9:14 pm   Post subject: (No 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.
Aziz




PostPosted: Fri Oct 07, 2005 10:26 pm   Post subject: (No 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
[Gandalf]




PostPosted: Fri Oct 07, 2005 10:31 pm   Post subject: (No 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.
wtd




PostPosted: Fri Oct 07, 2005 10:39 pm   Post subject: (No 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.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to:   


Style:  
Search: