Combined Condition
A combined condition is logically grouping two or more conditions. A combined condition is used to test multiple conditions at once.
Syntax -
A combined condition uses AND and/or OR to combine multiple conditions together.
AND -
When we combine conditions using AND, all conditions must be true for the combined condition to be true.
For example - Employees who have a salary greater than 50000 and have been with the company for more than 5 years are eligible for bonus.
IF WS-SALARY > 50000
AND WS-YEARS-OF-SERVICE > 5
DISPLAY "ELIGIBLE FOR BONUS"
ELSE
DISPLAY "NOT ELIGIBLE FOR BONUS"
END-IF
OR -
When we combine conditions using OR, either one or both condition(s) needs to be true for the combined condition to be true.
For example - Employees who have a salary greater than 50000 or have been with the company for more than 5 years are eligible for bonus.
IF WS-SALARY > 50000
OR WS-YEARS-OF-SERVICE > 5
DISPLAY "ELIGIBLE FOR BONUS"
ELSE
DISPLAY "NOT ELIGIBLE FOR BONUS"
END-IF
Combining AND and OR Together -
We can also use both ‘AND’ and ‘OR’ in a single combined condition, but it’s important to use parentheses () to clarify the order in which the conditions are evaluated.
For example - Employees who have a salary greater than 50000 and have been with the company for more than 5 years, or who are in a managerial position are eligible for bonus.
IF (WS-SALARY > 50000 AND WS-YEARS-OF-SERVICE > 5)
OR WS-POSITION = 'MANAGER'
DISPLAY "ELIGIBLE FOR BONUS"
ELSE
DISPLAY "NOT ELIGIBLE FOR BONUS"
END-IF
Table for Result validation -
Condition C1 | Condition C2 | C1 AND C2 | C1 OR C2 | NOT (C1 AND C2) | NOT (C1 OR C2) |
---|---|---|---|---|---|
True | True | True | True | False | False |
False | True | False | True | True | False |
True | False | False | True | True | False |
False | False | False | False | True | True |