Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on What are statements and expressions?

Parent

What are statements and expressions?

+12
−0

When I have tried to read technical explanations of the syntax rules for programming languages, and when I am trying to decipher error messages, I often encounter the terms expression and statement. It comes across that these two are related to each other somehow.

I understand that these terms have something to do with the actual code written in a programming language - not, for example, special sorts of values calculated by the program when it runs - right? But what do they mean exactly? How can I use these concepts to improve my understanding of a programming language?

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

A friendly challenge (2 comments)
Post
+7
−1

In computer programming, an expression is something that yields a value.

A statement performs an action.

For example, let us look at some pseudocode. Let's assume that we want to calculate the sum of 3 variables:

sum = a + b + c;
print(sum);

print(sum); is a statement: it performs an action.
a + b + c is an expression: it yields a value.

Now you may be wondering: is sum = a + b + c a statement, or an expression?
The answer is that it's a statement, but it contains an expression. a + b + c yields a value, and then an action is taken: the value is assigned to a variable.

In this example, we have an arithmetic expression. But most operations on strings and booleans are also expressions!
For example, we could have a conditional: if (a > 3 && p == 5) { ... } In this condition statement, the part a > 3 && p == 5 is an expression. A boolean expression.

Or, we might be concatenating strings:

fullName = firstName + " " + lastName;

In this line of code, firstName + " " + lastName is a string expression. It yields a value. The line as a whole is a statement: it evaluates an expression and stores the result in a new variable.

In general, expressions occur inside statements. An expression yields a value, but after you have your value, you'll want to do something with it - store it somewhere, or output it, or send it as an argument to another function.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

What about assignment expressions? (4 comments)
What about assignment expressions?
mr Tsjolder‭ wrote 9 months ago · edited 9 months ago

There also exist expressions that perform actions (e.g. the walrus operator := since Python 3.8, or ++i in C). Would you consider these expressions to be statements as well?

hkotsubo‭ wrote 9 months ago

In some languages, the assignment also yields a value. For example, in JavaScript console.log(sum = a + b + c) will assign the value of a + b + c to sum and yield its value, so console.log will print it. In this case, the whole thing sum = a + b + c would also be considered an expression?

FractionalRadix‭ wrote 9 months ago

I'm thinking that a statement that returns a value, is both an expression and a statement. I need to do a little more thinking before I'll update my answer.

Derek Elkins‭ wrote 9 months ago

I'll add this here since it is somewhat in the vein. I've downvoted because, while your answer does touch on the main idea separating statements and expressions, it presents it as a comprehensive definition and not the rough intuitive bucket that it is. As Moshi's answer correctly states, what is an expression or statement is defined by a language's grammar and can be anything. Plenty of languages have "statements" that return values, such as assignment expressions, or expressions that "perform an action". Indeed, there are exceptions to your definition in every way: expressions without values, statements that don't perform an action, expressions that do perform and action, and statements that have values.

I'd have no problem retracting the downvote if your answer made it clear that it is more a loose description rather than a definition, and that languages often do have formalized definitions of these terms, e.g. via a grammar, which may not perfectly align with your answer.