Refactoring a while loop without if .. else

Discussion in 'Java' started by -, May 13, 2005.

  1. -

    - Guest

    Is it possible to refactor the while loop into one method? The only way
    I can think of is to use if ... else when deciding the setSize but
    that would be inefficient.


    if (....) {
    while (thread == Thread.currentThread()) {
    setSize(x, getHeight());

    x--;

    if (x == SOME_VALUE) {
    thread = null;
    }
    }
    } else {
    while (thread == Thread.currentThread()) {
    setSize(getWidth(), y);

    y--;

    if (y == SOME_VALUE) {
    thread = null;
    }
    }
    }
    -, May 13, 2005
    #1
    1. Advertising

  2. -

    Wibble Guest

    I'll assume setSize has no side affects other than changing size...

    if (thread==Thread.currentThread() {
    if (...) setSize((x=SOME_VALUE)+1, getHeight());
    else setSize(getWidth(), (y=SOME_VALUE)+1);
    thread = null;
    }


    - wrote:
    > Is it possible to refactor the while loop into one method? The only way
    > I can think of is to use if ... else when deciding the setSize but
    > that would be inefficient.
    >
    >
    > if (....) {
    > while (thread == Thread.currentThread()) {
    > setSize(x, getHeight());
    >
    > x--;
    >
    > if (x == SOME_VALUE) {
    > thread = null;
    > }
    > }
    > } else {
    > while (thread == Thread.currentThread()) {
    > setSize(getWidth(), y);
    >
    > y--;
    >
    > if (y == SOME_VALUE) {
    > thread = null;
    > }
    > }
    > }
    Wibble, May 13, 2005
    #2
    1. Advertising

  3. -

    Wibble Guest

    Or did you want the infinite loop when x <= SOME_VALUE?

    Wibble wrote:
    > I'll assume setSize has no side affects other than changing size...
    >
    > if (thread==Thread.currentThread() {
    > if (...) setSize((x=SOME_VALUE)+1, getHeight());
    > else setSize(getWidth(), (y=SOME_VALUE)+1);
    > thread = null;
    > }
    >
    >
    > - wrote:
    >
    >> Is it possible to refactor the while loop into one method? The only
    >> way I can think of is to use if ... else when deciding the setSize
    >> but that would be inefficient.
    >>
    >>
    >> if (....) {
    >> while (thread == Thread.currentThread()) {
    >> setSize(x, getHeight());
    >>
    >> x--;
    >>
    >> if (x == SOME_VALUE) {
    >> thread = null;
    >> }
    >> }
    >> } else {
    >> while (thread == Thread.currentThread()) {
    >> setSize(getWidth(), y);
    >>
    >> y--;
    >>
    >> if (y == SOME_VALUE) {
    >> thread = null;
    >> }
    >> }
    >> }
    Wibble, May 13, 2005
    #3
  4. -

    Wibble Guest

    Or maybe setSize sets y & x equal to SOME_VALUE. Then the following
    should do:

    while(true);

    Wibble wrote:
    > Or did you want the infinite loop when x <= SOME_VALUE?
    >
    > Wibble wrote:
    >
    >> I'll assume setSize has no side affects other than changing size...
    >>
    >> if (thread==Thread.currentThread() {
    >> if (...) setSize((x=SOME_VALUE)+1, getHeight());
    >> else setSize(getWidth(), (y=SOME_VALUE)+1);
    >> thread = null;
    >> }
    >>
    >>
    >> - wrote:
    >>
    >>> Is it possible to refactor the while loop into one method? The only
    >>> way I can think of is to use if ... else when deciding the setSize
    >>> but that would be inefficient.
    >>>
    >>>
    >>> if (....) {
    >>> while (thread == Thread.currentThread()) {
    >>> setSize(x, getHeight());
    >>>
    >>> x--;
    >>>
    >>> if (x == SOME_VALUE) {
    >>> thread = null;
    >>> }
    >>> }
    >>> } else {
    >>> while (thread == Thread.currentThread()) {
    >>> setSize(getWidth(), y);
    >>>
    >>> y--;
    >>>
    >>> if (y == SOME_VALUE) {
    >>> thread = null;
    >>> }
    >>> }
    >>> }
    Wibble, May 13, 2005
    #4
  5. Hi,

    > Is it possible to refactor the while loop into one method? The only way
    > I can think of is to use if ... else when deciding the setSize but
    > that would be inefficient.


    I'll give an untested example (should work for non-negative x and y
    values) but have to mention, that I'm in doubt that this will be more
    efficient. It sets the size and decrements the corresponding x/y-values
    with respect to a given direction.

    So, let's assume the follow constants and instance variables;

    public static final int HORIZONTAL = 0xffffffff;
    public static final int VERTICAL = 0;
    private int direction = HORIZONTAL; // or VERTICAL

    Then one can rewrite the loops as follows:

    int xMask = direction;
    int yMask = ~xMask;

    int newX = x & xMask + getWidth() & yMask;
    int newY = y & yMask + getHeight() & xMask;

    int stopX = SOME_VALUE & xMask - 1 & yMask;
    int stopY = SOME_VALUE & yMask - 1 & xMask;

    while ( thread == Thread.currentThread() ) {
    setSize( newX, newY );

    newX -= 1 & xMask;
    newY -= 1 & yMask;

    if ( newX == stopX || newY == stopY )
    thread = null;
    }

    Bye
    Michael
    Michael Rauscher, May 13, 2005
    #5
  6. "-" <> schreef in bericht
    news:42840f4d$...
    > Is it possible to refactor the while loop into one method? The only way I
    > can think of is to use if ... else when deciding the setSize but that would
    > be inefficient.


    Unroll it:

    switch (x) {
    case 20: setSize(20, getHeight());
    case 19: setSize(19, getHeight());
    case 18: setSize(18, getHeight());
    case 17: setSize(17, getHeight());
    case 16: setSize(16, getHeight());
    case 15: setSize(15, getHeight());
    case 14: setSize(14, getHeight());
    case 13: setSize(13, getHeight());
    case 12: setSize(12, getHeight());
    case 11: setSize(11, getHeight());
    case 10: setSize(10, getHeight());
    case 9: setSize( 9, getHeight());
    case 8: setSize( 8, getHeight());
    case 7: setSize( 7, getHeight());
    case 6: setSize( 6, getHeight());
    case 5: setSize( 5, getHeight());
    case 4: setSize( 4, getHeight());
    case 3: setSize( 3, getHeight());
    case 2: setSize( 2, getHeight());
    case 1: setSize( 1, getHeight());
    }

    etc.
    Boudewijn Dijkstra, May 13, 2005
    #6
  7. -

    Wibble Guest

    Lol, generate the byte codes to a file, then
    load them with a class loader.

    Boudewijn Dijkstra wrote:
    > "-" <> schreef in bericht
    > news:42840f4d$...
    >
    >>Is it possible to refactor the while loop into one method? The only way I
    >>can think of is to use if ... else when deciding the setSize but that would
    >>be inefficient.

    >
    >
    > Unroll it:
    >
    > switch (x) {
    > case 20: setSize(20, getHeight());
    > case 19: setSize(19, getHeight());
    > case 18: setSize(18, getHeight());
    > case 17: setSize(17, getHeight());
    > case 16: setSize(16, getHeight());
    > case 15: setSize(15, getHeight());
    > case 14: setSize(14, getHeight());
    > case 13: setSize(13, getHeight());
    > case 12: setSize(12, getHeight());
    > case 11: setSize(11, getHeight());
    > case 10: setSize(10, getHeight());
    > case 9: setSize( 9, getHeight());
    > case 8: setSize( 8, getHeight());
    > case 7: setSize( 7, getHeight());
    > case 6: setSize( 6, getHeight());
    > case 5: setSize( 5, getHeight());
    > case 4: setSize( 4, getHeight());
    > case 3: setSize( 3, getHeight());
    > case 2: setSize( 2, getHeight());
    > case 1: setSize( 1, getHeight());
    > }
    >
    > etc.
    >
    >
    Wibble, May 14, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,231
    Tim Slattery
    Mar 30, 2005
  2. Uday Bidkar
    Replies:
    4
    Views:
    489
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Dec 12, 2006
  3. Tinku
    Replies:
    5
    Views:
    346
    Chris Dollin
    Jun 10, 2008
  4. Gergely Kontra
    Replies:
    16
    Views:
    230
    Devin Mullins
    Sep 20, 2005
  5. Isaac Won
    Replies:
    9
    Views:
    372
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page