1

Possible Duplicate:
In C/C++ why does the do while(expression); need a semi colon?

I understand the structure of a "do while" loop compared to a "while" loop. What I don't understand, is why does the language require this syntax:

do{
     statements();
} while(condition);

Is it absolutely necessary for the language to have a semicolon at then end of this expression? Or is this more for ease of writing a compiler?

Community
  • 1
  • 1
eternalmatt
  • 3,524
  • 4
  • 25
  • 25

2 Answers2

1

Since the } isn't the end, then it helps to have some way to know that the end of the statement is reached, hence the semi-colon.

It is possible to write a language without it, but it makes more sense to require it, in these languages.

James Black
  • 41,583
  • 10
  • 86
  • 166
  • It's a pseudo-argument. How does it make more sense? That was the question - it can't be the answer as well. – user unknown Apr 12 '11 at 23:44
  • @user unknown: you can't be missing the answering argument in the first paragraph too. That's impossible. It wouldn't make more sense. That was the answer. – sehe Apr 12 '11 at 23:58
  • Helps whom? The compiler? If I leave out the semicolon, the compiler dispraises me, I should put a semicolon there, and knows exactly, where to put it. So he could put it in there himself, infer it, it could be optional. Do you need the semicolon? – user unknown Apr 13 '11 at 00:27
  • 1
    @user unknown - It is required as part of the language specification, but it was a designer choice. It fit best with how they designed the language, but, it would be possible to design it not to be required, but that was not what was chosen. – James Black Apr 13 '11 at 00:47
  • 1
    I understand your words, but the meaning is pure positivism: It is as it is. It is this way, because somebody made it that way. `fit best with how they designed the language` is the only part which makes some sense. So to what does the semicolon fit concrete? The other controlling structures, loops and ifs, have the controlling part in the beginning, followed by a block or by a statement, which is ended by a semicolon. I'm not really satisfied (I don't think my own answer is better). :) – user unknown Apr 13 '11 at 01:36
  • I think my original question might be better as "Why does the grammar of the language require ...." because that's really what I don't understand. If they regex rules are laid out properly for yacc (or other), then the idea of it 'helping the compiler to know the end of a block' suddenly goes away. – eternalmatt Apr 15 '11 at 16:34
0

At least in java, you can't write:

} while (i < 10) && (j != 8);

without getting such errormessages:

NodeSorter.java:24: ';' expected
        } while (i < 10) && (j != 8);
                        ^
NodeSorter.java:24: not a statement
        } while (i < 10) && (j != 8);
                               ^
NodeSorter.java:24: ';' expected
        } while (i < 10) && (j != 8);
                                   ^
3 errors

So I guess it is for reasons of symetry - maybe it is more easy to write a parser that way.

user unknown
  • 35,537
  • 11
  • 75
  • 121