Results 1 to 10 of 10
- 04-03-2009, 03:59 PM #1
Senior Member
- Join Date
- Mar 2009
- Posts
- 105
- Rep Power
- 0
[SOLVED] Is this good programming practice?
May seem like a strange question, but my experience with java is still pretty limited. I just wanted to know if this is good programming practice:
The general idea is that I create some sort of arbitrary boolean value, the sole purpose of which is to start a loop.Java Code:boolean runLoop = true; while (runLoop = true) { WHILE BODY }
Is this acceptable or is it discouraged?Last edited by porchrat; 04-03-2009 at 04:24 PM.
- 04-03-2009, 06:38 PM #2
If it must be the infinite loop you can write only
Java Code:while ( true ) { // WHILE BODY }
- 04-03-2009, 06:46 PM #3
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
maybe the poster meant while(runLoop == true) and not runLoop = true? that would be acceptable code, assuming that either there is a break in the loop or a condition where runLoop = false.
- 04-03-2009, 06:58 PM #4
Maybe, but if runLoop is local method variable (I've supposed it because of serial code in the first post) code inside loop have to modify runLoop to false value and later check it for break... I think it's not good.
And poster wrote "the SOLE purpose of which is to start a loop". It means runLoop doesn't use in any another place, I think...
- 04-04-2009, 12:30 AM #5
Senior Member
- Join Date
- Jul 2008
- Posts
- 125
- Rep Power
- 0
This is acceptable.
I can think of two ways out of this loop.
You can use conditional tests within this
while loop, which if true, execute a "break".
or
You can place the while loop within a
try/catch statement. I've used code like
this to read a file:
Translated this means,Java Code:try{ while(true){ //pseudo code read a line from a file; } catch(Exception e){}
"Try to do this forever thingy, unless it throws
an Exception."
Since I have no infinitely long file, an
exception will occur, causing try{}catch{}
to abandon the while loop.
The other purpose produces cleaner looking
code for traversing linked-lists (at least I think
it's cleaner looking, and easier to follow), but
this is a style isssue. I'm tempted to say it
improves code performance, but I haven't
tested that aspect of it yet.
I would not freak at seeing this kind of loop.
- 04-06-2009, 09:58 AM #6
Senior Member
- Join Date
- Mar 2009
- Posts
- 105
- Rep Power
- 0
- 04-06-2009, 10:04 AM #7
Senior Member
- Join Date
- Mar 2009
- Posts
- 105
- Rep Power
- 0
That is pretty much what I was asking, thanks again guys, I am learning a lot from this forum. I was planning on using a runLoop = false; statement inside an 'if' statement, but I hadn't thought of using a break;
It seems easier to follow (for me anyway) with a runLoop = false statement instead of a break.
- 04-06-2009, 10:56 AM #8
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
i think it is goodJava Code:boolean runLoop = true; while (runLoop) { // WHILE BODY // runLoop may set to false }
- 04-06-2009, 02:51 PM #9
Member
- Join Date
- Mar 2009
- Posts
- 13
- Rep Power
- 0
the corrected one is good runLoop==true
goof luck ^^
- 04-08-2009, 12:54 PM #10
Senior Member
- Join Date
- Mar 2009
- Posts
- 105
- Rep Power
- 0
Similar Threads
-
I need help on a practice code for java
By Sageinquisitor in forum New To JavaReplies: 12Last Post: 07-13-2010, 04:00 PM -
Hello Good Morning, Good afternoon, and Good Evening
By MrFreeweed in forum IntroductionsReplies: 3Last Post: 12-11-2009, 03:32 PM -
Best practice of IF loop with NOT
By sathishranganathan in forum Advanced JavaReplies: 13Last Post: 05-29-2008, 12:11 PM -
Which book is good for 3D+2D game programming with java
By romina in forum Java 2DReplies: 1Last Post: 08-07-2007, 05:19 AM -
Web Services - IBM Expands SOA Management Practice
By Felissa in forum Web FrameworksReplies: 0Last Post: 06-25-2007, 04:08 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks