# Thread: multiply variable in for loop

1. Member
Join Date
May 2011
Posts
39
Rep Power
0

## multiply variable in for loop

Hi,

how can I multiply the value of x for each loop please

Java Code:
`for (int x=2; x <=20; [B]x*2[/B])`

2. Originally Posted by aconti
Hi,

how can I multiply the value of x for each loop please

Java Code:
`for (int x=2; x <=20; [B]x*2[/B])`
Have a look at the *= operator-assignment operator; i.e x*=2 does the job, but there are more ways to multiply the value of a variable by 2, pick your choice:

Java Code:
```x*= 2; // the preferred one
x+= x; // duh, simple but it looks a bit stupid
x<<= 1; // if you want to look like a bit-hacker
x-=-x; // if you want to look like a weirdo
x-=~x+1; // totally nuts```
and I'm sure there are many more possibilities ...

kind regards,

Jos

3. Originally Posted by JosAH
I'm sure there are many more possibilities ...
Puh-leaze! you sent me to the JLS via the tutorials with that last one.

db

4. Originally Posted by DarrylBurke
Puh-leaze! you sent me to the JLS via the tutorials with that last one.

db
Oh dear, don't look now:

Java Code:
`x-=1+~++x+1;`
kindest regards,

Jos ;-)

5. You owe me for the operation to correct my newly acquired squint.

db <---why does that look like bd?

6. Member
Join Date
May 2011
Posts
39
Rep Power
0
can you explain the below :)

x-=1+~++x+1;

7. Is your psychotherapist where you can get to him, fast?

db

8. Originally Posted by aconti
can you explain the below :)

x-=1+~++x+1;
No, that line was left as an exercise for the reader.

kind regards,

Jos

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•