if-statementjuliasuppress

(Julia) Not able suppress output using semicolon when assigning variable inside an IF statement


Why does adding a semicolon in an IF ELSE statement is unable to suppress the output display?

Unable to suppress output


Solution

  • Indeed as the Julia Manual explains:

    If an expression is entered into an interactive session with a trailing semicolon, its value is not shown.

    However, this statement refers to the whole entered expression. In your case the whole expression includes the if part so you should write:

    if condition
        ...
    else
        ...
    end;
    

    (note the semicolon afer end)

    In particular note, as explained here in the Julia Manual, that:

    if blocks also return a value, which may seem unintuitive to users coming from many other languages. This value is simply the return value of the last executed statement in the branch that was chosen

    Putting ; after end suppresses printing of the value returned by the if block.