What is the difference between a statement and a keyword?

柔情痞子 提交于 2019-12-08 08:25:01

问题


After calling return a statement, it was brought out to me in the comments:

return isn't a statement, it's the keyword that starts the return statement.

What is the difference between a statement and a keyword that starts a statement?


回答1:


What's the difference between a sentence and a noun that starts a sentence? ;-)

return is a keyword, which means it's one of a few basic terms (tokens) of the language. They are privileged, each reserved for a special purpose and having special meaning (compare this with run of the mill identifiers/names).

A statement is (in broad terms - specific differ between languages) a higher-level unit of the language, akin to (a particular kind of) sentence in natural language. Statements include return 1+1; and foo(bar);, but generally not expressions like 1+1 or foo(bar).

Keywords often form part of statements (e.g. return introduces a return statement), but they never make a full statement on their own - even return; still needs a statement terminator.




回答2:


A keyword (sometimes called a reserved word) is some word with a special meaning inside of a programming language. For example, in C, C++, and Java, int, void, and break are keywords, while in Python def is a keyword.

In an imperative programming language, a statement is a command that the program should execute. For example, the statement

x = y * 137;

means "evaluate the expression x = y * 137," while the statement

while (true) {
    x++;
}

means "continue to increment x forever."

Some keywords can be used in statements. For example, the statement

break;

means "break out of the current loop," while the statement

return true;

(which consists of the return and true keywords) means "exit the current function/method, yielding result true." These are called "break statements" and "return statements," respectively, and it's not incorrect to use the terms this way. However, break and return, by themselves, aren't statements. Notice that the syntax is

break;

with a semicolon and

return [opt-value];

with an optional value and semicolon. I think it's a bit of a stretch to say that it's incorrect to call return and break statements, since while it's technically incorrect to do so, everyone will know what you mean.

Note that some statements may consist of keywords, but not all keywords are statements. For example, you can't write

public;

or

volatile;

in any programming language that I know of.

Hope this helps!




回答3:


The language lawyer in me points out that the C standard n1570 sayeth as follows.

S6.4.1 keywords includes:

return

S6.8 says:

A statement specifies an action to be performed.

S6.8.6. The return statement is defined to include the semicolon.

return expression(opt) ;

Answers the question, if not as interesting to read as some of the other answers.



来源:https://stackoverflow.com/questions/23034078/what-is-the-difference-between-a-statement-and-a-keyword

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!