logic instead of if

G

Gary Wessle

Hi

in order to assign a value to a variable if a switch is true, can I do
this
switch && var=val;
instead of
if( switch == true ) var = val;
and be safe?

thanks
 
A

Andre Kostur

Hi

in order to assign a value to a variable if a switch is true, can I do
this
switch && var=val;
instead of
if( switch == true ) var = val;
and be safe?

First... can't name a variable "switch". It's a keyword.

Second, why would you want to? First write your code to be readable and
correct. The if form is far clearer to read.
 
G

Gavin Deane

Gary said:
Hi

in order to assign a value to a variable if a switch is true, can I do
this
switch && var=val;
instead of
if( switch == true ) var = val;
and be safe?

Well you can't have a variable called switch because switch is a
keyword :) Other than that, your two alternatives have the same effect
[*]. The && operator is guaranteed to operate its left-hand operand
first, and guaranteed only to evaluate its right-hand operand if its
left-hand operand evaluated to true.

Whether you would ever want to obfuscate your code like this is a
different matter ...

[*] The use of && in your first option introduces a sequence point that
is not present in your second option. With more complex expressions,
that sequence point may be significant. To be equivalent in that
respect your second option would have to become

bool flag = (sw == true); // I used sw instead of switch as the
variable name
if (flag) var = val;

Gavin Deane
 
P

peter koch

Gary Wessle skrev:
Hi

in order to assign a value to a variable if a switch is true, can I do
this
switch && var=val;
instead of
if( switch == true ) var = val;
and be safe?

Not in the general case. For one thing, the result of var = val must be
convertible to bool, and even if it is, the result of that conversion
might have unwanted effects.
Also important is that you obscure your code for no reason at all.
Keep it as
if (switch) var = val; // disregarding for this example that you can't
name a variable "switch".
there is no reason to have the comparison to true - this is another
obfuscation.

/Peter
 
O

Ondra Holub

Gary Wessle napsal:
Hi

in order to assign a value to a variable if a switch is true, can I do
this
switch && var=val;
instead of
if( switch == true ) var = val;
and be safe?

thanks

You could do it if var is bool. BUT in this case would be better to
write it with if due to readability (or you could write var = switch &&
val - it would be still more readable than switch && var=val).

It could wotk also when var can be typecasted to bool, but the
readability is away.

It could be also valid, when type of var may be constructed from your
'switch' and this type has overloaded operator&&. In such case are
evaluated BOTH operands of && and this is definitely not what you want.

Conclusion: do not such things. You will get lesss readable code which
is not faster as code with if.
 
A

Andrew Koenig

Well you can't have a variable called switch because switch is a
keyword :) Other than that, your two alternatives have the same effect
[*]. The && operator is guaranteed to operate its left-hand operand
first, and guaranteed only to evaluate its right-hand operand if its
left-hand operand evaluated to true.

No they don't. && binds more tightly than assignment, so

x && y = z;

means the same as

(x && y) = z;

which is surely not what you intend.

But even if you intended

x && (y = z);

this would work only if the type of the result of = can be converted to
bool. So, for example:

struct Foo { int x; };
bool b = true;
Foo foo; foo.x = 42;

b && (foo = foo);

will not compile because the result of (foo = foo) has type Foo, which
cannot stand as the right operand of &&.
 
P

peter koch

Gary Wessle skrev:
Hi

in order to assign a value to a variable if a switch is true, can I do
this
switch && var=val;
instead of
if( switch == true ) var = val;
and be safe?

thanks

No - it is NOT safe. First of all, it requires a conversion to bool of
whatever type var is. This conversion could get you into trouble.
But most important is that you obfuscate your program for no reason at
all. Use the "standard" way (disregarding that switch is a reserved
word) if (_switch) var = val. And notice the missing comparison with
bool. This is also an obfuscation that has no place in "real" code.

/Peter
 
M

Mirek Fidler

Andrew said:
struct Foo { int x; };
bool b = true;
Foo foo; foo.x = 42;

b && (foo = foo);

will not compile because the result of (foo = foo) has type Foo, which
cannot stand as the right operand of &&.

b && (foo = foo, true)

should solve the problem.

This is of course obfuscation, but I can see that such construct could
be useful in some macro hackery.

Mirek
 
S

Sylvester Hesp

Mirek Fidler said:
b && (foo = foo, true)

should solve the problem.

Foo & operator , (const Foo & foo, bool b)
{
return foo;
}

now what? ;)

Bottom-line: use ifs for these kinds of constructs.
 
G

Gavin Deane

Andrew said:
Well you can't have a variable called switch because switch is a
keyword :) Other than that, your two alternatives have the same effect
[*]. The && operator is guaranteed to operate its left-hand operand
first, and guaranteed only to evaluate its right-hand operand if its
left-hand operand evaluated to true.

No they don't. && binds more tightly than assignment

That question occurred to me but for some reason I didn't bother to
check it.

But even if you intended

x && (y = z);

this would work only if the type of the result of = can be converted to
bool.

And despite the OP mentioning nothing about the types involved I was
assuming int or somthing similar throughout.

For both errors I can only point to the fact that it was well past my
bed time and my brain had already gone to sleep while my hands were
still typing.

I believe my point about the sequence point introduced by && having the
potential to change the meaning still stands. For example (using
parentheses to group the operators correctly, and using a type
convertible to bool on the right-hand side):

int main()
{
int i = 0;
if (i++) i = 42;

int j = 0;
j++ && (j = 42);
}

as I understand it, the code using i has undefined behaviour while the
behaviour of the code using j is well defined.

Gavin Deane
 
K

kwikius

Gary said:
Hi

in order to assign a value to a variable if a switch is true, can I do
this
switch && var=val;
instead of
if( switch == true ) var = val;
and be safe?

thanks

Herse some alternatives:

#include <iostream>

int main()
{
int var=0,val=1, switch_=2;

switch_? var = val : var;
var = switch_?val:var;

while( (var !=val) && switch_) var = val;

for(;(var !=val) && switch_;)var = val;

switch (switch_ ){
case false:
break;
default:
var = val;
}

std::cout << var <<'\n';
}

;-)

regards
Andy Little
 
G

Grizlyk

Gary said:
switch && var=val;
instead of
if( switch == true ) var = val;

In modern compilers no reason to do
ext && expr
intead of
if(ext) expr;
because code for "if()" will not be worse than for "&&", but if() is
better to read, and you and compiler both exactly know what do you
want.

In many cases *p= operation will give to you code exactly as p[0]= and
*p++= exactly as p[0]=;++p;

The times, when C/C++ compilers just unrolled you lines to asm
fortunately passed.

mov ax,0
mov bx,ax
mov ax,0
mov dx,ax
add bx,dx
mov word ptr var, bx

mov ax, word ptr var
push ax

mov ax,0
mov bx,ax
mov ax,0
mov dx,ax
add bx,dx
mov word ptr var2, bx

mov ax, word ptr var2
push ax

lea ax, word ptr var3
push ax
call _printf
 
M

Mirek Fidler

Sylvester said:
Foo & operator , (const Foo & foo, bool b)
{
return foo;
}

now what? ;)

OK, you want to play it hard, let us play hard :)

template <class T>
void Q(const T&) {}

b && (Q(foo = foo), true)
Bottom-line: use ifs for these kinds of constructs.

Of course. But as I said, it can be seldom useful for macro hackery -
the whole thing is still an expression, not statement. Think about
things like boost's FOREACH macro....

Mirek
 
S

Sylvester Hesp

Mirek Fidler said:
OK, you want to play it hard, let us play hard :)

template <class T>
void Q(const T&) {}

b && (Q(foo = foo), true)

You can hardly call that usable ;)
Of course. But as I said, it can be seldom useful for macro hackery -
the whole thing is still an expression, not statement. Think about
things like boost's FOREACH macro....

My remark was more aimed at the OP, but fair enough, for a macro it would be
useful. You don't need the Q function btw, you could also cast to void:
b && ((void)(foo = foo), true)

I think now it's as good as unbreakable

- Sylvester
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top