'If', "and', and 'not'

Blitz3D Forums/Blitz3D Beginners Area/'If', "and', and 'not'

OwlEpicurus(Posted 2009) [#1]
Given the following code:

a = 1
b = 1
If Not a = 0
	If Not b = 0
		Print "Check"
	EndIf
EndIf

WaitKey()

End


Is there any way to do with one 'if' statement the same thing that is done with these 'if' statements?


Yasha(Posted 2009) [#2]

a = 1
b = 1
If a And b
	Print "Check"
EndIf

WaitKey()

End



If checks the entire following statement to see if it evaluates as true. It's a common misconception that If has to have a comparison operator, but it doesn't - if you say "If a<>False" it evaluates "a<>False" and passes the value of that to the If, so the "<>False" part is redundant. It's still good practice to include comparison operators where practical though so people can see exactly what's being checked.

On the other hand, if you're a serious optimisation junkie, having two If statements is better than one, as Blitz3D (unlike C) will check the entire expression and evaluate both a and b in "a And b" because the And is binary, not Boolean. So if a is false, it will still evaluate b - therefore in complex expressions (generally not where you're simply comparing values, though) the nested Ifs are faster.


OwlEpicurus(Posted 2009) [#3]
But what should you do if the event should only occur when two things are not true? Keep in mind that this is a much simplified example of what I am trying to get across.


Yasha(Posted 2009) [#4]
Well if you want it to happen when they're not true, it becomes
If Not a And Not b

That simple. However, you might want to put brackets in there to either make it clearer or enforce the precedence:
If (Not a) And (Not b)

Remember that "Not a=0" actually returns True if a is True.


OwlEpicurus(Posted 2009) [#5]
Actually, the second one you suggested works, but without the brackets you get an error message ("expecting expression").

Thanks.


Yasha(Posted 2009) [#6]
Ah, please forgive my error. "Not" has the lowest operator precedence (you can find a list in Help->Language Reference->Expressions) so it does definitely need the brackets.


_PJ_(Posted 2009) [#7]
True and False are simply 1 and 0
You canmake use of the math properties of these numbers, where any result multiplied by 0 is still 0 and that to the compiler, a TRUE result on a value is any non-0 result (including negative numbers):

This will Return TRUE only if BOTH a AND b are FALSE.
If Not(a+b)


This will Return TRUE only if BOTH a AND b are TRUE.
If (a*b)


This will Return TRUE only if a OR b are TRUE.
If (Abs(a)+Abs(b))