Commit 7e00a767 authored by Chris's avatar Chris
Browse files

Completed Operators

parent 6b2439a5
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -100,3 +100,15 @@ https://www.youtube.com/watch?v=AFma5JYgIIE&list=PLvfQp12V0hS2PQd9-X-E2AjmXj1o05
  - inheritance is done by merging two smart contracts together at compilation time
- child contracts will not have access to `private` variables of the parent contract, but it will have access to `internal` ones

## Part 4

this tutorial talks about operators you can use in Solidity

### Video link

https://www.youtube.com/watch?v=KlO23rhqEnw&list=PLvfQp12V0hS2PQd9-X-E2AjmXj1o05WOo&index=5

### Notes

- see **MyOperators.sol** for a list of operators you can use
- a **view function** is a function that does not change state: use keyword to help compiler and readability
+69 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract MyOperators {
    uint8 a = 1;
    uint8 b = 2;
    uint8 c = 3;

    uint8 newValue;
    bool myBool = false;

    /// @notice Arithmetic Operators (+, -, *, /, %, ++, --)
    function example_1() view public {
        a * b + c;
        c % b;
        // a++; // cannot do this in a view function: this is assignment
        // b--; // cannot do this in a view function: this is assignment
    }
     /// @notice Assignment Operators (=, +=, -=, *=, /=, %=)
    function example_2() public {
        newValue = a * b + c;
        a = a * b + c;
        b *= c;
    }
     /// @notice Comparison Operators (==, !=, <, <=, >, >=)
    function example_3() view public {
        bool result = false;
        if (a < b) {
            result = true;
        }
        if (a <= b) {
            result = true;
        }
        if (a == b) {
            result = true;
        }
        if (result == myBool) {
            result = true;
        }

    }
     /// @notice Logical Operators (!, &&, ||)
    function example_4() view public {
        bool result = false;
        if (a < b && b > c) {
            result = true;
        }
        if (a < b || b > c) {
            result = true;
        }
        if (a < b && b > c && a > 20) {
            result = true;
        }
        if (result == myBool) {
            result = true;
        }
        if (!myBool) {
            result = true;
        }
        result = !result;
        
    }
     /// @notice Conditional Operator (? A:B)
    function example_5() view public {
        uint8 result;
        result = a < b ? 100 : 50;
    }
}
 No newline at end of file