<p>Some languages (for example C and C++) have assignment expressions, which means the assignment statement as a whole returns a value of the object specified by the left operand after the assignment.</p>
<p>So for example in C++, <a href="https://godbolt.org/z/q4xrjfj4h" rel="nofollow noreferrer">this snippet</a>:</p>
<pre class="lang-cpp prettyprint-override"><code>int a;
std::cout &lt;&lt; (a = 1);
</code></pre>
<p>will print <code>1</code>.</p>
<p>In other languages (such as Pascal), define the assignment as a statement (meaning that it cannot be used in an expression), and there are no assignment expressions at all. Try to do <code>writeln(a := 1111);</code> and <a href="https://tio.run/##K0gsTk7M0U0rSP7/vyyxSCHRSiEzryQ1PbXImispNT0zj0tBobwosyQ1J08jUcHKVsFQ05orNS9F7/9/AA" rel="nofollow noreferrer">it will return an error</a>.</p>
<p>Python takes a hybrid approach, in that it has both operators, <code>=</code> and the walrus operator <code>:=</code>, in which <code>:=</code> is an assignment expression but <code>=</code> is not, so you can do <code>print(a := 1)</code> (<a href="https://tio.run/##K6gsycjPM7YoKPr/v6AoM69EI1HBylbBUJPr/38A" rel="nofollow noreferrer">link</a>), but not <code>print(a = 1)</code> (<a href="https://tio.run/##K6gsycjPM7YoKPr/v6AoM69EI1HBVsFQk@v/fwA" rel="nofollow noreferrer">link</a>).</p>
<p>It's clear that each language takes a different approach to assignment expressions, so what are the pros and cons of having assignment expressions?</p>

<expressions><assignment>
What are the pros and cons of having assignment expressions?
<h2>Assignment expressions can introduce bugs (when paired with implicit conversion)</h2>
<p>For example, in C++, consider <a href="https://godbolt.org/z/vzE7qzq3j" rel="nofollow noreferrer">this snippet</a>:</p>
<pre class="lang-cpp prettyprint-override"><code>int a = 1;
if(a = 2)
{
    std::cout &lt;&lt; &quot;Code in here will be executed&quot;;
}
</code></pre>
<p>It will print the code inside the <code>if</code> statement, although it's not clear as to why at first glance.</p>
<p>Not having assignment expressions can avoid this entire class of bugs.</p>

CC BY-SA 4.0
2023-07-07T10:03:22.723Z
2023-07-07T10:03:22.723Z
justANewbie
CC BY-SA 4.0
2023-07-07T10:43:46.087Z
I fully agree that this is a problem. But there are three factors that together lead to the problem: assignment as expression, treating integers as valid booleans, and using a very similar syntax for comparison and assignment. So, you can't blame it on assignment expressions alone.
Ralf Kleberhoff
# What are the pros and cons of having assignment expressions?
Posted by: username_0 on 2023-07-07
Some languages (for example C and C++) have assignment expressions, which means the assignment statement as a whole returns a value of the object specified by the left operand after the assignment.
So for example in C++, this snippet:
int a;
std::cout << (a = 1);

will print 1.
In other languages (such as Pascal), define the assignment as a statement (meaning that it cannot be used in an expression), and there are no assignment expressions at all. Try to do writeln(a := 1111); and it will return an error.
Python takes a hybrid approach, in that it has both operators, = and the walrus operator :=, in which := is an assignment expression but = is not, so you can do print(a := 1) (link), but not print(a = 1) (link).
It's clear that each language takes a different approach to assignment expressions, so what are the pros and cons of having assignment expressions?
**Tags:** expressions assignment
---
## 1 answers
### Answer by username_0 on 2023-07-07. Score: 3
Assignment expressions can introduce bugs (when paired with implicit conversion)
For example, in C++, consider this snippet:
int a = 1;
if(a = 2)
{
    std::cout << "Code in here will be executed";
}

It will print the code inside the if statement, although it's not clear as to why at first glance.
Not having assignment expressions can avoid this entire class of bugs.
> **Comments:**
> **username_1** (2023-07-07): I fully agree that this is a problem. But there are three factors that together lead to the problem: assignment as expression, treating integers as valid booleans, and using a very similar syntax for comparison and assignment. So, you can't blame it on assignment expressions alone.  
---
