ATX2
Logical Operators
By: Engr. Esmeraldo T. Guimbarda Jr.
“ if ” Function
Used to test for evaluating conditions during program operation,
for example, if an input value is greater than a certain value,
what should be done in that case.
Syntax:
The program will check if ( someVariable > 50)
whether the value of
“someVariable” is
greater than 50 or not. If {
so, what will have to do
but if it is not, just skip
to the function of this
// do something here
section.
}
Syntax:
The function of this if ( someVariable > 50)
command is to test
certain conditions,
which are written in {
brackets. If the
condition is true, follow
an instruction in braces
// do something here
but if the condition is
false, leave out the }
function of this section.
The conditional test which is in brackets will have to use these comparison
operators as follows.
x == y (x equal to y)
x != y (x not equal to y)
x<y (x less than y)
x>y (x greater than y)
x <= y (x less than or equal to y)
x >= y (x greater than or equal to y)
Logical operators
Performed a for comparison of if () statements and
there are 3 types;
● ! (not)
● && (and)
● || (or)
! (logic not)
The value is true when the comparison result is false.
if ( !x )
{
The result is
// ... true if x is false
(such as x = 0, the
} result is true)
&& (logic and)
Let a value is true when the comparison result of both sides is true.
if (x > 0 && x < 5)
{
The value is true
// ... when x is greater
than 0 and less than
} 5 (a value of 1 to 4).
|| (logic or)
The value is true when the comparison result shows that one of variables is true or
both variables are true.
if (x > 0 || y > 0)
{ It shows the
result is true when
// ...
the value of x or y is
} greater than 0.