Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Refactoring a while loop without if .. else

Reply
Thread Tools

Refactoring a while loop without if .. else

 
 
-
Guest
Posts: n/a
 
      05-13-2005
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;
}
}
}
 
Reply With Quote
 
 
 
 
Wibble
Guest
Posts: n/a
 
      05-13-2005
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;
> }
> }
> }

 
Reply With Quote
 
 
 
 
Wibble
Guest
Posts: n/a
 
      05-13-2005
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;
>> }
>> }
>> }

 
Reply With Quote
 
Wibble
Guest
Posts: n/a
 
      05-13-2005
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;
>>> }
>>> }
>>> }

 
Reply With Quote
 
Michael Rauscher
Guest
Posts: n/a
 
      05-13-2005
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

 
Reply With Quote
 
Boudewijn Dijkstra
Guest
Posts: n/a
 
      05-13-2005
"-" <(E-Mail Removed)> schreef in bericht
news:42840f4d$(E-Mail Removed)...
> 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.


 
Reply With Quote
 
Wibble
Guest
Posts: n/a
 
      05-13-2005
Lol, generate the byte codes to a file, then
load them with a class loader.

Boudewijn Dijkstra wrote:
> "-" <(E-Mail Removed)> schreef in bericht
> news:42840f4d$(E-Mail Removed)...
>
>>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.
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
quarry: Infinite loop without using for , while, do-while Tinku C Programming 5 06-10-2008 07:34 AM
Whats the difference between while loop in Windows message loop and while(1) Uday Bidkar C++ 4 12-12-2006 12:30 PM
ruby idiom for python's for/else while/else Gergely Kontra Ruby 16 09-20-2005 08:35 PM
while loop in a while loop Steven Java 5 03-30-2005 09:19 PM



Advertisments