Assigning variable values "on the fly" within expressions

Click For Summary
SUMMARY

The discussion focuses on the use of assignment expressions in programming languages such as JavaScript, C++, and Mathematica, highlighting their absence in Julia. The term "assignment expression" is identified as a key concept. A specific example in C++ demonstrates the potential for confusion regarding the order of evaluation, emphasizing that while the syntax may work, it can lead to subtle errors if not properly understood. The conversation concludes with a note on APL's handling of variable assignments within expressions.

PREREQUISITES
  • Understanding of assignment expressions in programming languages
  • Familiarity with C++ syntax and compilation behavior
  • Knowledge of order of evaluation in programming languages
  • Basic understanding of APL syntax and operations
NEXT STEPS
  • Research "C++ order of evaluation rules" to understand potential pitfalls
  • Explore "JavaScript assignment expressions" for practical applications
  • Learn about "Julia variable assignment" and its limitations
  • Investigate "APL programming techniques" for efficient variable handling
USEFUL FOR

Programmers, software developers, and computer science students interested in understanding assignment expressions and their implications across different programming languages.

Swamp Thing
Insights Author
Messages
1,047
Reaction score
780
TL;DR
This works in JavaScript : ((a=3) +( b=2) )**2 + 1/a +1/b

Is there a term for it? Does it work in may other languages?
I've used this kind of thing often in JavaScript and Mathematica.
Code:
z = ((a=3) +( b=2) )**2 + 1/a +1/b
I'm currently learning Julia, and it doesn't seem to be supported.

What is this syntax called?
 
Technology news on Phys.org
“Assignment expression” is one term sometimes used.

I won’t enter into the debate about whether using assignment expressions is a good idea (basically, whether the unnecessary and irrelevant reduction in line count justifies the gratuitous obscuration of the code, interference with the compiler’s ability to optimize, and potential for introducing subtle errors) but I will say that if you’re going to use them…. Be very sure that you understand what your language specification says about order of evaluation.
 
  • Like
  • Haha
Likes   Reactions: Swamp Thing, jedishrfu, pbuk and 2 others
It works in C++, too.

Code:
#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
    int a, b;
    int c = ((a = 3) + (b = 2)) * 2;  
    cout << a << ' ' << b << ' ' << c << endl;
    return 0;
}

produces the output

Code:
3 2 10
 
jtbell said:
It works in C++, too.
But note that the more complicated expression in the OP (((a = 3) + (b = 2)) ** 2 + 1/a + 1/b) will compile but there is no guarantee that the compiler will set a to 3 before it calculates 1/a (the order of these operations is not defined in the standard).

So as somebody not quite said upthread, even if this works it is never a good idea.
 
Last edited:
  • Like
Likes   Reactions: phyzguy, Nugatory, Swamp Thing and 1 other person
Nugatory said:
Be very sure that you understand what your language specification says about order of evaluation.
Please forgive me for paraphrasing that liberally!
 
  • Haha
Likes   Reactions: Nugatory
In APL, variable assignment can be done according to the order of evaluation. In this example, the right Z is declared, but uninitialized, where initialization occurs within the parenthesis. The expression is 2 modulo (Z##\leftarrow## 1 2 3 4 5) reduce Z (delete all values where Z modulo 2 are zero).

$$ 2|(Z \leftarrow 1 \ 2 \ 3 \ 4 \ 5)/Z $$

returns 1 3 5
 

Similar threads

Replies
235
Views
14K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
Replies
4
Views
3K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 40 ·
2
Replies
40
Views
8K