Advertisement

What is an expression?

Started by June 30, 2017 07:35 PM
19 comments, last by jpetrie 7 years, 5 months ago

Programming languages let you organize code by building things out of small, well-defined pieces.  Larger pieces of the program are built by combining smaller pieces.  The way that these pieces are allowed to be combined are defined by the language's "grammar".

For example in the C# code:

X = Y + 1;

"X" and "Y" are identifiers (which can be used anywhere an expression is allowed).

"1" is an integer-literal (which can also be used anywhere an expression is allowed).

"Y+1" is an additive-expression (which is a type of expression).

"X = Y+1" is an assignment (which is also a type of expression).

"X = Y+1;" is an expression-statement (which is a type of statement).

A function can contain statements.  A class can contain one or more fields, properties, functions.  Classes can be put inside namespaces.  A program can contain multiple namespaces.

 

You can see the entire set of what pieces are allowed to be combined in its nitty-gritty detail in a formal grammar.  Usually this is too hard to read for day-to-day purposes but sometimes you can find something that you didn't know was possible by reading through them:

https://msdn.microsoft.com/en-us/library/aa664812(v=vs.71).aspx?f=255&MSPPError=-2147217396

13 minutes ago, Lactose said:

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

Alright, I understand that but it's the "standalone" part that I'm confused about, don't statements belong to methods so how can any statement be standalone?

Advertisement
1 minute ago, Dovahfox said:

Alright, I understand that but it's the "standalone" part that I'm confused about, don't statements belong to methods so how can any statement be standalone?

"int a;" can stand on its own and compile.

"int;" cannot.

Hello to all my stalkers.

9 minutes ago, Lactose said:

"int a;" can stand on its own and compile.

"int;" cannot.

So essentially, it doesn't rely on previous codes to perform an action? Or do you mean like, don't do bad things that the compiler doesn't like?

1 minute ago, Dovahfox said:

So essentially, it doesn't rely on previous codes to perform an action? 

Kinda, but it depends a bit on how you look at it. If you have a struct/class named "myThing", then "myThing a;" is still valid. It relies on previous code (in order to compile, it needs to know what a "myThing" is), but with the previous code (struct/class definition) existing, that piece of code can stand on its own.

As a rule of thumb, it's generally equivalent to what you write as a single line of code.

Hello to all my stalkers.

Well, sadly, I'm still very confused. Maybe I'll understand it when I'm more experienced. Thank you anyways.

Advertisement
2 hours ago, Dovahfox said:

Alright, I understand that but it's the "standalone" part that I'm confused about, don't statements belong to methods so how can any statement be standalone?

Quite right. Certain kinds statements are only valid inside of method definitions. Other kinds of statements can only exist only outside of methods. These rules are defined by the language 'grammar'.

Many/most language grammars will define a concept called a 'statement' and a concept called an 'expression' (possibly they will define multiple kinds of each!). Along with rules about when and where these statements and expressions can be used.

In fact any language whether it be a natural language such as English or a computer language such as C# is governed by a unique grammar. A grammar basically descibes how words/tokens/symbols may be composed together to form various kinds of statements/expressions/sentences/etc. As well as how those constructs can be further composed together to define the language overall.

In Python, the difference between an expression and a statement is that a statement is a collection of expressions (which can be one expression or multiple expressions). An expression itself can be pretty much any segment of code as long as it returns something (and that something can be None).

As has been pointed out already, this is technical jargon, language-specific, and you shouldn't get so hung up over the meaning. You'll figure it out eventually if you don't get it now, and knowing doesn't make your code any better.

This  topic has been handled pretty thoroughly, but I feel I might be able to add a little bit of clarity:

Basically, don't worry about the definitions of the words, expressions vs statements. You sound like a beginner (if so, welcome to the wonderful world of computer programming!). I think you'll best learn expressions vs statements by trial and error. 

I'm willing to bet that the scenario that will clear this up for you is in writing if/then/else statements. 

For example:

You want to do something if condition1 is true.. So you'll have code like:

if (condition) { do_stuff } 

condition will have to be an expression.  

You will find that your condition can be expressions, such as:

"a == 5", ie value of variable a equal to integer literal 5, or

"a == b", ie value of variable  a equal to value of variable b, or

"someFunc()", ie return value of someFunc evaluates to True,

etc. 

You will find that condition cannot be statements, like:

"print a", or

"if x == y"

Often times (maybe always?) statements involve keywords reserved by the language. 

Eg, "print" might be a keyword. So, "print a" is a statement that is a command to print the value of the variable, a, to the console. The variable, a, is an expression in the "print" statement. 

I realize that I didn't give actual definitions of the terms "expression" and "statement"; I just gave examples. That's because it's a lot easier to give examples. :-D As others have mentioned, you'll want to study up on the grammar/specification of the language. But also, just practice! Write some code that is simple (even seemingly silly), to help you understand how the language works. Before long, you'll have an understanding of what is considered an expression, and what is considered a statement (and also, you'll have a working knowledge of how your code is interpreted/compiled) 

14 hours ago, Dovahfox said:

Well, sadly, I'm still very confused. Maybe I'll understand it when I'm more experienced.

You probably will, and I wouldn't worry about the fact that you don't get it yet. Understanding the nitty gritty details about what precisely constitutes an expression versus a statement, understanding why those concepts exist, and at all... it's not necessary for a beginner to understand that before progressing on. It might help, but if it doesn't make sense to you it is definitely one of those topics you can set aside for now and come back to later when you feel more confident as a programmer.

This topic is closed to new replies.

Advertisement