What are statements and operators?

Asked

Viewed 832 times

13

I usually access the MSDN often and I see a lot of these terms.

What are statements and operators? What differentiates them?

1 answer

16


Statement

Statement is a statement, by literal translation. It is a statement. We often call it a command in English, although the word does not express well what it is. Usually used in imperative languages (almost all mainstream) and it is all necessary to complete a complete instruction for the computer to run.

In general he executes that and closes. It does not produce a result (a value that determines, but rather an action, it does something, as opposed to producing something to be used later. At that point is the opposite of an expression.

In general there is a sequence of them and it usually starts with a keyword that tells you what you’re telling them to do. But there are exceptions. In some cases an expression may function as a statement. This is the case of variable assignments, or even its initial declaration, or function calls alone.

A statement may be composed of several others forming a block.

It is common, if not a block, but in many non-obligatory languages, that it occupies a line. In lineage languages of C (C#, for example) ; indicates the end of statement and the keys indicate the beginning and end of a block of statements. Then you can have more than one statement on the same line, or the same statement be divided into several lines.

In other languages (VB.NET, for example), the new line character indicates its end, and may put more than on the same line with : or you can break it into more than one line with _.

Note that languages usually allow statements empty. In C#, for example a ; alone is a statement valid that does nothing. This can be quite noted in a for when one omits any of the parts it is composed. See a loop infinite:

for (;;)

There are three statements, even if there is nothing in them. Even the last account also, the closing of the parentheses closes the statement.

One important detail that many people confuse is that the statement is not the keyword that usually starts a statement. It is the set of things that tells you to do something in an instruction. There are statements no keyword, such as assigning variables, called function or procedure with nothing else, among others.

I’ve answered over and over What is the difference between a statement and an expression?.

Operator

Operator It’s a form analogous to what we find in mathematics. It works as if it were functions (some are actually under the table even in certain languages and contexts). They usually form expressions together with their operands, always producing a result, as occurs with all expression.

Some languages prefer symbols, whenever possible close to what is used in mathematics, but not always. Others prefer words describing them. And there are those that mix between these two forms, the most common. So you can have a && and a AND which have different syntax in different languages and identical or very close semantics.

You have operators like is, typeof, sizeof, new, among others, who are operators, but many people think they are functions, especially when they are used with parentheses. This is why I think it is important to leave the parentheses glued to the function name and separated when they only serve as a grouping of sub-expressions. Some people think this is silly or just like, but it is useful visually to convey a message. It’s more elegant.

To the point that separates the object from its member is an operator (obj.propriedade), and many people do not understand. The assignment is an operator (i++, x *= 2, a = b - 3 which is an expression and a statement at the same time in most languages).

In general operators have rules of precedence and associativity (example).

Some languages allow you to define some operators' behavior for their types, or even create new operators.

It is common for operators to have a rating Infix (it is in the middle of two operands), but it can also be prefix (goes before the single operand), or may be postfix (after the operand). Some may be different operators depending on where it is placed, typical case of pre and post-increment/decrement.

It is common to have at least one operator with three operands, so it is called ternary (but in fact what exists is a conditional. He is ternary, but several operators are unary or binary, as I mentioned above. As there is usually only one ternary there is this confusion.

An operator is a token single.

Completion

Let’s say they have no direct relation unless operators can be used in expressions and these can be in statements.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.