We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Init Variable Not Visible Inside For Loop
Page Index Toggle Pages: 1
Init Variable Not Visible Inside For Loop (Read 1009 times)
Init Variable Not Visible Inside For Loop
Feb 13th, 2010, 2:34pm
 
Hi Everyone!
I'm trying to write a quick method that prints out the contents of an ArrayList.  It's called printMethod, and it takes an ArrayList as a variable.

My problem:
To print out the list I thought I could just use a For loop, but when I try to compile & run the program I get an error saying it can't see "j".  The variable I use to loop through the For loop. Huh
Below is my code and error...

I'm sure I'm doing something wrong, can anybody tell me what it is?????

Many Thanks in Advance!!!!!  Smiley

Here's my code:


int[] deckOrdered = {1,2,3,4,5,6,7,8,9,10,11,12,13,14};
ArrayList oldDeck = new ArrayList();

void setup()
{
  // Take the ordered deck and copy it to the "oldDeck" ArrayList.
  for(int i = 0; i < 14; i=i+1)
  {
    oldDeck.add(deckOrdered[i]);
  }
  // Now I want to see if it copied correctly.
  printDeck(oldDeck);
}

void printDeck(ArrayList deck)
{
  //To make sure the ArrayList was passed correctly, I check its size.
  println("deck.size() = " + deck.size());
  // Now loop through the ArrayList and print out each entry.
  for(int j = 0; j < deck.size(); j++);
  {
    print(deck.get(j) + ", ");
  }
}

Here's my Error:

...
print(deck.get(j) + ", ");
...

Cannot find anything named "j"
Re: Init Variable Not Visible Inside For Loop
Reply #1 - Feb 13th, 2010, 3:01pm
 
Your problem is here:

Quote:
 for(int j = 0; j < deck.size(); j++);
 {


(semi-colon after the for loop declaration).

I personally prefer to open the brace on the same line as the loop statement, method declaration etc. - to me that makes more sense and avoids these types of typos:

Code:
  for(int j = 0; j < deck.size(); j++) {
 // ...
}

void setup () {

}

// etc...


Of course this is just a coding style.  A lot of people place the opening brace on a new line - it's obviously a recognised approach and I'm sure someone will give a good reason for doing it.  Whatever - watch out for those typos: the interpreter isn't always clever enough to spot them Wink

Re: Init Variable Not Visible Inside For Loop
Reply #2 - Feb 13th, 2010, 7:53pm
 
blindfish wrote on Feb 13th, 2010, 3:01pm:
A lot of people place the opening brace on a new line - it's obviously a recognised approach and I'm sure someone will give a good reason for doing it.

Thoughts:
Code:

if (braceOnNewLine)
{
  whitespaceSeparation = true; // clarity
  bracesAlignVertically = true; // visual matching, symmetry
}

if (!braceOnNewLine && blahBlahBlah &&
   someMultiLineCondition == true) {
  whitespaceSeparation = false;
  bracesAlignVertically = false;
  potentiallyAvoidingSpuriousEmptyStatement = true; // good point
}


A compiler could generate a warning for empty statements following for(), if(), while(), and so on, but it probably isn't usual.

Sometimes you don't want/need a statement (especially in cases where the loop header has side effects); it is probably a good idea to put a comment to indicate this is on purpose in such cases, or write as, something like:

Code:
for (type var = object.init(); var.foo(); var.bar()) {} 



It is an unfortunate 'fact of life' that programming requires attention to details like a missing (or extra!) punctuation character. Other languages have other things to watch out for. For example, Python doesn't use braces, it uses indentation, which means if the indentation is wrong the program is wrong. Bah!

-spxl
Re: Init Variable Not Visible Inside For Loop
Reply #3 - Feb 14th, 2010, 1:43am
 
Sun coding style recommendation is to follow K&R Indent style.
Personally, I try and follow (as much as it is possible) the same style I chose years ago, in all programming languages I use.
So I put an initial capital letter to function names (yes, I think that's Microsoft style... But I chose it because it was pleasing for me) and I align the braces because I find it visually more pleasing/readable/easier to track.
Now, should I make a library, the official API would use lower case initials, and if I hack somebody else's code, I just follow the coding conventions used in the source I find.
Computer programming field is full of such senseless discussions (senseless because it leads nowhere, as good arguments are given on each side) such as indentation style, braces against 'end', tabs vs. spaces, initial capitals or not, and so on.

The empty statement problem is coming from an unfortunate trick possible from C times, where compact coding was seen as cool. I recall I was impressed, coming from Pascal world, to see lines like:
for (i = 0; x = readchr(); i++); (pseudo code)
although a good coder would put the semi-colon on a line of its own to make it standing out, or, better, would put the readchr call in the statement.
So, for (); {} is alas syntactically correct (loop with empty statement followed by an arbitrary block of code). At least, in Java, strong typing of boolean makes if (a = b) incorrect in most cases!
Re: Init Variable Not Visible Inside For Loop
Reply #4 - Feb 14th, 2010, 2:15am
 
subpixel - that makes some sense, but I look for alignment on the beginnings of statements and line the closing brace up with the beginning of the line...  Actually looking at PhiLho's link it seems that's 1TBS, which I probably picked up from ActionScript.  I personally don't like the extra lines used up by putting the opening brace on a new line and seeing a statement that doesn't end with a semi-colon or brace on the same line troubles me...   Shocked

But as PhiLho says these discussions are interesting, but ultimately fruitless: it's for each of us to learn the style that suits us in our own projects (or to match that used in a shared project) and to be consistent...  I think I mentioned the style simply because IIRC that approach helped me avoid this type of error when I was starting out.

As for Python - perverse perhaps - but I rather like it (though admittedly have only ever used it for short programmes).  VB on the other hand...   Lips Sealed
Re: Init Variable Not Visible Inside For Loop
Reply #5 - Feb 14th, 2010, 1:58pm
 
Thank you all so much!!!  I'm horrible at catching those silly mistakes sometimes.  It works great now.   Smiley
Cheers!!!
Page Index Toggle Pages: 1