Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends,

1) repeatedly call if
C#
if (i == 1)
    return false;
if (i == 2)
    return false;
if (i == 3)
    return false;
if (i == 4)
    return false;
if (i == 5)
    return false;
if (i == 6)
    return false;


2) conditional OR
C#
if (i == 1 || i == 2 || i == 3 || i == 4 || i == 5 || i == 6)
                return false;


Here you can see two different logic..

I want to know which is better as a performance or as a good programmer and why.

Thanks
Posted
Comments
DaveAuld 17-Sep-11 9:19am    
Homework question? Think about http://en.wikipedia.org/wiki/Short-circuit_evaluation

Try:
C#
switch (i)
   {
   case 1:
   case 2:
   case 3:
   case 4:
   case 5:
   case 6:
      return false;
   }
Why? It's easier to read, it's more flexible, and it's quicker to execute!
 
Share this answer
 
Comments
AditSheth 17-Sep-11 10:39am    
case is like a if else condition. but here only if conditions...
Thanks for reply
In this case you would be better using an expression that uses && (and).
 
Share this answer
 
Comments
AditSheth 17-Sep-11 10:42am    
Thanks for reply..
can you explain me why it is.as flexibility , quicker execution..
Richard MacCutchan 17-Sep-11 11:22am    
In your case you could write if (i > 0 && i <= 6).
OriginalGriff 17-Sep-11 10:49am    
This may be mine brain freezing up due to trying to find a replacement microswitch while a kitten eats my keyboard, but how is AND going to help in the OP's expression? :confused:
Richard MacCutchan 17-Sep-11 11:22am    
See my reply above.
OriginalGriff 17-Sep-11 11:29am    
:doh: It's brain freeze then... :laugh:

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