Summary of “Conditions”
Syntax
Condition:
if (condition) {
action;
}
Condition with alternative action:
if (condition) {
actions;
} else {
other actions;
}
Nested conditions:
if (condition1) {
if (condition2) {
actions;
}
}
How Conditions Work
Expression in parentheses (check) returns true
or false
. The action inside the condition is executed if true
is returned. If the expression returns false
, the action will not be executed.
Code inside the checks
Comparison operators:
Operator | Name |
---|---|
> | more than |
< | less than |
>= | more than or equal to |
<= | less than or equal to |
Equality operators:
Operator | Name | Description |
---|---|---|
== | approximate equality | with casting argument types |
=== | strict equality | without casting argument types |
!= | “approximate inequality” | with casting argument types |
!== | strict inequality | without casting argument types |
Any values within the checks are converted to Boolean datatype. All numbers except 0
are true
, with 0
being false
. All lines except the empty string are true
, empty string ''
is false
.
Logical operators.
- The
&&
operator or “logical AND” returns true only if both conditions, to the left and right of it, return true. - The operator
||
or “logical OR” returns true if any of the conditions to the left or to the right of it return true. - The
!
operator or “logical negation” changes the Boolean value of the expression to the right of it to the opposite value.
Continue