Associate
- Joined
- 6 Nov 2003
- Posts
- 991
- Location
- Redditch
Sorry DJ_Jestar, I don't follow? Can you elaborate.
Cheers
Cheers
it's a bit like goto. everyone bangs on about "don't use got". almost every set of company coding standards i've ever seen forbid them. and fair enough, you never need to use goto. there's always another way. but then, every time you do switch, break, continue, if, else, for, while... you're really just doing a goto. they all compile to some sort of JMP instruction. you can screw up with any of them.
for (int i=0; i<10; i++) {
for (int x=0; x<10; x++) {
if (x = 5)
goto end;
}
} end:
for (int i=0; i<10; i++) {
bool break_outer_loop;
if (break_outer_loop)
break;
else {
for (int x=0; x<10; x++) {
if (x = 5)
break_outer_loop = true;
}
}
}
Sorry DJ_Jestar, I don't follow? Can you elaborate.
Cheers
Break bool's are usually a code smell. There are few instances (if any) that I'd use one.Depends on how you use goto, it can be used in some rare situations like this:
Code:for (int i=0; i<10; i++) { for (int x=0; x<10; x++) { if (x = 5) goto end; } } end:
In my opinion is more efficient than:
*snip*
I still wouldn’t use goto though because its frowned upon.
for (int i = 0; i < 10; i++) {
for (int x = 0; x < 10; x++) {
if (x == 5) break;
}
if (x == 5) break;
}
It's a "shortcut" syntax, rather than an explicit global. Java's JVM will look at a variable and say "So it's not a local/temp variable, it must be a property" and if it still doesn't find it, will throw an exception (or wobbly as dynamic typed developers like to call it).
private String _foo;
public String Foo {
get { return _foo; }
set { _foo = value; }
}
if (tempVars.containsKey(theVariableName)) {
return tempVars.get(theVariableName);
} elseif (instVars.containsKey(theVariableName)) {
return instVars.get(theVariableName);
} else {
throw new Wobbly();
}
public class Test
{
private String [COLOR="Red"]name[/COLOR];
public Test(String [COLOR="Magenta"]name[/COLOR])
{
[COLOR="Yellow"]this.name = name;[/COLOR]
}
}
public class Test
{
private String name;
public Test(String name1)
{
name = name1;
}
}
Like itCode:throw new Wobbly();
would be my preference; ignoring of course that there is no reason to iterate i in the first placeCode:for (int i = 0; i < 10; i++) { for (int x = 0; x < 10; x++) { if (x == 5) break; } if (x == 5) break; }
![]()
Quite.I was meaning in a situation where you had a loop within a loop and you needed to exit the outer loop from within the inner loop based on a condition that could only be checked within the inner loop. My example didn’t show this very well I must admit.
Its not a very common situation anyway.