Can I declare two variables in a conditional in C++. The compiler gave an error but still I think I should get an opinion:
int main()
{
double d1 = 0;
if((double d2 = d1) || (double d3 = 10))
cout << "wow" << endl;
return 0;
}
Can I declare two variables in a conditional in C++. The compiler gave an error but still I think I should get an opinion:
int main()
{
double d1 = 0;
if((double d2 = d1) || (double d3 = 10))
cout << "wow" << endl;
return 0;
}
This is not possible in C++, maybe you were thinking of C# or a newer programming language. No disadvantages to declaring d2 and d3 outside the conditional though.
You may want to ask where the C++ standard documentation is.
I don't think that it's a good idea, what you are trying to do is to tell the compiler:
declare d2 put d1 in d2 compare d2 with 0 if it's != 0 jump to the code below else declare d3 put 10 in d3 compare d2 with 0 if it's != 0 jump to the code below else skip the code below
Just declaring d2 and d3 above the conditional could be a good idea, only if you work with limited memory (and do not wish to allocate room for d2 and d3 until this piece of code is reached). It all boils down to what you really intend to do.
You can only do that for one variable
if(double d2 = d1)
cout << "wow" << endl;
else
if(double d3 = 10)
cout << "wow" << endl;
But i would declare them outside of the conditions. If you are scared about the scope, you can always limit it:
{
double d2 = d1;
double d3 = 10;
if(d2 || d3)
cout << "wow" << endl;
}
Of course, this evaluates the second initializer even if d2
evaluates to true. But i assume that's not important.
In C++ you can do something like this:
if (bool b = (a == b)) {
// Do something with b here...
}
or:
if (double d2 = d1) {
// ...
}
However, I've never seen this style used in real code. I think there is always a clearer way to write the same thing.
Furthermore, trying to declare multiple variables in the conditional part of an if
or while
statement is not supported. You will have to resort to nested if
s or else if
to get the same semantics.
Example:
if (double d2 = d1) {
cout << "wow" << endl;
} else if (double d3 = 10) {
cout << "wow" << endl;
}
This way d3
is only created if d2
evaluates to false.
if((double d2 = d1) || (double d3 = 10))
cout << d2 + d3 << endl;
the ||
operator short-circuits, so the d2 + d3
expression references (potentially) uninitialized d3. such a feature would have many ill effects and IMO no benefit, so that's probably why it's not there.
There might be something else that is ALSO bothering you here, but there is a problem with how the ||-operator works. I don't remember exactly how it is worded in the standard, but in a || b
either your compiler should not evaluate b
if a
evaluates to true, or your compiler may opt not to do so.
Considering that, in your statement you won't be sure if your variable has been declared - in C++ you can only used declared variables, so d3
will be a pretty useless variable in your example, if you ask me (as you won't be sure if it has been declared).