3

Obviously this is rookie question, however I do not believe Solidity docs, nor W3Schools explicitly state this.

In the return statement, is the second line in the block equivalent to a second Then statement? My (basic) understanding is that a return statement signals immediately halt to the function execution.

function mint(address receiver, uint amount) {
        if (msg.sender != minter) return;
        balances[receiver] += amount;
    }

Many thanks.

2 Answers 2

3

The construct if (condition) expression; is equivalent to if (condition) then expression. Essentially, if the sender is not equal to the minter, then it returns, so the rest of the function is not executed. Otherwise, execution continues.

So in this case, the line after the if statement is equivalent to being in an else clause.

2

is the second line in the block equivalent to a second Then statement?

No.

function mint(address receiver, uint amount) {
        if (msg.sender != minter) {
            return;
        }
        balances[receiver] += amount;
    }

The code in the question is identical to the above. Note that it is different from treating the balances[receiver] += amount; as part of the Then statement, which would be:

function mint(address receiver, uint amount) {
        if (msg.sender != minter) {
            balances[receiver] += amount;  // this is now inside the "if" but has completely changed the meaning from the original code
            return;
        }
    }

Also, for each if there's only one Then statement. To have a second Then statement would require another if (or else if).

It's also worth pointing out that some coding conventions highly encourage the use of braces {} around the Then statement, even if the Then statement only contains one statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.