-
Notifications
You must be signed in to change notification settings - Fork 2
/
solidity_basics_loops.sol
91 lines (72 loc) · 3.39 KB
/
solidity_basics_loops.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13; // always use the last available version
// conditionals handle decisions, where a conditional statment will give a true or false result
// we can trigger different instructions based on this result
// if a variable meets a condition, return true, else return false
contract Conditionals {
// function to return true if the parameter passed is bigger than 5
function bigNumber(uint _number) public pure returns(bool){
if(_number > 5) { // the condition returns true if it is met
return true;
}
else {
return false;
}
}
uint[] public values = [1,3,5,3,8,32,88,0,21]; // array full of numbers
// function to check how many numbers are in the array
function howMany() public view returns (uint) {
uint keep_count = 0; // initialize a count variable
// a for loop continues as long as the parameters specify
for(uint i = 0; i < values.length ; i++ ){ // for loop to go through all the numbers in the array
keep_count ++; // add 1 to keep_count for each iteration
}
return keep_count; // be careful about the brackets
}
// check how many numbers bigger than 5 are in the array
function howManyBig() public view returns (uint){
uint keep_count = 0;
for(uint i = 0; i < values.length ; i++ ){ // for loop to go through all the numbers in the array
if(bigNumber(values[i])) { // if the number is bigger than 5 continue
keep_count ++; // add 1 to keep_count if the condition is met
}
}
return keep_count; // returns the count
}
// a while loop continues until a condition is met
function goToZero() public pure returns(uint){
uint times = 10;
uint howMany = 0;
while (times > 0){ // keeps looping until the variable times equal 0
howMany = howMany + 2; // each loop adds 2 to howMany and remove 1 from times
times = times - 1;
}
return howMany;
}
// more practical example, check if the address that calls the function is the owner of the contract
address public owner; // a variable containing and address naed owner
constructor(){ // when the contract is created, assigns the address to the variable owner
owner = msg.sender;
}
// function to check if the address calling it is the owner
function areYouOwner() public view returns(bool){
if(msg.sender == owner){
return true;
} else{
return false;
}
}
// most common conditional operators:
// ! (logical negation)
// && (logical conjunction, “and”)
// || (logical disjunction, “or”)
// == (equality)
// != (inequality)
// comparisons operators:
// <= (smaller or equal than)
// < (smaller than)
// == (equal to)
// != (different than)
// >= (bigger or equal than)
// > (bigger than)
}