官术网_书友最值得收藏!

Coding a loop

Loops are very helpful when we need to repeat the execution of a piece of code a certain number of times. Rather than copying and pasting the same lines of code, making the code block longer, we use this programming construct to add brevity to our code. Loops can be finite or infinite in nature. For the infinite loop, we generally provide a break statement to come out of it.

The smart contract in Figure 3.16 is using a finite loop to calculate the summation of a finite number of integers:

Figure 3.16: Loop inside a Solidity smart contract

The output on remix IDE is shown in Figure 3.17. As discussed earlier, keep a watch on the amount of ethers that is getting decreased in the Account tab for each call of the myFirstLoop function:

Figure 3.17: Execution of myFirstLoop inside a smart contract

The best way to check the inner working of a loop in runtime is to go to the Launch debugger tab and manually push the horizontal scroll bar from left to right, as shown in Figure 3.18. Try it! It will be fun:

Figure 3.18: Debugging mode to identify states of execution

At this point, I hope you are quite comfortable playing with smart contracts using the remix IDE in your browser. However, what I have shown is just a happy-path scenario. The development community for Ethereum is highly agile and is rolling out newer changes very quickly. By the time you start reading this book, there might be slight or major modifications in the IDE. Once you start coding, you will realize the nuances and even capture many peculiar bugs down the line. The next section is all about what you can do when you are stuck with a problem or a code bug.

主站蜘蛛池模板: 襄城县| 宣城市| 随州市| 南汇区| 尼木县| 通河县| 澜沧| 富裕县| 沅陵县| 迭部县| 延吉市| 孟州市| 晋宁县| 昭觉县| 吴忠市| 江山市| 沈丘县| 云浮市| 呈贡县| 剑川县| 乌兰县| 承德市| 贡嘎县| 赤水市| 凤庆县| 贵定县| 泸西县| 江口县| 宜都市| 孟津县| 安阳市| 曲周县| 简阳市| 十堰市| 邮箱| 蒲城县| 呼和浩特市| 怀化市| 南投市| 县级市| 交口县|