0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Solidity】(tips)三項演算子に関して

Last updated at Posted at 2023-02-19

三項演算子はif ~ elseを一文で書きたいときに使う演算子です。

【三項演算子の書き方】 → 条件式 ? 条件が真の値 : 条件が偽の値

下記hello関数の場合、20以下の数字を渡すと"Ohayo"が返され、20より大きい数字を渡すと "Konnichiha"が返されます。
if else文より簡潔にビジュアルでわかりやすいので、状況に応じて積極的に使っていきたいです。

// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;

contract Sample{
    //
    function hello(uint _x)public pure returns (string memory){
        return _x >20 ? "Ohayo" : "Konnichiha";  
    }
}

※上記コードの動作確認はwebブラウザであるRemix IDEを使用

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?