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.
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.
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
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.