{"content":{"title":"Solidity  If 语句","body":"`if` 语句的行为与其他语言完全相同\r\n\r\n```solidity\r\n\r\ncontract ExampleIfStatement {\r\n    function isAMultipleOfTen(uint256 x)\r\n        public\r\n        pure\r\n        returns (bool) {\r\n            if (x % 10 == 0) {\r\n                return true;\r\n            } else {\r\n                return false;\r\n            }\r\n    }\r\n}\r\n\r\n```\r\n\r\n`if` 语句中的参数必须是一个布尔值。请注意，上面的代码等同于以下代码。\r\n\r\n```solidity\r\n\r\ncontract ExampleIfStatement {\r\n    function isAMultipleOfTen(uint256 x)\r\n        public\r\n        pure\r\n        returns (bool) {\r\n            bool isMul = x % 10 == 0;\r\n            if (isMul) {\r\n                return true;\r\n            } else {\r\n                return false;\r\n            }\r\n    }\r\n}\r\n\r\n```\r\n\r\n与 Python 或 JavaScript 等动态语言不同，你不能执行以下操作\r\n\r\n```solidity\r\n\r\nfunction isNotZero(uint256 x)\r\n    public\r\n    pure\r\n    returns (bool) {\r\n        if (x) {\r\n            return true;\r\n        } else {\r\n            return false;\r\n        }\r\n}\r\n\r\n```\r\n\r\nSolidity 也支持“else if”结构，但我们会假设你已经熟悉它的样子。\r\n\r\nSolidity 不像 Java 和 C 那样有 **switch** 语句。\r\n\r\n**练习题目**\r\n\r\n[IfStatement](https://github.com/RareSkills/Solidity-Exercises/tree/main/IfStatement)\r\n\r\n### 了解更多\r\n\r\n查看  [Solidity 训练营](https://learnblockchain.cn/openspace/1) ，以了解更多关于智能合约开发和代币标准的内容。\r\n \r\n\r\n>- 原文链接： [rareskills.io/learn-soli...](https://www.rareskills.io/learn-solidity/if-statements)\r\n>- 登链社区 AI 助手，为大家转译优秀英文文章，如有翻译不通的地方，还请包涵～"},"author":{"user":"https://learnblockchain.cn/people/20722","address":null},"history":null,"timestamp":1740625243,"version":1}