|
-
Mar 10th, 2010, 05:11 PM
#1
Thread Starter
New Member
Simplify my IF statement
So I have here a sub, that is called to check values in my program. It works as it is, but it is a very long line of code. I know there must be a way to simplify this. As long as it uses fewer than 10 lines (even though this is 3 lines, split up. Same thing in reference). Any thoughts suggestions?
Code:
If (btnArry(1) = btnArry(2) And btnArry(2) = btnArry(3) And Not btnArry(1) = Nothing) Or _
(btnArry(4) = btnArry(5) And btnArry(5) = btnArry(6) And Not btnArry(4) = Nothing) Or _
(btnArry(7) = btnArry(8) And btnArry(8) = btnArry(9) And Not btnArry(7) = Nothing) Or _
_
(btnArry(1) = btnArry(5) And btnArry(5) = btnArry(9) And Not btnArry(1) = Nothing) Or _
(btnArry(3) = btnArry(5) And btnArry(5) = btnArry(7) And Not btnArry(3) = Nothing) Or _
(btnArry(1) = btnArry(4) And btnArry(4) = btnArry(7) And Not btnArry(1) = Nothing) Or _
_
(btnArry(2) = btnArry(5) And btnArry(5) = btnArry(8) And Not btnArry(2) = Nothing) Or _
(btnArry(3) = btnArry(6) And btnArry(6) = btnArry(9) And Not btnArry(3) = Nothing) Then
addscore()
End If
This has cut down code duplication a LOT so far. My only question is if I can reduce the code so I don't need so many 'OR's and, If possible, still only call 'addscore()' once.
~Stud
-
Mar 10th, 2010, 05:17 PM
#2
Re: Simplify my IF statement
What type of variable is in your array, a button control?
-
Mar 10th, 2010, 06:31 PM
#3
Re: Simplify my IF statement
Hmmm, nine buttons in an array, checking for triplets horizontally, vertically, and diagonally and an addscore method. Would this be tic-tac-toe? If so, then there may be a better way. I forget what it was, but somebody posted on here a month or so back about a way to assign values to the cells such that if you added all the values, the sum would tell you whether or not there was a winner. That's all I remember about it.
BY the way, I would tend to increase the length of your code: All And should be changed to AndAlso, and all Or should be changed to OrElse. These changes will make the code more efficient.
My usual boring signature: Nothing
 
-
Mar 11th, 2010, 12:39 AM
#4
Thread Starter
New Member
Re: Simplify my IF statement
in fact it is a tic-tac-toe. lol. The example in the book i have at school uses 2-dimensional array's, and is about 4 times as long as my code with just the tic tac toe buttons. My program isn't as advanced, but uses pictures and keeps score and such. Makes me happy anyway. The kids at my school are whiners and under achievers though, so my teacher made it as simple for them to understand as she could, as there are very few in the comp sci class that are actually interested in coding. I'm one of those few... My program has well exceeded the minimum to receive full credit, I'm just trying to make it better.
The array is a char array. When a player clicks a button, the button click sets the appropriate number in the array to 'x', or 'o'. It finds the right slot in the array by taking the value of button.tag and inserting 'x' or 'o' into btnArray(button.tag) (depending on who's turn the GUI tells it is to choose a square)
in my code button.tag is known by the code from a global variable included in the button click; dim btnClicked as button = sender.
The If statement I provided is called after Every button click, which is in a sub statement. Thus, is why I created btnClicked as a global variable.
@Shaggy Hiker
AndAlso & orElse make the code more efficient? are they that much different from And & Or?
Last edited by studmuffin; Mar 11th, 2010 at 12:43 AM.
-
Mar 11th, 2010, 12:54 AM
#5
Thread Starter
New Member
Re: Simplify my IF statement
this might sound far fetched. A friend in another class is doing Java, and is doing some work on javabat.com. would something like the exapmle in this link be possible? where the true value's would result into the array, using it to call other values in my btnArry?
http://codingbat.com/prob/p198700
Code:
ex: JavaArray(1,2,3) -> true then
if btnArry(JavaArray(0)) = btnArry(JavaArray(1)) AndAlso btnArry(JavaArray(1)) = btnArry(JavaArray(2)) AndAlso Not btnArry(JavaArray(0)) = Nothing then
addScore()
end if
I botched the explanation... but does what I'm trying to say make sense? Of course the JavaArray is in java code, and thus we'd have to code it for VB.net instead. But in my head it seems like it would work if I knew how to code the algorithm to be true only for the values I need, and false for anything else; additionally i'm sure i would also have to use a for or while loop for the if statement. Or am I just talking craziness here?
Of course this would take more lines of code... but it just seems silly to me to have so many 'OR's in one if statement.
-
Mar 11th, 2010, 02:52 AM
#6
Re: Simplify my IF statement
 Originally Posted by studmuffin
...
AndAlso & orElse make the code more efficient? are they that much different from And & Or?
Yes there are a few new operators AndAlso, OrElse, IsNot, If() which are the equivalents of their old counterparts And, Or, Not Is, IIF but with better performance.
In most languages like C++, the (equivalents of) And/Or/IIF perform short-circuit operations. i.e. If at any stage, the output can be decided without checking the rest of the conditions, the function returns.
VB didn't have any such thing for a long time. All conditions were evaluated regardless of anything.
But these new operators are optimized to perform short-circuiting.
e.g.
Code:
x = IIF(a = b, Function1, Function2)
If Function1 Or Function2 Then DoSomething
If Function1 And Function2 Then DoSomething
In each of the above cases, both Function1 and Function2 will be evaluated, regardless of what they return.
Now see the same thing with new operators.
Code:
x = If(a = b, Function1, Function2)
If a=b then only Function1 is evaluated otherwise Function2 is evaluated. i.e. only one of them will be evaluated in any case.
Code:
If Function1 OrElse Function2 Then DoSomething
Here either one of them must be true to succeed. So if Function1 is true, you know result will be true irrespective of Function2.
If Function1 returns True then Function2 is not evaluated, since the result is known and Function2 can't affect the result;
Otherwise Function2 will also be evaluated.
Code:
If Function1 AndAlso Function2 Then DoSomething
Here both must be true to succeed. So if either one of them is false, you know result is false.
If Function1 returns False then Function2 is not evaluated, since the result is known and Function2 can't affect the result;
Otherwise Function2 will also be evaluated.
-
Mar 11th, 2010, 08:17 AM
#7
Re: Simplify my IF statement
 Originally Posted by Pradeep1210
Yes there are a few new operators AndAlso, OrElse, IsNot, If() which are the equivalents of their old counterparts And, Or, Not Is, IIF but with better performance.
I don't agree ... i use andalso and orelse in 99.9% of cases as it does short circuit (like u said) and is more efficient because of this ... however they ARE NOT COUNTERPARTS ... as this:
if something(asd) = true and somethingelse(asd) = true then
... is not the same as
if something(asd) = true and somethingelse(asd) = true then
even if something(asd) returned false they will have a slightly different result in some cases ... for example if you wanted somethingelse to execute even (as it has some extra functionality) even though the first part = false and therefore the code within the if statement won't execute.
..dont know how to word it better... but hopefully some1 will know what i mean
Kris
-
Mar 11th, 2010, 08:28 AM
#8
Re: Simplify my IF statement
 Originally Posted by i00
I don't agree ...
if something(asd) = true and somethingelse(asd) = true then
... is not the same as
if something(asd) = true and somethingelse(asd) = true then
what's the difference?? Both statements seem exactly same
-
Mar 11th, 2010, 10:45 AM
#9
Re: Simplify my IF statement
I think he meant to use AndAlso in one of those two lines.
I particularly liked the explanation given by Pradeep, and he did show that AndAlso was not the exact same thing as And, but in most cases they really are equivalent, with AndAlso having better performance. However, the better performance of AndAlso is BECAUSE it it not exactly the same as And. If you must not have short circuiting, then you must not use AndAlso. If you DO need to run both alternatives because they happen to be functions that perform some action, then you are relying on side effects for your code to run, and that is a practice that should be only undertaken after lengthy contemplation, as it is a pretty sure path to spaghetti.
My usual boring signature: Nothing
 
-
Mar 11th, 2010, 01:55 PM
#10
Thread Starter
New Member
Re: Simplify my IF statement
makes sense to me. It's useful to know that it exists at least. I never would have known :P I think I also understand the difference between the two, or enough to figure which situations would be better to use one instead of the other. Thank you.
-
Mar 11th, 2010, 02:24 PM
#11
Re: Simplify my IF statement
And I forgot to mention this:
Since now all conditions will not be evaluated, it now becomes significant how you place them.
e.g. Assume that Function1 takes 100ms to execute and Function2 takes 200ms.
So,
If Function1 OrElse Function2 Then DoSomething
would be more efficient than
If Function2 OrElse Function1 Then DoSomething
This is because the first condition will always be evaluated, while the second condition will be evaluated only if required.
What I mean is, you should place conditions that either perform very good compared to others, or make others useless to be evaluated towards the beginning and the others towards the end.
-
Mar 11th, 2010, 05:55 PM
#12
Re: Simplify my IF statement
Right, the check that is most likely to determine the final outcome should come first.
There's another really useful place to use this. In VB6, it was kind of painful to check whether a value in a datatable was Null and not some value. You had to check for Null first, then check for the value second. If you tried to do both in the same If statement, then both would be evaluated. If the value was Null, the first check would return True, and the second check would throw an error. With AndAlso, if the first check returns True, then the second check isn't even performed. This reduces the number of If statements needed.
My usual boring signature: Nothing
 
-
Mar 11th, 2010, 08:31 PM
#13
Thread Starter
New Member
Re: Simplify my IF statement
If it makes my code more efficient, I'm will to try it ^_^ Small program though, so it's hard noticed either way on the school computers if this makes my code any faster or smoother. Will test on my netbook soon. It's the slowest computer I got, so MAYBE something will be noticeable.
Noticeable or not though, this is Great to know. I trust your guys words more than my own, so I won't doubt y'all :P
Prepare to be Muffin'd!! Or- something like that...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|