Skip to content

Sign of a number

Java if / else if / else

Same task in: TypeScriptRust

Flowchart (ISO 5807)

YesNoYesNoStartInput nn > 0Return 1Endn < 0Return −1EndReturn 0EndFigure 1 — Sign.sign

Source code

Java
class Sign {
    static int sign(int n) {
        if (n > 0) return 1;
        else if (n < 0) return -1;
        else return 0;
    }
}

The editor opens with this code — change it and the chart redraws.

Language
Java
Constructs
if / else if / else
Topic
Branching
Formats
Word, Visio, draw.io, PDF, SVG

Related examples