473,385 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

logic instead of if

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
Jan 13 '07 #1
13 1866
Gary Wessle <ph****@yahoo.comwrote in
news:m3************@localhost.localdomain:
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.
Jan 13 '07 #2

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

Jan 13 '07 #3

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

Jan 13 '07 #4

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.

Jan 13 '07 #5
"Gavin Deane" <de*********@hotmail.comwrote in message
news:11**********************@51g2000cwl.googlegro ups.com...
>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 &&.
Jan 13 '07 #6

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

Jan 14 '07 #7

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

Jan 14 '07 #8

"Mirek Fidler" <cx*@ntllib.orgwrote in message
news:11**********************@l53g2000cwa.googlegr 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? ;)

Bottom-line: use ifs for these kinds of constructs.
Jan 14 '07 #9

Andrew Koenig wrote:
"Gavin Deane" <de*********@hotmail.comwrote in message
news:11**********************@51g2000cwl.googlegro ups.com...
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

Jan 14 '07 #10

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

Jan 14 '07 #11
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

Jan 14 '07 #12

Sylvester Hesp wrote:
"Mirek Fidler" <cx*@ntllib.orgwrote in message
news:11**********************@l53g2000cwa.googlegr 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

Jan 14 '07 #13

"Mirek Fidler" <cx*@ntllib.orgwrote in message
news:11**********************@v45g2000cwv.googlegr 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
Jan 14 '07 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: nc | last post by:
My iterator can find my collection when my Action class calls my jsp directly, however when my Action class calls an html file that is set up with IFrames (one of which is loading that same jsp), I...
5
by: A.V.C. | last post by:
Hello, I stuck in a delimma. Where to put the business logic that involves only one update but N number of selects from N tables........with N where conditions
22
by: James H. | last post by:
Greetings! I'm new to Python and am struggling a little with "and" and "or" logic in Python. Since Python always ends up returning a value and this is a little different from C, the language I...
3
by: mca | last post by:
Hi everyone, I'm new to asp.net and i have a question about separating the html code from the programming code. i have an unknown numbers of entries in my table. I want to make a hyperlink...
7
by: Rob R. Ainscough | last post by:
I understand and implement the concept (as best I can), BUT what I would like to know -- how is it possible to completely remove the UI from business logic? "UI references business logic, but...
5
by: pcnerd | last post by:
I just recently got VB.NET 2005 Express Edition. How do I represent logic gates (AND, OR, NOT, etc.) in VB? I considered a function because functions return a value. For example, a 2-input AND...
16
by: MS newsgroup | last post by:
I don't have clear reasons why we need business logic layer and data logic layer instead of having only data logic layer. Are there any good reasons for that?
5
by: salberts | last post by:
Hi, I am writing an application that has its UI and Logic layers. My initial idea was to launch the two layers on two different threads and manage calls made by the UI to the Logic manually....
2
by: Chris Zopers | last post by:
Hello, I would like to know what's the best way to implement a business logic layer between my user interface and my database. I would say I'd make a dll-project for the business logic layer...
15
by: bruno.desthuilliers | last post by:
On 27 juin, 18:09, "John Salerno" <johnj...@NOSPAMgmail.comwrote: For which definitions of "content" and "logic" ??? The point of mvc is to keep domain logic separated from presentation logic,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.