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

About variables in Loop

 
Andry Dub
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is wrong with my code?

for(int m=0,n=0;m<5;(m++)++) {
System.out.print(m + " ");
}
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andry,

"(m++)++" is not valid Java syntax, even when it's not part of a loop. The ++ operator must always operate on an assignable variable (or what people sometimes call an "lvalue"). "m++" is not an assignable variable, which means you cannot write a statement like "m++ = 5". "m++" by itself is fine, but this evaluates to an integer value--and you cannot use ++ on a raw integer value (i.e. you can't write "2++").

To achieve what you seem to be trying to do, use something like this instead:
 
Andry Dub
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your interesting reply!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic