Simple Code? What's Wrong?

P

Peter van der Goes

It's probably something obvious, but this has me stumped:

Very simple code extracted from a proper main()

int x = 65;
int y = 55;

if (x > y)
int ans = x + y;

Errors:
E:\CIT1613\HelloWorld.java:17: '.class' expected
int ans = x + y;
^
E:\CIT1613\HelloWorld.java:17: not a statement
int ans = x + y;

Line 17 is int ans = x + y;

?!?
 
M

Mike Schilling

Peter van der Goes said:
It's probably something obvious, but this has me stumped:

Very simple code extracted from a proper main()

int x = 65;
int y = 55;

if (x > y)
int ans = x + y;

Errors:
E:\CIT1613\HelloWorld.java:17: '.class' expected
int ans = x + y;
^
E:\CIT1613\HelloWorld.java:17: not a statement
int ans = x + y;

Line 17 is int ans = x + y;

You can't declare a variable here. In JLS-speak:

Statement : if ParExpression Statement [else Statement]

and while a LocalVariableDeclarationStatement can appear in a Block, it
isn't itself a Statement. (Which argues it should be called something else
, but ...)

Likewise

if (x > y)
label: x = y;

and

if (x > y)
class Inner { .....}

are also forbidden.

int ans;
if (x > y)
ans = x + y;

is of course OK.
 
R

Roedy Green

if (x > y)
int ans = x + y;
spelling that out longhand

if ( x > y )
{

int ans = x + y;

}

your error should be more obvious. You create ans then instantly throw
it away.

you mean to write something like this:

int ans = ( x > y) ? x + y : x - y;

or

int ans = y;

if ( x > y )
{
ans = 42;
}


--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
P

Peter van der Goes

Mike Schilling said:
You can't declare a variable here. In JLS-speak:

Statement : if ParExpression Statement [else Statement]

and while a LocalVariableDeclarationStatement can appear in a Block, it
isn't itself a Statement. (Which argues it should be called something
else , but ...)

Likewise

if (x > y)
label: x = y;

and

if (x > y)
class Inner { .....}

are also forbidden.

int ans;
if (x > y)
ans = x + y;

is of course OK.
Thanks very much for the explanation. I don't think this is well known, at
least not by textbook authors (one of whom used this code in a test question
bank, indicating the correct answer is: ans = 120).
A local variable declaration with initialization not a statement.
Interesting. Same code works fine in C++, but that's not pertinent.
As your explanation suggests, adding {} will allow the code to compile.
I learned something today. I appreciate you taking the time...
 
M

Mike Schilling

Peter van der Goes said:
Thanks very much for the explanation. I don't think this is well known, at
least not by textbook authors (one of whom used this code in a test
question bank, indicating the correct answer is: ans = 120).
A local variable declaration with initialization not a statement.

Certainly it's a statement in the normal use of that term, but it's not a
Statement in the JLS's Backus-Naur grammar.
Interesting. Same code works fine in C++, but that's not pertinent.

That's a really bad idea, I would think.

i = 0;
if (i > 0)
int ans = 12;

if (ans > 0) // is ans declared here? If so, what is its value?

But C++, unlike Java, has no rules to force variables to be initialized
before they're used.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top