Message Board
Message Board > Programming > Explanation of a for loop?.. |
July 3, 2012, 19:01 | |
Zomg
None 641 posts |
Honestly I'm stuck on this one. Apparently the following explanation of a for loop in java is incorrect: for (A;B;C) {D} simply means: A; while (B) { D; C;} It makes sense to me. Can someone explain to me why this is incorrect? Someone told me that 'the variables of A are in a different scope'. Wtf? Aren't they in the same local scope? And 'continue does not jump over C'. Well honestly that's too idiomatically explained. Can anyone explain this for me and what's wrong here? Thanks. ____________ |
# |
July 3, 2012, 20:51 | |
Dennis
どこかにいる 2092 posts |
well, in the "while" example the variable gets defined before the while-scope and in the "for" example, the variable is defined in a higher scope. Java code: // 1st example void testMethod1() { i = -38; // error: this variable is not declared within the scope for (int i=0;i<10;i++) { doStuff(); } } // 2nd example void testMethod2() { int i = 0; while(i<10) { doStuff(); i++; } } Is that what you mean? You cannot define the "i" variable in the while-loop declaration. Everything between { } is a new scope. Of course this is a trick question, since you can simply initialize pre-declared variables in a for-loop: Java code: // 1st example void testMethod3() { int i = -38; for (int i=0;i<10;i++) { doStuff(); } } ____________ Kwakkel |
# |
July 3, 2012, 21:16 | |
PB
Defender of the faith 630 posts |
I do think both points are valid though. I know there has been a bug in a C++ compiler in Visual Studio once where the scope of the initialization part of the for loop would be the scope outside the for loop, but this wás in fact a bug. I think the confusion might be with DIV, which is limited to LOCAL scoped variables as the finest scope. In more advanced programming languages, the scope of a variable is from when it's declared until the end of the code block in which it was declared. Code blocks often are created using { and } characters. Also the statement about using continue is true. Continue wil go directly back to the while statement, skipping C. For instance try the following two javascript loops: Javascript code: for(var a = 1;a<10;a++) { alert(a); continue; } alert(a) Javascript code: var a = 1; while(a < 10) { a++; continue; alert(a); } alert("done"); As you can see the second loop will not execute alert(a);, which would be a++ in your example. I didn't do that, because that would cause an infinite loop... [Edited on July 3, 2012 by PB] ____________ |
# |
July 3, 2012, 21:35 | |
Zomg
None 641 posts |
Hmmm. I thought in Java there were the following scopes: (global? dunno) private protected public local (= method scope) The while loop is working with the same reference, so. What I think was meant with A; while (B) { D; C;} is: Code: int a=0; while(a<10){ do(); a++; } Does this mean that a++; will not be executed in Java? I guess I could try it in my IDE. ____________ |
# |
July 4, 2012, 00:46 | |
PB
Defender of the faith 630 posts |
private, protected and public are Access Modifiers, not scopes. You can not access a variable outside of it's scope, basically because you do not know if it even exists (and probably it doesn't) in that part of the code. However you can declare variables outside of a function, for instance on a public class. This means, if this would be it, that any code that has an instance of that object would be able to access that variable. To have control over who can access the variable, you can use access modifiers. For instance a private variable, does have a global scope (it exists as long as the object exists), but can only be accessed from code within the object itself. A public variable can be accessed from any code that has a reference to the object. A protected variable can only be accessed from code that runs in the same namespace. Ok, having said this, in short: Access Modifiers do not have anything to do with the scope of the variables declared in a for loop. Hope it clears it up a bit, I haven't read it through for a second time... ____________ |
# |
July 4, 2012, 08:59 | |
Zomg
None 641 posts |
If you declare a variable private outside a function in a class, that means no one with an instance to that class would have access to it. Same thing with making the constructor private (=not able to create objects from it). I assume though that you meant public. I know all this basic knowledge. Heck, I'm programming for thread safety in web applications. I'm doing much more serious things (like making a thread safe Yahtzee web application (that takes at least a month. I already spent half a day defining functionality pieces in my requirements elicitation)). Ok so like Dennis said, everything between {} is a new scope? So that means that a variable declared and instantiated between those {} would be accessible to anything within those {}. Yes? That continue statement was a hack anyway lol. Now my question is this: Disregarding the continue statement (i.e. it's not there), would my initial definition of ABCD be correct? Unless 'continue' is an implicit instruction in Java. Thanks ____________ |
# |
July 4, 2012, 09:01 | |
Dennis
どこかにいる 2092 posts |
The access modifiers are to maintain visibility and control data encapsulation on a functional level. Even a private variable can be accessed throughout the whole class if it is declared on the class level. Basically a scope is determined with { and } outside the { and } where the variable is declared, it cannot be accessed. I must say it's easier to understand in PL/SQL where each "block" has its "declare" section then it's more obvious you cannot use it outside the block. PL/SQL code: BEGIN -- scope A DECLARE outer VARCHAR2(10); BEGIN outer := 'test'; -- outer is within its scope -- scope B DECLARE a VARCHAR2(10); BEGIN outer := outer || 'ing'; -- outer is within its scope a := 'Scope B'; -- a is within its scope END; DBMS_OUTPUT.put_line(a); -- ERROR: out of scope END; DBMS_OUTPUT.put_line(outer); -- ERROR: out of scope END; [Edited on July 4, 2012 by Dennis] ____________ Kwakkel |
# |
July 4, 2012, 09:16 | |
Zomg
None 641 posts |
I must say nested declare-begin-end blocks are confusing for me.
____________ |
# |
July 4, 2012, 20:02 | |
PB
Defender of the faith 630 posts |
Quoting Zomg: I assume though that you meant public. I can't find where I said private where you assume I meant global? Not to nit pick, just to check we're on the same wavelength. Quoting Zomg: I know all this basic knowledge. Heck, I'm programming for thread safety in web applications. I'm doing much more serious things (like making a thread safe Yahtzee web application (that takes at least a month. I already spent half a day defining functionality pieces in my requirements elicitation)). No problem, I work with experts all the time. Experts often have really good knowledge about all sorts of stuff, but just as often lack/forget knowledge about some of the details of basic stuff... So when I talk about basic stuff, I really don't mean that I estimate you're on that level in general. Also it's been some time since I paid serious attention to the Java programming language, so I'm a little bit rusty myself here. Quoting Zomg: Ok so like Dennis said, everything between {} is a new scope? So that means that a variable declared and instantiated between those {} would be accessible to anything within those {}. Yes? Anything is a dangerous word here, but in general: yes. Quote: That continue statement was a hack anyway lol. Now my question is this: Disregarding the continue statement (i.e. it's not there), would my initial definition of ABCD be correct? Unless 'continue' is an implicit instruction in Java. Thanks Same goes for break/return or, forgive me for saying, GOTO... The point is, that the second construction is different. The "C;" could stand for code that effects something more important than increasing the counter which has no value after the break statement. If C for instance is "i+=SafeChanges()", then SafeChanges() could sometimes be called one time less. Then this difference would be more important. [Edited on July 4, 2012 by PB] ____________ |
# |
July 5, 2012, 07:58 | |
Dennis
どこかにいる 2092 posts |
Just a quick note: A global is not the same as public variable. A global is a piece of memory that is declared over an entire program while a public variable still belongs to a specific class, though it can be accessed from everywhere.
____________ Kwakkel |
# |
July 5, 2012, 21:31 | |
PB
Defender of the faith 630 posts |
Well, in Java, if you want a variable to be global over an entire program (independant of if the code has a reference to an instance of an object), you can use static variables for that...
____________ |
# |
Message Board > Programming > Explanation of a for loop?..