wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Question about do-while loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about do-while loop" Watch "Question about do-while loop" New topic
Author

Question about do-while loop

Milton Ochoa
Ranch Hand

Joined: Oct 23, 2007
Posts: 336

This is pretty

�Why cannot find the 'a' symbol (variable)?



----------------------------------
Test10.java:14: cannot find symbol
symbol : variable a
location: class Test10
}while(a == 0);
^
1 error
-------
praveen Shangunathan
Ranch Hand

Joined: Sep 06, 2005
Posts: 65
You will have to declare 'a' before the do .

for eg.,
int a;
do{
a = 0;
System.out.println(a);
}while(a == 0);
Petrus Pelser
Ranch Hand

Joined: Feb 20, 2006
Posts: 132
It's called variable scope. If you declare a variable inside a block, i.e inside braces {}, it will only be visible inside that block. In a do..while, or any other loop construct for that matter, the condition statement is clearly not inside the block and can thus not see any variables declared in that block.
Milton Ochoa
Ranch Hand

Joined: Oct 23, 2007
Posts: 336

Thank you Mr. Petrus
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Question about do-while loop