Why use an assignment in state?

In many languages, assignments are legal under conditions. I never understood the reason for this. Why should you write:

if (var1 = var2) { ... } 

instead:

 var1 = var2; if (var1) { ... } 
+65
variable-assignment condition
Sep 30 '08 at 5:24
source share
11 answers

This is more useful for loops than for statements.

 while( var = GetNext() ) { ...do something with var } 

What else would have to be written down

 var = GetNext(); while( var ) { ...do something var = GetNext(); } 
+92
Sep 30 '08 at 5:32
source share

This is more useful if you call a function:

 if (n = foo()) { /* foo returned a non-zero value, do something with the return value */ } else { /* foo returned zero, do something else */ } 

Of course you can just put n = foo (); on a separate statement, then if (n), but I think this is higher, this is a pretty readable idiom.

+28
Sep 30 '08 at 5:27
source share

I find it most useful in action chains, which often include error detection, etc.

 if ((rc = first_check(arg1, arg2)) != 0) { report error based on rc } else if ((rc = second_check(arg2, arg3)) != 0) { report error based on new rc } else if ((rc = third_check(arg3, arg4)) != 0) { report error based on new rc } else { do what you really wanted to do } 

An alternative (without using the assignment in the condition) is:

 rc = first_check(arg1, arg2); if (rc != 0) { report error based on rc } else { rc = second_check(arg2, arg3); if (rc != 0) { report error based on new rc } else { rc = third_check(arg3, arg4); if (rc != 0) { report error based on new rc } else { do what you really wanted to do } } } 

With long-term error checking, the alternative may exit the RHS page, while the conditional version does not.

+25
Sep 30 '08 at 5:59
source share

This can be useful if you call a function that returns data for work or a flag to indicate an error (or what you did).

Something like:

 while ((c = getchar()) != EOF) { // process the character } // end of file reached... 

Personally, this is an idiom that I don’t really like, but sometimes the alternative is uglier.

+22
Sep 30 '08 at 5:26
source share

GCC can help you discover (with -Wall) if you inadvertently try to use a destination as a true value, if it recommends writing

 if ((n = foo())) { ... } 

those. use extra parentheses to indicate that this is really what you want.

+9
Sep 30 '08 at 5:32
source share

The idiom is more useful when you write a while instead of an if . For the if you can break it down as you described. But without this design, you will either have to repeat:

 c = getchar(); while (c != EOF) { // ... c = getchar(); } 

or use a loop and half structure:

 while (true) { c = getchar(); if (c == EOF) break; // ... } 

I would prefer a loop-and-half shape.

+8
Sep 30 '08 at 5:32
source share

In PHP, for example, it is useful for cyclically using the results of an SQL database:

 while ($row = mysql_fetch_assoc($result)) { // Display row } 

This looks a lot better than:

 $row = mysql_fetch_assoc($result); while ($row) { // Display row $row = mysql_fetch_assoc($result); } 
+3
Sep 30 '08 at 5:33
source share

The short answer is that Expression-oriented programming languages ​​allow you to use shorter code. Do not force you to separate commands from requests .

+3
Sep 30 '08 at 6:36
source share

Another advantage is the use of gdb. In the following code, the error code is not known if we must take one step.

 while (checkstatus() != -1) { // process } 

Instead

 while (true) { int error = checkstatus(); if (error != -1) // process else //fail } 

Now, in one step, we can find out what the return error code from checkstatus () was.

+2
Oct 10 '13 at 6:39
source share
 if((var = someFunction())){ //Enclosed in additional pair of brackets to show its intentional ... 

The above can be used instead of ...

 var = someFunction(); if(var){ ... 
0
Jun 07 '18 at 20:09
source share

The reason is this:

  • Performance improvement (sometimes)

  • Small code (always)

Take an example: there is someMethod() , and in if you want to check if the return value of the method is null . If not, you will use the return value again.

 If(null != someMethod()){ String s = someMethod(); ...... //Use s } 

This will interfere with performance as you call the same method twice. Use instead:

 String s; If(null != (s = someMethod())) { ...... //Use s } 
-2
Nov 18 '16 at 12:24
source share



All Articles