Results 1 to 8 of 8

Thread: while(true)

  1. #1
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default while(true)

    Hi Java Coders,

    I came across the following:

    Java Code:
    while(true)
    {
    // code goes here
    }
    This while loop wont terminate. Then whats the use of it. Why Java allows such loops.

  2. #2
    sandeepkk2005 is offline Member
    Join Date
    Nov 2007
    Posts
    18
    Rep Power
    0

    Default

    Inside the loop there will be some conditions to come out of it(break;)
    It is to execute some statements continuously unless and until some conditions reached.

  3. #3
    Renegade85 is offline Member
    Join Date
    Nov 2007
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by ravian View Post
    Hi Java Coders,

    I came across the following:

    Java Code:
    while(true)
    {
    // code goes here
    }
    This while loop wont terminate. Then whats the use of it. Why Java allows such loops.
    It could test if something is true and terminate then. Simple example: It could be testing to see if i=5. Inside the loop, the i value could be changed so then it will not be true and the loop will terminate

    while(true)
    {
    i++;
    }

  4. #4
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default

    Ok kool. So a break statement is a must if we use while(true). Otherwise it wont terminate.

  5. #5
    clement1 is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default

    not necessary a break statement but you need something inside the loop that alters the condition for it to exit.

    e.g.
    a=5;
    while (a < 10)
    {
    System.out.println("A is " + a);
    a++
    }

    This will print out something like:

    A is 5
    A is 6
    A is 7
    A is 8
    A is 9

    and then when a has reached 10, the condition is no longer true so it will break out of the loop. if you had

    a=5
    while(a<10)
    {
    System.out.println("A is " + a);
    }

    will be stuck in a continual loop because there is nothing inside it to alter the state of a. The program will just keep printing 'A is 5' an infinite amouint of times.

    A is 5
    A is 5
    A is 5
    A is 5
    A is 5
    A is 5
    A is 5
    A is 5
    A is 5........forever.

    Hope thats helped.

  6. #6
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default

    Thanks mate.
    I got the point.

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Annas, please don't resurrect threads that are 4 years old.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Removing the post by annas1159 as it is an irrelevant response to an already answered question.

    Closing the thread.

    db

Similar Threads

  1. bug (?) in ProcessBuilder.redirectErrorStream(true);
    By gadasie in forum Advanced Java
    Replies: 3
    Last Post: 03-13-2008, 09:04 AM
  2. setVisible(true) does not work
    By Gajesh Tripathi in forum AWT / Swing
    Replies: 2
    Last Post: 10-27-2007, 07:32 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
  •