Programming Language/Solidity
Solidity - For and While Loop
Yongari
2022. 12. 23. 19:12
Solidity supports for, while, and do while loops.
Solidity 언어는 for문, while문 do while 문등을 지원합니다. 즉 반복문을 지원합니다.
Don't write loops that are unbounded as this can hit the gas limit, causing your transaction to fail.
무한 루프를 작성하지 마십시오. 가스 한계에 도달하여 거래가 실패할 수 있습니다.
솔리디티에서 무한루프를 사용하면 가스 비용은 무한대로 비용이 들 수 있습니다.
For the reason above, while and do while loops are rarely used.
위의 이유로 while and do while 루프는 거의 사용되지 않는다.
풀이코드 설명
// SPDX-License-Identifier: MIT
// 라이선스 명시
pragma solidity ^0.8.13;
// 솔리디티 버전 명시
contract Loop {
function loop() public {
// for loop
for (uint i = 0; i < 10; i++) {
if (i == 3) {
// Skip to next iteration with continue
continue;
}
if (i == 5) {
// Exit loop with break
break;
}
}
// while loop
uint j;
while (j < 10) {
j++;
}
}
}
Remix - Ethereum IDE
remix.ethereum.org