is it possible in java to have a C like syntax for this:
int counter = 10;
while(counter){
System.out.println(counter--);
开发者_开发知识库 }
In C it is working very well to count until the counter is 0... do i really have to write
int counter = 10;
while(counter!=0){
System.out.println(counter--);
}
?
No, java requires a boolean
in a while
condition.
I wouldn't use a while loop here in Java or C. This is clearer IMHO.
for(int counter = 10; counter > 0; counter--)
System.out.println(counter);
The best place to clarify that kind of question is Java Language Specification.
Related to your question please refer The while Statement
Yes, you do have to write that. As @Thomas Jungblut said, Java requires a boolean in the while condition.
This is a good thing. In plain English, what does "while counter
" mean? Nothing. It only has meaning if you say "while counter
is nonzero". Java forces you to say what you mean.
精彩评论