Logical Operations

In this section, we will discuss how to perform logical operations.

- Table of Contents -

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:

TrueAndTrue.vcssl

- Execution Result -

true

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:

TrueAndFalse.vcssl

- Execution Result -

false

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:

CompAndLogic.vcssl

- Execution Result -

true

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:

LogicAndLogic.vcssl

- Execution Result -

true

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 "!"

Not.vcssl

- Execution Result -

false

When you execute this, it displays false.

Acknowledgement: We greatly appreciate the cooperation of two ChatGPT AIs in translating this page.
» How we translated this page