Leo language for Dummies. Logical Operators

Illy’s Web3 blog
6 min readAug 2, 2023

--

Intro

In the world of programming and computational logic, logical operators serve as foundational elements that aid in making decisions based on specific conditions. While they are often applied to boolean values, they can also operate on other data types, producing varied results. Understanding these operators is crucial as they are a key component of control structures in programming, such as conditional statements and loops.

What Are Logical Operators?

Logical operators are symbols or words used to connect two or more expressions, providing a means to determine the logic between the inputs. For instance, they can help determine if both inputs are true, if at least one input is true, or if an input is false.

Why Are Logical Operators Important?

Decision Making:

They play a pivotal role in determining the flow of program execution based on conditions.

Data Filtering:

Used extensively in database operations to filter out data based on multiple conditions.

Simplifying Complex Conditions:

Logical operators can be combined to form compound conditions, thus making code concise and readable.

Logical Operators of Leo Language

In Leo, there are eight primary logical operators. Each has its unique characteristics and applications. In this article, we will delve into a detailed examination of each of these operators, illustrating their functionality with practical examples. Whether you are a beginner or an experienced developer, understanding these fundamental operators will enrich your skillset and deepen your knowledge in Leo programming.

Data Types that we’ll be considering

1st type — bool:

Description:

This represents a Boolean data type, which can have one of two values: “True” or “False”. In most programming contexts, "True” is typically interpreted as 1 and “False” as 0.

Use Case:

Commonly used for conditions and logical operations.

2nd type — i8-i128:

Description:

These are signed integers of various bit lengths. The number after the “i” indicates the number of bits. For example, “i8” means an 8-bit signed integer. Being "signed" means they can represent both positive and negative numbers.

Range:

  • i8: -128 to 127
  • i16: -32,768 to 32,767
  • i32: -2,147,483,648 to 2,147,483,647
  • i64: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
  • i128: Large range, useful for very big numbers.

Use Case:

Used for general purpose counting, arithmetic, and mathematical operations.

3rd — u8-u128:

Description:

These are unsigned integers of various bit lengths. Being “unsigned” means they can only represent non-negative numbers.

Range:

  • u8: 0 to 255
  • u16: 0 to 65,535
  • u32: 0 to 4,294,967,295
  • u64: 0 to 18,446,744,073,709,551,615
  • u128: Extremely large range, suitable for very big numbers.

Use Case:

Used in scenarios where only non-negative numbers are expected, like indexing arrays or certain mathematical computations.

Leo’s Logical Operators

1st Operator — “NOT”

Operands: “!”, “.not()”

Description:

Returns the inverse of the input.

Usage:

Often used in conditions where a particular statement should be executed if a condition is not true.

Supported types:

  • bool,
  • i8, i16, i32, i64, i128,
  • u8, u16, u32, u64, u128.

Example:

let a: bool = true;
let b: bool !a;
return b; //Returns false
-------------------------
let a: bool = false;
let b: bool !a;
return b; //Returns true

Real-world application:

Used to reverse a condition in control statements.

2nd Operator — “AND”

Operands: “&”, “&=”,“.and()”

Description:

Returns true if both operands are true.

Usage:

Useful when multiple conditions must be satisfied simultaneously.

Supported types:

  • bool,
  • i8, i16, i32, i64, i128,
  • u8, u16, u32, u64, u128.

Example:

let a: bool = true;
let b: bool = true;
return a & b; //Returns true
-----------------------------
let a: bool = true;
let b: bool = false;
return a & b; //Returns false
-----------------------------
let a: bool = false;
let b: bool = true;
return a & b; //Returns false
-----------------------------
let a: bool = false;
let b: bool = false;
return a & b; //Returns false

Real-world application:

Applied in scenarios like authentication where both username and password need to be correct.

3rd Operator — “OR”

Operands: “|”, “|=”,“.or()”

Description:

Returns true if at least one operand is true.

Usage:

Handy when any one of the multiple conditions should be satisfied.

Supported types:

  • bool,
  • i8, i16, i32, i64, i128,
  • u8, u16, u32, u64, u128.

Example:

let a: bool = true;
let b: bool = true;
return a & b; //Returns true
-----------------------------
let a: bool = true;
let b: bool = false;
return a & b; //Returns true
-----------------------------
let a: bool = false;
let b: bool = true;
return a & b; //Returns true
-----------------------------
let a: bool = false;
let b: bool = false;
return a & b; //Returns false

Real-world application:

Used in scenarios where multiple alternative conditions can lead to the same result.

4th Operator — “XOR”

Operands: “^”, “^=”,“.xor()”

Description:

Returns true if exactly one operand is true.

Usage:

Useful in toggle scenarios.

Supported types:

  • bool,
  • i8, i16, i32, i64, i128,
  • u8, u16, u32, u64, u128.

Example:

let a: bool = true;
let b: bool = true;
return a & b; //Returns false
-----------------------------
let a: bool = true;
let b: bool = false;
return a & b; //Returns true
-----------------------------
let a: bool = false;
let b: bool = true;
return a & b; //Returns true
-----------------------------
let a: bool = false;
let b: bool = false;
return a & b; //Returns false

Real-world application:

Applied in algorithms that require toggling bits, like in cryptography.

5th Operator — “NAND”

Operands: “.nand()”

Description:

Opposite of AND.

Usage:

Less commonly used, but can simplify circuitry in hardware design.

Supported types:

  • bool.

Example:

let a: bool = true;
let b: bool = true;
return a & b; //Returns false
-----------------------------
let a: bool = true;
let b: bool = false;
return a & b; //Returns true
-----------------------------
let a: bool = false;
let b: bool = true;
return a & b; //Returns true
-----------------------------
let a: bool = false;
let b: bool = false;
return a & b; //Returns true

Real-world application:

NAND gates are fundamental in electronics and can be used to build other gates.

6th Operator — “NOR”

Operands: “.nor()”

Description:

Opposite of OR.

Usage:

Again, more prominent in electronic circuits.

Supported types:

  • bool.

Example:

let a: bool = true;
let b: bool = true;
return a & b; //Returns false
-----------------------------
let a: bool = true;
let b: bool = false;
return a & b; //Returns false
-----------------------------
let a: bool = false;
let b: bool = true;
return a & b; //Returns false
-----------------------------
let a: bool = false;
let b: bool = false;
return a & b; //Returns true

Real-world application:

NOR gates are basic building blocks in some digital circuits.

7th Operator — “conditional AND”

Operands: “&&”

Description:

The conditional logical AND operator “&&”, also known as the "short-circuiting" logical AND operator, computes the logical AND of its operands. The result of “a && b” is “True” if both “a” and “b” evaluate to “True”. Otherwise, the result is “False”. If “a” evaluates to “False” , “b” isn't evaluated.

Usage:

Useful for situations where the second condition should be evaluated only if the first one is true.

Supported types:

  • bool.

Example:

let a: bool = true;
let b: bool = true;
return a & b; //Returns true
-----------------------------------------
let a: bool = true;
let b: bool = false;
return a & b; //Returns false
-----------------------------------------
let a: bool = false;
let b: bool = true; // b isn't evaluated
return a & b; //Returns false
-----------------------------------------
let a: bool = false;
let b: bool = false; // b isn't evaluated
return a & b; //Returns false

Real-world application:

Used in code optimizations where the second evaluation is heavy or risky.

8th Operator — “conditional OR”

Operands: “||”

Description:

The conditional logical OR operator “||”, also known as the "short-circuiting" logical OR operator, computes the logical OR of its operands. The result of “a || b” is “True” if either “a” or “b” evaluates to “True”. Otherwise, the result is “False”. If “a” evaluates to “True”, “b” isn't evaluated.

Usage:

Useful for situations where the second condition should be evaluated only if the first one is false.

Supported types:

  • bool.

Example:

let a: bool = true;
let b: bool = true; // b isn't evaluated
return a || b; //Returns true
----------------------------------------
let a: bool = true;
let b: bool = false; // b isn't evaluated
return a || b; //Returns true
-----------------------------------------
let a: bool = false;
let b: bool = true;
return a || b; //Returns true
-----------------------------------------
let a: bool = false;
let b: bool = false;
return a || b; //Returns false

Real-world application:

Helps in short-circuiting code paths for efficiency.

In Conclusion

Logical operators, while seemingly simple, form the foundation of decision-making in software. A deep understanding of their mechanics and applications is invaluable for both novice and experienced developers, and across different programming paradigms and applications.

Stay curious, keep learning, and delve deeper into the Aleo ecosystem — the journey is just beginning. Join the community here:

--

--

No responses yet