• Post Reply Bookmark Topic Watch Topic
  • New Topic

For loops - variables outside?  RSS feed

 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created a basic for loop and outside the loop I called on one of the variablea (rand1) but got an error, does this mean that variables in the loop cannot be used outside the loop? And yes I am well aware that I should know this by now, but it is taking longer than I thought to even understand the basics of programming. I am frustrated indeed.

The ERROR SAY "rand1 cannot be resolved as a variable."

 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct.

Variables are only in scope for the block they are declared in.
So rand1 where it is declared now is only valid within the braces of the for loop.

If you want to access it outside of the for loop, you have to move your declaration of the variable outside the loop.

 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can you retrieve the information from inside the loop then? For example, if I wanted to use the value of "i" later on, how could I use it?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David De wrote:How can you retrieve the information from inside the loop then? For example, if I wanted to use the value of "i" later on, how could I use it?


You can't. If you want the variable at a scope that is larger than the block, then you need to declare the variable at a scope that is larger than the block.

Henry
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The quick example, what Henry just said:

Variable visible within loop only:
Variable visible within and outside loop:
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you that was helpful.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David De wrote:For example, if I wanted to use the value of "i" later on, how could I use it?

if this is your loop:

You could declare i outside the loop:


Of course, i will always be six when you exit the loop.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Of course, i will always be six when you exit the loop.

Well, it depends on the existing code within the while loop. If "brake;" statement would exist, not necessarily "i" would be 6.
Just clarified to make sure OP won't get confused.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
fred rosenberger wrote:Of course, i will always be six when you exit the loop.

Well, it depends on the existing code within the while loop. If "brake;" statement would exist, not necessarily "i" would be 6.
Just clarified to make sure OP won't get confused.

True...i was basing my statement off what was actually posted.

I suppose the better way of phrasing it would be something like "i will be whatever value causes the loop to exit - in the posted code, it would always be 6"
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:If "brake;" statement would exist, not necessarily "i" would be 6.
Just clarified to make sure OP won't get confused.


And to make doubly sure that the OP doesn't get confused, I think you meant "break", not "brake"
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Liutauras Vilda wrote:If "brake;" statement would exist, not necessarily "i" would be 6.
Just clarified to make sure OP won't get confused.


And to make doubly sure that the OP doesn't get confused, I think you meant "break", not "brake"

You're right, my mistake
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still trying to wrap my head around the hows and whys of methods. I can explain them to someone, but actually putting them in practice is another thing. So here is my code and I am getting an error because RAND1 is not defined, because it was defined in the FOR LOOP. So I tried to return RAND1 from the FOR LOOP, but it says that because my MAIN is VOID, it will not return.

So I am left with this
1) I need another METHOD that doesn't have VOID (not sure how to write this part)
2) I need the MAIN to just run the program and little else (not sure where to put this part)



 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David De wrote:I am still trying to wrap my head around the hows and whys of methods. I can explain them to someone
Good place to start - give a try. What is the method? Why you'd use them in your code? Do you see any advantages of them? Maybe disadvantages?

And it doesn't seem you understood above explained variables visibility scopes
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
… and this is a simple way to write a main method
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I fixed it to look like the below code, because I continued to get DUPLICATE variable errors (on the Rand1). I am not sure if this is what anyone else would do, but it worked and probably is redundant and could easily be fixed by someone with a brain.

1) I put the int rand1 = 0; outside the loop (which makes sense).
2) I was getting a problem of duplicates because my random generator was also a variable of rand1 so I tried to change it to something else and then back again.
3) trying out the method but still having issues, I wanted to have two methods but only came up with one for now.

 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You were close, amend it like this. And read about it in more details why HERE << link
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Avoid doing arithmetic with Math#random. I would recommend you create an instance of Random. Long discussion here. Using myRandom.nextInt(names.length); is much less error‑prone and the result matches array indices exactly.
Don't create findLength local variables. Use names.length throughout. There is no need for two variables to record the same value, and names.length is constant which findLength isn't.

If you reassign the array, names.length might be different, however. All places where names.length is used will immediately use the new value and all your code will work for the new array. If you have findLength, that value might be different from the size of the new array and you may suffer errors.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!