Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: while(true){ section of code runs only once}

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default while(true){ section of code runs only once}

    hi

    while(true)
    {

    i need a partcular section of code runs only once


    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,106
    Thanks
    65
    Thanked 2,717 Times in 2,667 Posts

    Default Re: while(true){ section of code runs only once}

    Remove the while(true) { and ending } and the code will only execute once.

    Do you know about the break statement?

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: while(true){ section of code runs only once}

    As Norm mentioned, there is not need for a while if you only need the code to execute once. If you need the code to execute at least once, the use do{} while()

  4. #4
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: while(true){ section of code runs only once}

    I need to use while loop as its the threads run method ,

    but in that loop i want a section of code to ecxecute once , or for a particular count..

    public Run()
    {

    while(true)
    {

    [ this section of code runs for 200 counts]

    [this scode runs only once]




    }
    Last edited by jack_nutt; June 18th, 2011 at 08:04 PM.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,106
    Thanks
    65
    Thanked 2,717 Times in 2,667 Posts

    Default Re: while(true){ section of code runs only once}

    If the section of code is inside the while loop, then use a boolean oneTime switch in an if statement
    Define the boolean variable as true outside the loop. The inside the loop:
    if (oneTime) {
       oneTime = false;  // flip the switch to prevent execution next loop around
       // do the one time stuff
    } // end if

  6. The Following User Says Thank You to Norm For This Useful Post:

    jack_nutt (June 18th, 2011)

  7. #6
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: while(true){ section of code runs only once}

    I don't see why it has to be inside the while loop - surely only the code that needs to run many times should be in the loop. One-time code should be before or after the loop, as appropriate.

    Also, if you need code executed 200 times, why not use a 'for..' loop that counts 200 iterations? A 'while(true)' loop is really only suitable for loops with an indefinite number of repeats (and not particularly good practice).

Similar Threads

  1. help with user selected section has no available seat applet
    By chonch in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 31st, 2011, 07:39 AM
  2. would this expression be true or false??
    By robertsbd in forum Java Theory & Questions
    Replies: 1
    Last Post: October 24th, 2010, 10:00 PM
  3. Thread runs only once and not again
    By enflation in forum Threads
    Replies: 3
    Last Post: June 9th, 2010, 10:51 AM
  4. Does this still hold true?
    By April in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: February 2nd, 2010, 09:28 AM
  5. runs once only
    By silverspoon34 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 21st, 2009, 03:31 AM