Forums Register Login

do while and curly braces:-

+Pie Number of slices to send: Send
This does not compile:-
boolean is = true;
do
int val = 45;
while(is);
boolean is = true;
do{
int val = 45;
}while(is);

but the curly braces are not compulsory, this also compiles:-
boolean is = true;
do
System.out.println("Hello");
while(is);

Any experts out there able to explain this?
+Pie Number of slices to send: Send
Sorry should have said that this compiles:-

boolean is = true;
do{
int val = 45;
}while(is);
+Pie Number of slices to send: Send
interesting little example. Seems it doesn't like a variable declaration / assignment inside a do-while without brackets.
It works fine if its just reassigning an already declared variable

Fails if you're trying to declare a variable in the do-while without brackets

Maybe its a bug? I didn't see anything in the JLS saying its illegal (�14.12 The do Statement)
+Pie Number of slices to send: Send
Hi Steven
This is just a guess.
I think what is happening behind the scences is that do statement acts as a label for the while. As labels can't be placed before the variable initialization it is giving an error.
+Pie Number of slices to send: Send
OK, this is one of those things that most people who are just interested in getting their SCJP should just ignore. The exam is not going to test you on this bit of esoterica. Really. You can drive yourself crazy worrying about these sort of minutae - don't. Most programmers will never need to know this - the compiler just tells them their code is invalid, and they fix it.
Note that there's really no valid reason to ever try to write a local variable declaration as the only thing inside a loop, because there's no chance that the local variable will ever be used for anything. Anyone who does this is probably just confused about what they're doing. Java is set up to catch some of the stupid mistakes we might make while coding, if it's something that is obviously useless like this. Of course there are plenty of other mistakes you can make which the compiler won't catch, so don't count on the compiler too much - but certain classes of errors are caught by th compiler, and this is one of them. If you get an error message in this case, it doesn't really matter why the compiler is complaining, so much as what was the coder trying to do? If you see this error, you look at the source code:

and ask yourself, what possible use is there to having that statement? None. Nada. No one could ever use val; it's useless. So get rid of it. That's all we really need to know as programmers.
But, for those who are nonetheless wondering how this is addressed by the JLS:
Maybe its a bug? I didn't see anything in the JLS saying its illegal
It's not a bug; this is illegal according to the JLS. Though like many things in the JLS, this is far from obvious. In this case we have to track the grammar productions found throughout the JSL. The grammar for a do statement is defined in JLS 14.12:

So, what's allowable as a Statement? That's defined in [url=]JSL 14.5[/url]:

Now I'm not going to follow each of these options separately, but the thing is, none of the options above can lead to a LocalVariableDeclarationStatement unless it's nested in a Block. That path is found by following Statement to StatementWithoutTrailingSubstatement to Block, which is defined in JLS 14.2:
A LocalVariableDeclarationStatement can be part of a BlockStatement, which can be part of BlockStatements - but to make that part of a Block, you need the braces.
The short version of this is, despite the name, a LocalVariableDeclarationStatement is not a Statement. But a { LocalVariableDeclarationStatement } is. So you can't have

but you can have

[ December 14, 2003: Message edited by: Jim Yingst ]
straws are for suckers. tiny ads are for attractive people.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 4029 times.
Similar Threads
testing for integer
Getting individual values from a table
Local variable
While Loop Mystery...
invoking methods while inside methods
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 15:55:23.