Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integer and boolean type introduction & about #66

Merged
merged 10 commits into from
Jul 12, 2024
4 changes: 2 additions & 2 deletions concepts/booleans/.meta/config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"blurb": "<todo>",
"blurb": "As in most other programming languages, a Boolean type in Cairo has two possible values: true and false.",
"authors": [
"misicnenad"
"rajeebkm"
],
"contributors": []
}
74 changes: 74 additions & 0 deletions concepts/booleans/about.md
Original file line number Diff line number Diff line change
@@ -1 +1,75 @@
# The Boolean Type

In Cairo, the Boolean type is essential for logical operations and conditional statements. Booleans in Cairo are represented as one `felt252` in size, ensuring efficient use of space while maintaining the simplicity of true/false logic.
rajeebkm marked this conversation as resolved.
Show resolved Hide resolved

The Boolean type in Cairo is specified using the keyword `bool`. This enables the declaration and manipulation of Boolean variables within the language. For instance, consider the following example:

```rust
fn main() {
let t = true;

let f: bool = false; // with explicit type annotation
}
```

In this example, two Boolean variables are declared. The variable $t$ is assigned the value true implicitly, while the variable $f$ is explicitly annotated with the bool type and assigned the value false.
rajeebkm marked this conversation as resolved.
Show resolved Hide resolved

One important aspect to note is that when declaring a bool variable in Cairo, it is mandatory to use either the true or false literals as the value. This means that integer literals, such as $0$ or $1$, cannot be used as substitutes for false or true. The strict enforcement of this rule ensures type safety and prevents potential logical errors in smart contract development.
rajeebkm marked this conversation as resolved.
Show resolved Hide resolved


The following code snippet demonstrates how to use Boolean types in Cairo. It shows the declaration of Boolean variables, assignment of Boolean expressions, and usage of assertions to verify the correctness of the Boolean logic.
rajeebkm marked this conversation as resolved.
Show resolved Hide resolved

```rust
rajeebkm marked this conversation as resolved.
Show resolved Hide resolved
fn main() {
let t: bool = true;
let true_expr = 5 == 5;
assert(t == true_expr, 'this is true');

let f: bool = false;
let false_expr = 7 == 5;
assert(f == false_expr, 'this is false');
}
```

## Declaration of Boolean Variables
```rust
let t: bool = true;
```
This line declares a Boolean variable t and assigns it the value true.

## Boolean Expression Evaluation
```rust
let true_expr = 5 == 5;
```
Here, a Boolean expression 5 == 5 is evaluated. Since the expression is true, true_expr is assigned the value true.

## Assertion for True Expression
```rust
assert(t == true_expr, 'this is true');
```
This assertion checks if the value of t is equal to true_expr. Since both are true, the assertion passes, and the message 'this is true' confirms the expected result.

## Declaration of Another Boolean Variable
```rust
let f: bool = false;
```
This line declares another Boolean variable f and assigns it the value false.

## Boolean Expression Evaluation for False
```rust
let false_expr = 7 == 5;
```
Here, another Boolean expression 7 == 5 is evaluated. Since the expression is false, false_expr is assigned the value false.

## Assertion for False Expression
```rust
assert(f == false_expr, 'this is false');
```
This assertion checks if the value of f is equal to false_expr. Since both are false, the assertion passes, and the message 'this is false' confirms the expected result.

## Summary
This code snippet illustrates the use of Boolean variables and expressions in Cairo. It highlights the following key points:

1. Declaring Boolean variables with explicit type annotations.
2. Evaluating Boolean expressions and assigning the results to variables.
3. Using assertions to verify that Boolean variables match the expected values of evaluated expressions.
4 changes: 4 additions & 0 deletions concepts/booleans/introduction.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# Introduction

In Cairo, similar to most other programming languages, the Boolean type represents a binary state with two possible values: `true` and `false`.

This allows developers to implement logical conditions and control structures efficiently, enabling robust and dynamic smart contract development.
7 changes: 6 additions & 1 deletion concepts/booleans/links.json
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
[]
[
{
"url": "https://book.cairo-lang.org/ch02-02-data-types.html#the-boolean-type",
"description": "Boolean type in the Cairo book"
}
]
Loading