Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

While

Status
Not open for further replies.

munnanext

Programmer
Aug 20, 2004
49
US
I have a question

when I have a program like below

claa MyHelloWorld{
public static void main(String args[]){

While(false){
System.out.println("This is in False statement);}
}
}

When I execute the above Program it is giving the Compilation Error

Unreachable statement.

but when I slightly modify the program like this

claa MyHelloWorld{
public static void main(String args[]){

//Here I changed from true to false
While(true){
System.out.println("This is in False statement);}
}
}

Can you explain me why the First one is giving the Compilation and the Second one is working ?
Thanks
 
because a while loop only continues when the condition is true.

the statement:

while(false) {
// whatever
}

will never evaluate to "true", so the body of the while loop will never be executed.

similar to:

public void foo() {
return;

// this is unreachable
System.out.print("unreachable");
}

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top