• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

scope of variables in functions

 
tmwhalens
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my problem is this:
i am comparing two arrays with a for loop. in each iteration, if there is a match i want to increment iCorrect by one. if there is NOT a match i want to increment iWrong by one. however, after the loops have terminated i can not access the values of variables iCorrect and iWrong. why? must i use an array for storing the number of correct and incorrect responses?
thanx,
thomas
PS. i am sticking in some code to show that i have attemtpted this problem:



[This message has been edited by tmwhalens (edited November 08, 2001).]
[This message has been edited by tmwhalens (edited November 08, 2001).]
[This message has been edited by tmwhalens (edited November 08, 2001).]
 
Junilu Lacar
Bartender
Pie
Posts: 7747
60
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas,
Thanks for making the effort to format your code to make it nice and readable
I can't see anything in the code that would make it print incorrect results and my own test of this code gives me the correct results.
Some nit-picking:
- instead of naming the arrays "arrayA" and "arrayB" then adding comments about what they are, just name them "answers" and "responses". Makes the code much more readable.
- instead of hard-coding the size of the array (in this case 5, but that could change, right?), either declare a static final int NUM_QUESTIONS and use that throughout the code or just declare and initialize in the same statement, e.g.
int[] answers = {1, 2, 3, 4, 5};

- In the for-loop, it is better to use the actual array.length in the loop condition, e.g.:
for (int i = 0; i < arrayA.length; i++) { ... }
This way, if you decide to change the array size, you'll have less code to change.
Good luck!
Junilu
 
Thomas Whalen
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu,
Thanks for the advice! Is it better to use the ArrayName.length property in a function << or >> use that property outside of the function? I was wondering if it might make a difference in regards to overall speed, even though speed is not going to be a factor for such a simple, small program.
Thanks,
Thomas
 
Junilu Lacar
Bartender
Pie
Posts: 7747
60
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas,
Not sure if this is what you mean by "in" or "out" of a function but I'll take a stab:
<pre>
// is this better...
// #1
int len = array.length;
for (i = 0; i < len; i++) {...}
// ...than this?
// #2
for (i = 0; i < array.length; i++) {...}
</pre>
I don't think you gain anything with #1. Plus, you have an additional temporary variable. In his book "Refactoring", Martin Fowler often advises against using temporary variables like this and actually has a Refactoring called "Replace Temp with Query", which basically refactors #1 to #2.

------------------
Junilu Lacar
Sun Certified Programmer for the Java� 2 Platform
 
Thomas Whalen
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Junilu! I will work on keeping excessive temporary variables to a minimum
Thomas
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is nothing wrong with the code. Try putting print statements where iCorrect and iWrong are incremented to see which block is being hit.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic