Beginners question

M

Mike P

I have a basic question about if statements. Is this the right way to
have an if statement if (condition a or condition b) then ...

if ((strCash != "" && blnValidRSValue == true) || (strCash == ""))


Thanks,

Mike
 
J

Jon Skeet [C# MVP]

Mike P said:
I have a basic question about if statements. Is this the right way to
have an if statement if (condition a or condition b) then ...

if ((strCash != "" && blnValidRSValue == true) || (strCash == ""))

Yup, that seems fine to me. Is there any particular concern you were
worried about?

Note that although the above is logically sound, it could be simplified
to:

if (strCash=="" || blnValidRSValue==true)
 
M

Morten Wennevik

Hi Mike,

That would be perfectly legal. An if statement can contain any number of
sub conditions as long as they all can be handled as true or false.

In your case it would read
if (condition 1 AND condition 2) OR condition 3
do stuff

This would be the same as

if(strCash == "")
do stuff
else if(blnValidRSValue == true)
do same stuff

In fact, since || is the Conditional OR (if condition 1 AND 2 is true,
condition 3 will never be checked). If you do it the other way around

if(strCash == "" || blnValidRSValue == true)

would be the same, and easier to read too.

Is strCash empty? If not, is blnValidRSValue true?

Hope this helps,
Morten
 
O

ozbear

Yup, that seems fine to me. Is there any particular concern you were
worried about?

Note that although the above is logically sound, it could be simplified
to:

if (strCash=="" || blnValidRSValue==true)

Err....it is never necessary to test against the values of /true/ or
/false/...just use...

if (strCash=="" || blnValidRSValue)

But I am sure you already knew that :O)

Oz
 

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

Top