# multiply variable in for loop

• 06-02-2011, 02:44 PM
aconti
multiply variable in for loop
Hi,

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

Code:

`for (int x=2; x <=20; [B]x*2[/B])`
• 06-02-2011, 02:52 PM
JosAH
Quote:

Originally Posted by aconti
Hi,

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

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:

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
• 06-02-2011, 03:32 PM
DarrylBurke
Quote:

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
• 06-02-2011, 03:52 PM
JosAH
Quote:

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:

Code:

`x-=1+~++x+1;`
kindest regards,

Jos ;-)
• 06-02-2011, 04:22 PM
DarrylBurke
You owe me for the operation to correct my newly acquired squint.

db <---why does that look like bd?
• 06-02-2011, 04:25 PM
aconti
can you explain the below :)

x-=1+~++x+1;
• 06-02-2011, 06:17 PM
DarrylBurke
Is your psychotherapist where you can get to him, fast?

db
• 06-03-2011, 12:24 PM
JosAH
Quote:

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