Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-03-2007, 01:48 PM
Senior Member
 
Join Date: Nov 2007
Posts: 115
ravian is on a distinguished road
while(true)
Hi Java Coders,

I came across the following:

Code:
while(true) { // code goes here }
This while loop wont terminate. Then whats the use of it. Why Java allows such loops.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-03-2007, 03:33 PM
Member
 
Join Date: Nov 2007
Posts: 18
sandeepkk2005 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-03-2007, 04:36 PM
Member
 
Join Date: Dec 2007
Posts: 25
Renegade85 is on a distinguished road
Quote:
Originally Posted by ravian View Post
Hi Java Coders,

I came across the following:

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

Quote:
while(true)
{
i++;
}
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-03-2007, 07:16 PM
Senior Member
 
Join Date: Nov 2007
Posts: 115
ravian is on a distinguished road
Ok kool. So a break statement is a must if we use while(true). Otherwise it wont terminate.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-03-2007, 10:34 PM
Member
 
Join Date: Nov 2007
Posts: 8
clement1 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 12-04-2007, 11:43 AM
Senior Member
 
Join Date: Nov 2007
Posts: 115
ravian is on a distinguished road
Thanks mate.
I got the point.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
bug (?) in ProcessBuilder.redirectErrorStream(true); gadasie Advanced Java 3 03-13-2008 11:04 AM
setVisible(true) does not work Gajesh Tripathi AWT / Swing 2 10-27-2007 09:32 PM


All times are GMT +3. The time now is 04:00 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org