What is an expression?

Started by
19 comments, last by jpetrie 6 years, 9 months ago

I'm having a hard time understanding expressions, what I'm perceiving is that expressions are things that can be evaluated right but what counts as something being evaluated, like does printing something in C# count as an expression, is declaring a variable without initializing it an expression? It doesn't have any value, right? I'm just very confused, I also wonder how it ties in with a statement because it seems that every statement is an expression, I mean, why wouldn't a statement evaluate something? Note that I'm just an absolute beginner that hasn't touch stuff like functions yet so I may just be getting the wrong ideas.

Advertisement

The precise nature of an expression varies slightly from language to language: it is a technical term. In the general sense, an expression is a combination of tokens that are evaluated to produce some kind of result or value. Different languages will tend to clarify or refine that broad generalization further, usually in terms of other concepts specific to that language.

For example, you mentioned C#. In C#, an "expression" is a sequence of tokens (specifically operators and operands) that evaluates to a single value, object, method or namespace.

Conversely in C++, an expression of operators and operands that denote a computation which may produce a value and/or side-effects.

Almost everything in the source code you write in a programming language is evaluated in some way, so almost everything is an expression of some sort.

 

I think the best answer for you if you're just now learning C# is to not get too caught up in the jargon this soon but to instead just be focused on how to code. The technical words and jargon come later. In the meantime try a tutorial site like this http://www.learncs.org/. There's information on everything you'd need to get started and an interactive code terminal that will definitely go a long way in helping you. I find most people learn how to program best when you're just allowed to experiment and play around with stuff.

I've been a software developer for several years now and people seem to generally just consider expressions and statements as meaning a line or a few lines of code. I honestly can't even tell you the difference.

Traditionally, expressions are descriptions of computations that result in a numeric value. Eg "1 + 2" is an expression, since it says that you should add 1 to 2 to get numeric result. The process of adding those numbers and producing the result is called "evaluating an expression".

Now, 1 + 2 is so simple to us, that we do the computation in our head, and read 3 instead. A more difficult example is "1 + a", which says to take the value of "a" (a variable with a numeric value, presumably), and add 1 to it, to get the result.

Obviously, things can get more complicated by adding parentheses and more math operators. For extra fun, you can add floating point numbers (numbers with fractions, like 1.75). The evaluation process just gets longer, as there are more computation steps to perform before you're done.

Simpler is also possible. "2" in itself is the value 2, but you can also see it as an expression. Evaluation is extremely simple. Take the number, and ... nothing, you're immediately done. There are no math operations to perform, so the number is also the result of this (very short) evaluation process.

One step further, there is no reason to limit ourselves to numeric values. We could do the same trick with, say, strings. For example "he" + "llo" could be evaluated as 'take "he", and concatenate "llo" to it, giving you "hello" as result string value'. In that sense "he" + "llo" is also a description of a computation, ie an expression, in this case to produce a string.

Similar tricks can be done with other kinds of data, like lists or arrays (not sure what C# allows there).

 

One more step further, what about statements "a = 1; b = 2"? Isn't this a description that you first must 'assign 1 to a', and then assign '2 to b'? That is, isn't it a description of a computation, in this case to find the final value computed by the program? Instead of "+" in the examples above, you have ";". Instead of "1" or "2" or "a" like above, you have elementary statements "a=1" or "b=2" that you compose after each other with ";". You can develop similar evaluation steps for statements like "for" and "while".  I agree it's a bit weird at first, but "evaluation" is nothing more than performing computation steps to come to a final result. That applies to doing computations in normal expressions like 1 + 2 * 3^(8/4 + 17) / 28, and in performing execution of statements like "a = 2; b = 3; return a + b", both the expression and the statements evaluate to a single result after doing computations

'What' qualifies as an expression vs a statement actually varies across different languages.

In some languages the distinction is that expressions evaluate to a value while statements do not.
In some languages expressions are just a certain type of statement which happen to return values.
In some languages everything is an expression because everything has some kind of a value.

For what it's worth you can go a long, long way without ever needing to know the difference between statements and expressions. This is not something most programmers need to think about day-to-day. It's more a concern for compiler authors and language designers and not really something programmers are required to know in order to actually 'use' the language.

You mentioned C# though so the rest of my answer is more towards the C# way of thinking...

An expression is one or more 'operands' strung together with 'operators'. Operands include things like literal values, variables and function calls and operators are things like +, -, /, *, &&, ||, etc. An expression can consequently be evaluated to a value.  E.g.   1 + x * y - func(10)

Meanwhile a statement is like an "action" or "instruction" to the computer. E.g. "Declare a variable", "Call this function", "compare this value", "loop over an array", etc.  Statements include things like declaring a variable, control-flow statements (if/for/do/while/return/continue/break/etc), declarations of types and methods, etc.

Statements as a whole are very often constructed out of expressions. Such as when you declare a variable and initialize it with the value of some expression all one one line:  int x = 10 + 2;

Now I'm even more confused, I thought statements were just completed "thoughts" of codes but apparently, statements are something else in other languages? 

A statement is (in general) the smallest standalone piece of code you can legally write in a language. A statement is usually composed of expressions.

In languages like C#, a simplistic but mostly-true example of a statement is "a bunch of expressions followed by a semicolon."

20 minutes ago, Dovahfox said:

Now I'm even more confused, I thought statements were just completed "thoughts" of codes but apparently, statements are something else in other languages? 

That's not wrong but it's also quite vague. The terms "statement" and "expression" are actually technical terms that are strictly defined by the language in question.

I think jpetrie's latest post above is a good succinct rule-of-thumb about what these things are.

36 minutes ago, jpetrie said:

A statement is (in general) the smallest standalone piece of code you can legally write in a language. A statement is usually composed of expressions.

In languages like C#, a simplistic but mostly-true example of a statement is "a bunch of expressions followed by a semicolon."

What do you mean by "smallest standalone piece of code you can legally write in a language"? 

Just now, Dovahfox said:

What do you mean by "smallest standalone piece of code you can legally write in a language"? 

"int a;" is legal to write, i.e. something that compiles/is valid code.

Hello to all my stalkers.

This topic is closed to new replies.

Advertisement