Logical Operations
In this section, we will discuss how to perform logical operations.
Logical Operators
To check if either or both of two boolean values (like the results from comparison operations) are true, we use logical operators.
VCSSL supports the following logical operators:
Symbol | Meaning | Supported Types | Details |
---|---|---|---|
&& | Logical AND | bool | The result becomes true only when both the right and left values are true. If either one is false, the result becomes false. |
|| | Logical OR | bool | The result becomes true if either the right or left value is true. The result becomes false only when both the right and left values are false. |
! | Logical NOT | bool | The result becomes false if the value is true, and true if the value is false. |
Combining Two Comparison Results
Let's look at an example combining two comparison results:
- Execution Result -
When you execute this, it will display true. Both b1 and b2 are true, so b1 && b2 also becomes true.
Now, let's see what happens with the following:
- Execution Result -
This time, it will display false. While b1 is true, b2 is false, causing b1 && b2 to evaluate to false. The "&&" operator returns true only if all compared values are true.
Mixing Comparisons and Logical Operations
In the previous examples, we firstly stored the results of comparison operations into bool type variables, and then performed logical operations on them. However, in practical code, it's cumbersome to create bool variables for every comparison.
Therefore, you can mix and write them as follows:
- Execution Result -
When you execute this, it will display true.
Combining Multiple Logical Operations
To process multiple logical operations together, you need to use parentheses like this:
- Execution Result -
This evaluates to "( true && true ) || ( false && false ) || false," which is equivalent to "true || false || false," so when executed, it displays true.
Negation
You may need the opposite of a comparison result (true becomes false, and vice versa). In such cases, you can use the negation symbol "!"
- Execution Result -
When you execute this, it displays false.
» How we translated this page
- What is VCSSL? - Features of VCSSL
- Let's Get Started with VCSSL! - Setting Up the Environment
- Variables
- Data Types
- Arithmetic Operations
- Comparison Operations
- Logical Operations
- Scope
- Control Structures
- Arrays
- Vector Operations
- Functions
- "main" Function
- System Functions and Constants
- Standard File I/O