Results 1 to 8 of 8
  1. #1
    aconti is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default 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. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by aconti View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    Quote Originally Posted by JosAH View Post
    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. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by DarrylBurke View Post
    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 ;-)
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

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

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

  6. #6
    aconti is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default

    can you explain the below :)

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

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

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

    db

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by aconti View Post
    can you explain the below :)

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

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 10
    Last Post: 05-11-2011, 03:26 AM
  2. Accessing a variable from a loop
    By mhz041986 in forum New To Java
    Replies: 4
    Last Post: 04-04-2011, 09:49 AM
  3. Replies: 4
    Last Post: 05-18-2009, 03:23 PM
  4. Variable not initialized after while loop
    By ejs7597 in forum New To Java
    Replies: 6
    Last Post: 02-28-2009, 06:00 AM
  5. [SOLVED] Referencing Variable within Loop
    By Judoon_Platoon in forum Java Applets
    Replies: 2
    Last Post: 05-22-2008, 12:54 PM

Posting Permissions

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