Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about do-while loop

 
Milton Ochoa
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Mr. Petrus
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic