# problem with loop

• 12-31-2011, 02:17 PM
pietr0
problem with loop
Hi!

Assume that I have 5 variables: a1,...,a5.

I wonder if it is possible to do a loop like that:
Code:

```for(int i=1; i<=5; i++){ a[i] = "whatever"; }```
I would like to change all my variables "a". Can you help me?
• 12-31-2011, 02:23 PM
Norm
Re: problem with loop
Quote:

I would like to change all my variables "a"
You have an array named: a that can hold some values (don't know how many because you do not show the size of the array).

Where are the variables: a1,a2, etc How are those variables related to the array?
What do you mean by 'change all my variables "a"'?
• 12-31-2011, 03:40 PM
ozzyman
Re: problem with loop
Quote:

Originally Posted by pietr0
Assume that I have 5 variables
...
Code:

```for(int i=1; i<=5; i++){ a[i] = "whatever"; }```

Array indexes start at 0 so your loop should also start at 0. Or you should use
array[i-1] = ...
to reference the array item.

And yes you can change all the values to A exactly how you've written it except changing Whatever to A.
• 12-31-2011, 05:14 PM
pietr0
Re: problem with loop
Quote:

Originally Posted by Norm
You have an array named: a that can hold some values (don't know how many because you do not show the size of the array).

Where are the variables: a1,a2, etc How are those variables related to the array?
What do you mean by 'change all my variables "a"'?

There is no array.
I just want to change values of those int variables (named: a1, ..., a5) at the same time.
I do not want to change their values individually. Perhaps in some place in my program I need to "reset" value of every variable (a5=10 and I need a5=0...).

I wonder if there is possibility to "reset" their values using a loop instead of write individually a1=0; a2=0; ... ; a5=0 (and in some extreme cases a10000=0;) ??
I'm just curious...
• 12-31-2011, 05:21 PM
Norm
Re: problem with loop
Quote:

I just want to change values of those int variables (named: a1, ..., a5) at the same time.
This will change all those variables in one statement:
a0=a1=a2=a3=a4=a5=0;
• 12-31-2011, 05:24 PM
JosAH
Re: problem with loop
Quote:

Originally Posted by pietr0
There is no array.
I just want to change values of those int variables (named: a1, ..., a5) at the same time.
I do not want to change their values individually. Perhaps in some place in my program I need to "reset" value of every variable (a5=10 and I need a5=0...).

I wonder if there is possibility to "reset" their values using a loop instead of write individually a1=0; a2=0; ... ; a5=0 (and in some extreme case a10000=0;) ??
I'm just curious...

It seems as if you are afraid of arrays; an array 'a' is like a block of houses with house numbers 0, 1, 2, 3 etc. so a[0] is a house, a[1] is a house, etc. and each house serves as a single variable. You can set each variable to, say, 42, like this:

Code:

```for (int i= 0; i < a.length; i++)   a[i]= 42;```
You can't use a loop with individual variables a0, a1, a2 etc. in Java

kind regards,

Jos
• 12-31-2011, 05:48 PM
ozzyman
Re: problem with loop
Add the items to an array like this:

int[] a = new int[] { a1, a2, a3, a4, a5 };

So you'll be able to loop through the values with
a[i] = ...
where 0 <= i <= 4
• 12-31-2011, 05:58 PM
Norm
Re: problem with loop
@ozzyman
??? What is your code supposed to do?