-
Notifications
You must be signed in to change notification settings - Fork 0
/
SolidityLoop.sol
58 lines (45 loc) · 1.1 KB
/
SolidityLoop.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
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.2 <0.9.0;
contract LoopContract
{
// while
// _number 5
// digit 1*2*3*4*5(_number)
function exeWhile(int _number) pure public returns(int)
{
//factorial
int fact=1;
int digit=1; // init 1
while( digit <= _number) // cond 2
{
//fact =fact *
fact *= digit;
digit++; // step 3
}
return fact;
}
//do.. while
function exeDoWhile(int _number) pure public returns(int)
{
//factorial
int fact=1;
int digit=1; // init 1
do {
fact = fact * digit;
digit++; // step 2
}while(digit <= _number); // cond 3
return fact;
}
//for
function exeFor(int _number) pure public returns(int)
{
int fact=1;
int digit;
//for( init ; cond ; step )
for( digit=1 ; digit <=_number ; digit++ )
{
fact *= digit;
}
return fact;
}
}