Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > logic instead of if

Reply
Thread Tools

logic instead of if

 
 
kwikius
Guest
Posts: n/a
 
      01-14-2007

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

 
Reply With Quote
 
 
 
 
Grizlyk
Guest
Posts: n/a
 
      01-14-2007
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

 
Reply With Quote
 
 
 
 
Mirek Fidler
Guest
Posts: n/a
 
      01-14-2007

Sylvester Hesp wrote:
> "Mirek Fidler" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
> >
> > 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

 
Reply With Quote
 
Sylvester Hesp
Guest
Posts: n/a
 
      01-14-2007

"Mirek Fidler" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python Logic Map/Logic Flow Chart. (Example Provided) spike Python 8 02-09-2010 12:31 PM
Asynchronous Logic Gates and Analog Logic Gates Jyoti Ballabh Software 3 11-26-2009 06:48 PM
Introduction to Logic Gates Silverstrand Front Page News 0 10-24-2005 01:44 PM
M$ Business Logic .brian. Firefox 0 09-23-2004 10:32 PM
C++ Template Classes of Multi-Value Logic Qunwei Chen VHDL 6 08-22-2003 03:50 PM



Advertisments