if ___ and ___ { }
Moderator: General Moderators
-
Bennettman
- Forum Contributor
- Posts: 130
- Joined: Sat Jun 15, 2002 3:58 pm
if ___ and ___ { }
In "if" functions how can I make it so more than one condition needs to be met before the commands are carried out (in other words an "and" formula)? Likewise, is there a similar "or" formula so I don't have to make 4 or more of the same thing?
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
and
or
Code: Select all
if(TRUE && TRUE){
}Code: Select all
if(TRUE || FALSE){
}-
Bennettman
- Forum Contributor
- Posts: 130
- Joined: Sat Jun 15, 2002 3:58 pm
So it'd be (for example):
and
or
right?
and
Code: Select all
if ($first == 1 && $second == 2) { code; }or
Code: Select all
if ($first == 1 || $second == 2) { code; }right?
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
-
Bennettman
- Forum Contributor
- Posts: 130
- Joined: Sat Jun 15, 2002 3:58 pm
one suggestion, make your conditions distinct, by nesting each in its own parens.
So:
becomes
In essense, the first way the whole if is one condition, the second way each condition is distinct.
So:
Code: Select all
if ($first == 1 && $second == 2) { code; }Code: Select all
if ( ($first == 1) && ($second == 2) ) { code; }The parenthesis can be required by some languages, C IIRC would change if (a==b && c==d) internally into if (a==(b&&c)==d) due to its operator priority.(That doesn't sound right, but I know that the order was slightly non-intuitive.) So people from a C background will religiously parenthesis comparisions.
If's are a language construct not a function, as such they do not follow normal evaluation rules and all the subexpressions are NOT fully evaluated ahead of time, regardless of the parenthesis because of the promise that the if will short circuit when possible.
If it were math this would be like
(1+1) - (2+2) evaluating to 2 - (2+2) and then realizing that we don't need to finish for some reason.
This is often the (only) reason why something is a language construct versus a built-in function.
If's are a language construct not a function, as such they do not follow normal evaluation rules and all the subexpressions are NOT fully evaluated ahead of time, regardless of the parenthesis because of the promise that the if will short circuit when possible.
If it were math this would be like
(1+1) - (2+2) evaluating to 2 - (2+2) and then realizing that we don't need to finish for some reason.
This is often the (only) reason why something is a language construct versus a built-in function.
operator's precedence assures that something like
if ($a == 2 && $b == 4)
is evaluated as expected. == binds stronger than &&, so it is evaluated first => no difference between
if ($a == 2 && $b == 4) and if (($a == 2) && ($b == 4))
thereforevaluates to TRUE - but nobody wants to read something like that 
on the other hand I don't like either 
if ($a == 2 && $b == 4)
is evaluated as expected. == binds stronger than &&, so it is evaluated first => no difference between
if ($a == 2 && $b == 4) and if (($a == 2) && ($b == 4))
therefor
Code: Select all
$a = 3;
if ( ($a == 3) ? 1 : 0 + 2 == 3 && $a|1 != 0 || $a%2 == 0)
...on the other hand I don't like
Code: Select all
if ( (((($a == 3) ? 1 : 0) + 2) == 3) && ((($a|1) != 0) || (($a%2) == 0)) )