logic instead of if

Discussion in 'C++' started by Gary Wessle, Jan 13, 2007.

  1. Gary Wessle

    Gary Wessle Guest

    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
    Gary Wessle, Jan 13, 2007
    #1
    1. Advertising

  2. Gary Wessle

    Andre Kostur Guest

    Gary Wessle <> wrote in
    news::

    > 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.
    Andre Kostur, Jan 13, 2007
    #2
    1. Advertising

  3. Gary Wessle

    Gavin Deane Guest

    Gary Wessle wrote:
    > 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
    Gavin Deane, Jan 13, 2007
    #3
  4. Gary Wessle

    peter koch Guest

    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
    peter koch, Jan 13, 2007
    #4
  5. Gary Wessle

    Ondra Holub Guest

    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.
    Ondra Holub, Jan 13, 2007
    #5
  6. "Gavin Deane" <> wrote in message
    news:...

    >> 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.


    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 &&.
    Andrew Koenig, Jan 13, 2007
    #6
  7. Gary Wessle

    peter koch Guest

    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
    peter koch, Jan 13, 2007
    #7
  8. Gary Wessle

    Mirek Fidler Guest

    Andrew Koenig wrote:

    > 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
    Mirek Fidler, Jan 14, 2007
    #8
  9. "Mirek Fidler" <> wrote in message
    news:...
    >
    > Andrew Koenig wrote:
    >
    >> 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.


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

    now what? ;)

    Bottom-line: use ifs for these kinds of constructs.
    Sylvester Hesp, Jan 14, 2007
    #9
  10. Gary Wessle

    Gavin Deane Guest

    Andrew Koenig wrote:
    > "Gavin Deane" <> wrote in message
    > news:...
    >
    > >> 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.

    >
    > 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.

    <snip>

    > 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
    Gavin Deane, Jan 14, 2007
    #10
  11. Gary Wessle

    kwikius Guest

    Gary Wessle wrote:
    > 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
    kwikius, Jan 14, 2007
    #11
  12. Gary Wessle

    Grizlyk Guest

    Gary Wessle wrote:

    > 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
    Grizlyk, Jan 14, 2007
    #12
  13. Gary Wessle

    Mirek Fidler Guest

    Sylvester Hesp wrote:
    > "Mirek Fidler" <> wrote in message
    > news:...
    > >
    > > Andrew Koenig wrote:
    > >
    > >> 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.

    >
    > 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
    Mirek Fidler, Jan 14, 2007
    #13
  14. "Mirek Fidler" <> wrote in message
    news:...
    >
    > Sylvester Hesp wrote:
    >> 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)
    >


    You can hardly call that usable ;)

    >> 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....


    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
    Sylvester Hesp, Jan 14, 2007
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Qunwei Chen
    Replies:
    6
    Views:
    3,635
    Renaud Pacalet
    Aug 22, 2003
  2. RZ
    Replies:
    2
    Views:
    507
  3. Cliff Cummings

    SystemVerilog: "logic" or "ulogic?"

    Cliff Cummings, Sep 16, 2003, in forum: VHDL
    Replies:
    7
    Views:
    4,129
    cc0139808
    Sep 20, 2003
  4. Richard B. Katz
    Replies:
    0
    Views:
    686
    Richard B. Katz
    Dec 4, 2003
  5. spike
    Replies:
    8
    Views:
    1,459
    Steve Holden
    Feb 9, 2010
Loading...

Share This Page