Logical operators like AND "&&" and OR "| |" may be utilized within if-else statements. If you have two conditions (i.e. multiple Boolean expressions) which must be true or one of them is enough to make the statement true respectively,
logical operators if statement may be used to test more than one condition at the
same time.
The following table displays the | | (OR) operator results (true or false) of the two Boolean expressions. These results will be considered by the if statement:
Boolean Expressions |
Boolean Expressions |
A || B |
True | True | True |
True | False | True |
False | True | True |
False | False | False |
The following table displays the use of "&&" (AND) logical operator to guarantee that the two conditions are true:
Boolean Expressions |
Boolean Expressions |
A && B |
True | True | True |
True | False | False |
False | True | False |
False | False | False |
Example:
The following example includes two conditions. It is enough to prove one of the conditions to consider the if condition is true because you used the | | operator.
main() {
var Age = 16;
var DOB = 1998;
if(Age >= 18 || DOB >= 1998){
print('He is Authorized');
}
else {
print('He is Not Authorized');
}
}
The run result of this code follows:
If you replaced the | | operator with && operator as illustrated in the following code:
main() {
var Age = 16;
var DOB = 1998;
if(Age >= 18 && DOB >= 1998){
print('He is Authorized');
}
else {
print('He is Not Authorized');
}
}
You will get the following run result because the if condition will be considered true only if the two conditions are true. In this case, if the condition is false, else statement will work as shown below:
* This topic is a part of lesson 2 of Flutter Application Development course. For more information about this course, click here.