10

I've come across a situation like this a few times:

while (true) {

while (age == 5); //What does this semi-colon indicate?
//Code
//Code
//Code

}

The while(true) indicates that this is an infinite loop, but I have trouble understanding what the semi-colon after the while condition accomplishes, isn't it equivalent to this?:

while (age == 5) { }

//Code
//Code

In other words, does it mean that the while loop is useless as it never enters the block?

PC Luddite
  • 5,883
  • 6
  • 23
  • 39
Dot NET
  • 4,891
  • 13
  • 55
  • 98
  • 2
    Are you asking about C or C#? You tagged the question with both. – Oded Jan 02 '12 at 22:52
  • I havnt a clue... logically itll do nothing – craig1231 Jan 02 '12 at 22:53
  • I've seen it occur in both, does it behave differently in both languages? – Dot NET Jan 02 '12 at 22:53
  • If `age == 5` you will get an infinite loop. It will only exit if `age != 5` at some stage (different thread?). – Oded Jan 02 '12 at 22:54
  • "does it mean that the while loop is useless as it never enters the block?" Not if `age == 5`. In that case, it's an infinite loop. If `age == 5`, then `age == 5` is `true`, so it means `while (true) { }`. If `age != 5`, then `age == 5` is `false`, so it means `while (false) { }`. –  Jan 02 '12 at 22:56
  • 2
    The JIT compiler will translate that to the HCF machine instruction (Halt and Catch Fire). Putting out that fire is machine dependent. Background info is here: http://en.wikipedia.org/wiki/Halt_and_Catch_Fire – Hans Passant Jan 02 '12 at 23:04
  • @Oded The syntax is the same in either language, but I think the `c` tag ought to be removed judging by the top answer. – PC Luddite Aug 09 '16 at 19:21

4 Answers4

22
while (age == 5);     // empty statement

is equivalent to

while (age == 5) { }  // empty block

Update: Even if there is no body to execute, doesn't mean that the loop terminates. Instead it will simply loop repeatedly over the conditional (which may have or rely upon side-effects) until it is satisfied. Here is the equivalent form with a goto:

loop:
if (age == 5)
  goto loop;

This construct is sometimes used as a busy-loop waiting on a flag to be changed in threaded code. (The exact use and validity varies a good bit by language, algorithm, and execution environment.)

I find the use of ; for an "empty block" empty statement a questionable construct to use because of issues like this:

while (age == 5); {
   Console.WriteLine("I hate debugging");
}

(I have seen this bug several times before, when new code was added.)

Happy coding.

  • That's what I thought - what's confusing me is that I've seen it done countless times to illustrate multithreading synchronisation issues. I doubt it's a mistake since it's repeated. – Dot NET Jan 02 '12 at 22:55
  • @Sean It's equivalent syntax. Presumably `age` is ("volatile" and) set by another thread -- so it's not necessarily a "mistake" to use this idiom (but I argue against the specific construct). –  Jan 02 '12 at 22:58
  • I read somewhere that the compiler is allowed to optimise away infinite loops such as this; let me see if I can find where I read this... – Oliver Charlesworth Jan 02 '12 at 23:09
  • 1
    Ah, see these inconclusive questions: http://stackoverflow.com/questions/2178115/are-compilers-allowed-to-eliminate-infinite-loops and http://stackoverflow.com/questions/3592557/optimizing-away-a-while1-in-c0x. – Oliver Charlesworth Jan 02 '12 at 23:11
  • @OliCharlesworth It seems like it'd depend upon the scope of `age` (as for if the compile could really detect such code never had an effect). Nice reads, thanks. –  Jan 02 '12 at 23:13
  • 1
    ';' is not used as replacement for empty block, instead its an empty statement. "while(condition) statement" is also a way to write while loops with single statement. – Chethan Ravindranath Jan 03 '12 at 17:01
  • @ChethanRavindranath Thank you. I have made some minor edits as such. –  Jan 03 '12 at 20:03
1

while (age == 5); gets stuck into an infinite loop. In c ; is a null terminator. The compiler assumes that the above loop has only one statement that is ; which causes the loop to be iterated over infinitely.

Lion
  • 18,729
  • 22
  • 80
  • 110
1

a statement consisting of only

;

is a null statement. It is the same as a block (also called compound statement) with nothing inside

{
}

They both perform no operations.

ouah
  • 142,963
  • 15
  • 272
  • 331
0

If we put ; anywhere it means null statement (statement that does nothing).

When we write

while(true);

It means a while loop an a statement that does nothing. It is similar to the

while(true)
i++;

Here statement is not null but in the previous case statement was null.

Marko
  • 20,385
  • 13
  • 48
  • 64
Jatin Khurana
  • 1,155
  • 8
  • 15