Question:
(JAVA)variable declaration outside then initialization inside FOR statement?
Sd
2013-07-24 06:16:06 UTC
I'm just wondering if is this possible and will not return error:

int i= 1;
FOR (i=1; i < 10; i++){
statements;
}

is this the same as:

FOR (int i = 1; i < 10; i++){
statements;
}


thanks
Six answers:
McFate
2013-07-24 07:32:42 UTC
Other than i being visible outside the loop, it's equivalent. Also, you don't have to repeat the i=1 in the loop; you can just leave the first part blank:



int i = 1;

for (; i < 10 ; i++) ...



@M
MarkF120
2013-07-24 07:42:18 UTC
It would have been less work for you to try this than it was for you to ask this question, think about that, now down to business:



There is no such thing as a FOR statement in Java. Its "for" (lower case), Java is case sensitive, the code you posted will not even compile.



These will compile:



int i= 1;

for (i=1; i < 10; i++){

//statements;

}



for (int j = 1; j < 10; j++){

//statements;

}
James Bond
2013-07-24 06:26:09 UTC
int i= 1;

for(i=1; i < 10; i++){

statements;

}



Now, if you check, i value is 10. That is, i scope is still active.



for(int i = 1; i < 10; i++){

statements;

}

After the for loop, i is undefined or i's scope is over. You can not access i.
?
2013-07-24 06:17:25 UTC
It is possible, however in the case of the first one, the int i will persist past the life of the for loop holding the final value of the counter (in this case 10)
sin
2016-12-29 20:30:40 UTC
How specific are you that there is easily an interior or an exterior? it rather is extra rather comprehensible if one considers the easily shape of an atom and the dimensions and site of its factors. If one takes under consideration the actuality that the neutrons and protons style a dense cluster on the midsection of the atom and that the electrons orbit in the form of trend that huge areas exist between them and the nucleus it is going to become sparkling that the atoms that make up possible good gadgets are made up of ninety 9+ % empty area at any given 2nd. This by myself does not look too important until you upload the thought that the atoms that make up many possible good gadgets are extra of a unfastened conglomeration that share an identical charm yet not at all rather touch one yet another. in the commencing up look this does not rather look appropriate, yet closer diagnosis properly-knownshows that this provides an marvelous volume of empty area to good gadgets that are already made up of atoms that must be referred to as ninety 9 % area. whilst so-referred to as good gadgets are considered in this gentle it is going to become obvious that may not be the possible good gadgets they seem to us to be. We ourselves are actually not exceptions to this phenomenon. those possible good gadgets are extra like ghostly pictures that we interpret as good gadgets in step with our perceptual conclusions. From this one ought to end that conception is a few form of a trick that facilitates us to take those ghostly pictures and turns them right into a international we are in a position to affiliate and work together with. This clever device seems to be a creation of our mind that helps us to work together with one yet another in what seems to be a three dimensional certainty. i want to characteristic that it rather is in step with my own own way of observing the placement and grew to become into not at all meant to be a physics lesson. Love and advantages Don
2013-07-24 07:36:06 UTC
The only difference is that in first case you can use "i" in whole program and in secound only in that FOR loop.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...