Variable Assignment in an "If" Condition

Assign variable value inside if-statement

Variables can be assigned but not declared inside the conditional statement:

int v;
if((v = someMethod()) != 0) return true;

Assign variable in if condition statement, good practice or not?

I wouldn't recommend it. The problem is, it looks like a common error where you try to compare values, but use a single = instead of == or ===. For example, when you see this:

if (value = someFunction()) {
...
}

you don't know if that's what they meant to do, or if they intended to write this:

if (value == someFunction()) {
...
}

If you really want to do the assignment in place, I would recommend doing an explicit comparison as well:

if ((value = someFunction()) === <whatever truthy value you are expecting>) {
...
}

Variable assignment inside an 'if' condition in JavaScript

It has nothing to do with the if statement, but:

if(a=2 && (b=8))

Here the last one, (b=8), actually returns 8 as assigning always returns the assigned value, so it's the same as writing

a = 2 && 8;

And 2 && 8 returns 8, as 2 is truthy, so it's the same as writing a = 8.

Variable assignment inside a C++ 'if' statement

if ((int i=5) == 5) is a syntax error. It does not match any supported syntax for if statements. The syntax is init-statement(optional) condition, where condition could either be an expression, or a declaration with initializer. You can read more detail about the syntax on cppreference.

if (int i=5; i == 5) is correct. However, you are using an old version of GCC that dates from before C++17 was standardized. You would need to upgrade your compiler version. According to C++ Standards Support in GCC this feature was added in GCC 7.

Assign variable and check it within an IF evaluation

Yes you can do it, like so:

var myString = "Hello World";
int pos;

if ((pos = myString.IndexOf("World")) >= 0)
{
Console.WriteLine(pos); // prints 6
}
else if ((pos = myString.IndexOf("Some Other Substring")) >= 0)
{
// Do whatever
}

Note that I'm using myString.IndexOf(...) >= 0 as the index of the substring could be 0 (i.e starting at the first character), and the IndexOf method returns -1 if none was found

But you could rather just use string.Contains like so:

var myString = "Hello World";

if (myString.Contains("World"))
{
// Do whatever
}
else if (myString.Contains("Some Other Substring"))
{
// Do whatever
}

This is better if you don't explicitly need the location of the substring, but if you do, use the first one

Put a condition check and variable assignment in one 'if' statement

First, it assigns the value of B to A (A = B), then it checks if the result of this assignment, which is A and evaluates to 1, is equal to 1.

So technically you are correct: On the way it checks A against 1.

To make things easier to read, the code is equivalent to:

UINT A, B = 1;
A = B;
if(A == 1){
return(TRUE);
} else {
return(FALSE);
}

Assigning value to variable using if statement

You cannot use a statement to assign into a variable. For the alternate solution use conditional/Ternary operator

Syntax

const var_name = condition ? true_value : false_value

Example

const w = 2 > 1 ? 1500 : 2500


Related Topics



Leave a reply



Submit