Hi,

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

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

Printable View

- 06-02-2011, 03:44 PMacontimultiply 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, 03:52 PMJosAH
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

kind regards,

Jos - 06-02-2011, 04:32 PMDarrylBurke
- 06-02-2011, 04:52 PMJosAH
- 06-02-2011, 05:22 PMDarrylBurke
You owe me for the operation to correct my newly acquired squint.

db <---why does that look like bd? - 06-02-2011, 05:25 PMaconti
can you explain the below :)

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

db - 06-03-2011, 01:24 PMJosAH