0

I found a few weird constructs in Java that the compiler allows, but for which I'm not sure what could be the practical use.

1) if statement:

if((score=score+10) > 110); //No if body 

while eg: switch(++i); is not

2) for loop:

for(;;); //No loop body 

Are there practical, valid circumstances to use the preceding code?

html_programmer
  • 18,126
  • 18
  • 85
  • 158
  • 1
    it's usually only useful if there are side effects of the expressions within the statements – Alnitak Feb 14 '16 at 16:47
  • 1
    Useful or not, question is too-broad. And opinion-based. – Tunaki Feb 14 '16 at 16:48
  • 1
    I was just looking for an example where the code could be useful. I'll delete the question if you think it adds no value. – html_programmer Feb 14 '16 at 16:48
  • 2
    I have used `while` loops without bodies to completely consume an iterator or `InputStream`. – mihi Feb 14 '16 at 16:54
  • @Tunaki well, if you say it is opinion based, I opine that `for(;;);` brings me cakes! – Sнаđошƒаӽ Feb 14 '16 at 16:58
  • 2
    @mihi but OP's code literally does nothing - the `for` loop has no body – Bohemian Feb 14 '16 at 16:58
  • 1
    @Shadowfax Really? It brings waffles for me! – Tunaki Feb 14 '16 at 16:59
  • 3
    If you wait long enough, and look in enough places, quantum theory says that a unicorn will appear – Bohemian Feb 14 '16 at 17:03
  • 1
    @Tunaki Not such a stupid question so it seems. http://stackoverflow.com/questions/14112515/semicolon-at-end-of-if-statement http://stackoverflow.com/questions/16428903/empty-if-statements It does appear to be a duplicate though. Also a great example of the SO bandwagon, thanks for your contribution. – html_programmer Feb 14 '16 at 17:33
  • 1
    Where did I say this was a stupid question really? Because I didn't. I said "Too broad and opinion-based". Which is completely different. – Tunaki Feb 14 '16 at 17:34

1 Answers1

2

This:

if((score=score+10) > 110);

is equivalent to:

score += 10;

but has no practical use otherwise.


This:

for(;;);

loops forever doing nothing - not particularly useful, unless you wanted to create a permanently busy thread perhaps for testing purposes.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • 1
    You're wrong. for(;;) is the equivalent to while(true) and there are PLENTY of uses for using that. – Luke Xu Feb 14 '16 at 16:52
  • @LukeXu Except that if you mean "while true" why not say `while(true)`? – Patricia Shanahan Feb 14 '16 at 16:54
  • 2
    @Luke `for(;;);` (empty body as per OP Q) it is equivalent to `while(true);` (empty body). No use. – Bohemian Feb 14 '16 at 16:55
  • @LukeXu FYI, before you judge someone as wrong (especially a high-rep user), there is PLENTY of difference between `for(;;)` and `for(;;);`, which, as said here already, has **no use**. – Sнаđошƒаӽ Nov 29 '16 at 16:15
  • @Sнаđошƒаӽ FYI, before you judge perhaps you should look at the edit history and then look at the time stamps of the comments. I didn't know reputation made you a good coder. – Luke Xu Nov 29 '16 at 23:36
  • @LukeXu Apologies, I didn't look at the edit history. But still, I think the initial version was just the result of a typo, not the intention of the answerer. You are right, reputation doesn't make you a good coder, but in most of the cases only good coders make good reputations. I am sure you will agree :-) – Sнаđошƒаӽ Nov 30 '16 at 06:48