Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a problem like below

CASE 1:
C#
bool flag1= false;
bool flag2= true;

If(flag1 && flag2)
{
}

else
{

}

CASE 2:
C#
bool flag1= false;
bool flag2= true;

If(flag1 || flag2)
{
}

else
{

}

in both case how control will follow from if else block.
Posted
Updated 22-Oct-13 23:17pm
v4
Comments
phil.o 22-Oct-13 14:43pm    
Is it me, or both cases are the same? One should read (flag2 && flag1) for case 2 obviously.

More exactly, in case 1, since flag1 is false, flag2 won't ever be evaluated.
On the contrary, in case 2, flag2 will be evaluated to true, then flag1 will be evaluated to know if both are true.
BillWoodruff 23-Oct-13 0:06am    
I wonder if this question may have been edited and the Conditional And Operator && substituted for the Binary And Operator & in one of the two cases ? The solutions below suggest to me that's the case.

@Member 10350030 What exactly is the "problem" here ? Did the answers you received satisfy you ?
Member 10350030 23-Oct-13 5:16am    
In Case2 there is some modification

bool flag1= false;
bool flag2= true;

If(flag1 || flag2) {//or}
{
}

else
{

}

there is logical OR.

Case1 executes the else block, because one of the flags is not set to true. AND requires all flags to equal true.

Case2 executes the if block, because one of the flags is set to true. OR is satisfied with one of the flags equalling true.
 
Share this answer
 
You code won't even compile (you used falg1 instead of flag1 in if expression :-) ).
In both cases, executing the else block is the correct behavior, since one of the and operands is false. However there is a slight difference ( as correctly noted by phil.o):
  • In CASE 1, flag2 is not even evaluated.
  • In CASE 2, both flag1 and flag2 are evaluated

This is a nice property, called short-circuit, of the && operator[^].
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Oct-13 16:35pm    
5ed, but I added some detail in my Solution 4, please see.
—SA
An addition to Solution 3: why "flag2 is not even evaluated" can be important?

C#
static bool Check1() { System.Console.WriteLine("checking condition 1"); return false; }
static bool Check2() { System.Console.WriteLine("checking condition 2"); return true; }

if (Check1() && Check2()) { } // will print only "checking condition 1"

// but 

if (Check1() & Check2()) { } // will print only "checking condition 1" and then "checking condition 2"


So, using '&&', instead of '&', "optimizes out" and redundant checks where it is possible. The difference is manifested with there is a side effect in a Boolean operand.

In practice, I would advise:
  • avoid using complex Boolean operation with any Boolean functions which may cause side effects;
  • In case the above is taken into account, prefer '&&' and '||' over '&' or '|'.


—SA
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 23-Oct-13 1:31am    
When you think C# as the first-choice you make the same mistake C# designers made when removing the bit-wise handling didn't remove & and | but changed it's meaning...
In fact you complicated the case for a man just started his way (using boolean variables there are no meaning of the optimization you bring here up).
Sergey Alexandrovich Kryukov 23-Oct-13 1:48am    
This is a good point, about not the first-choice technology, but I don't do this mistake. This is first thing which concerned me. For example, meaning of '&&' and '&' is different. So what? Every language's syntax and semantics should be reviewed and not mixed up with those in some other language.

Now, complicate what? Would you suggest to hide some information?
Remember, this is not the solution, this is an illustration and advice. At this moment, I see no sense in your criticism based on "complication", otherwise I would greatly appreciate it...

Thank you.
—SA
CPallini 23-Oct-13 3:16am    
Good. My laziness is fixed now. :-)
5.
Sergey Alexandrovich Kryukov 23-Oct-13 9:22am    
Thank you, Carlo. :-)
—SA
BillWoodruff 23-Oct-13 3:41am    
I wrote to Anders H. and told him that having two distinct use-cases for & and | was just a terrible choice, and I suggested they implement BitAnd, and BitOr, or Bit& and Bit| instead, but I haven't heard back from him. No idea why :)
&& Operator default behavior is to make condition necessary to met and not only one both can met will execute. property of true or false or false and true so that if not met then else condition execute.
 
Share this answer
 
&& means and - sot this isn't a problem, you must map your request the make a condition (in the current case only when bot flags are true you will get the if, any other combination runs the else branch)...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900