while and do…while loops
Form:
[sourcecode language=”java” toolbar=”false”]
while(true/false){…}
[/sourcecode]
The while loop is executed each time round so if the value to be tested is set here it will be reset every time round.
[sourcecode language=”java” toolbar=”false”]
while( (x = getX()) != 0 )
{
…
}
[/sourcecode]
If getX() returns an int then this loop will never finish.
[sourcecode language=”java” toolbar=”false”]
do{…} while(true/false)
[/sourcecode]
Examples of valid do … while loops
[sourcecode language=”java” toolbar=”false”]
do { break ; } while (true) ;
[/sourcecode]
Leave a Reply