Skip to main content
Mathematics LibreTexts

5.2: If Statements

  • Page ID
    53668
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    An if statement is used to do different things depending on the value of a variable. A standard example of this is the piecewise version of the absolute value. Mathematically, we write:

    \[ |x| = \begin{cases} x & x\geq 0, \\ -x & x<0\end{cases}\]

    We could write this as a function as

    function absValue(x::Number) 
      if x >= 0
    	return x 
      else
        return -1*x
      end
    end
     
     

    Just a check, let's find the absolute value of 3: 

    absValue(3)
     

    And check the absolute of -7 by editing the code block above. 

    And If-then-else with More than 2 choices

    You may need more than 2 choices on an if statement. Recall the quadrants of the \(xy\)-plane start at I for the upper right and increase as you traverse counterclockwise. A function could be written:

    function quadrant(x::Real,y::Real) 
      if x > 0 && y > 0
        return "I"
      elseif x < 0 && y > 0
        return "II"
      elseif x < 0 && y < 0
        return "III"
      elseif x > 0 && y < 0
        return "IV" 
      else
        return "NONE" 
      end
    end
     

    Notice that the format is that you start with an if boolean and then the other checks are ifelse and then the final one (if nothing is satified) is just else. Also, the reason we have final else statement is that technically if you are on an axis, then you are not in a quadrant, so that is the reason for the last option.

    Ternary If-Then-Else

    Often, an if-else statement is quite short and you want to a value depending on a condi- tion. The absolute value example was such an example. There is what is called a ternary if-else statement that has the form:

    condition ? value_if_condition_is_true : value_if_condition_is_false
    

    which returns value_if_condition_is_true if condition is true otherwise value_if_condition_is_false is returned. The absolute value example above can be written as a single line:

    absVal2(x::Real)=(x>=0) ? x : -x
     

    and once you practice with this, it will be easy to read and much shorter (1 line versus 7). Be careful with the syntax of this. It is required that the expressions around the ? be padded with spaces to parse correctly. The error is reasonably clear if you don’t write it correctly.

    Alternatively, you can write this as

    absVal3(x::Real)=ifelse(x>=0,x,-1*x)
     

    which is the same as the ternary if-then-else with a different syntax. 

    Exercise

    Write the recursive factorial function from Section XXXX using the ternary if-then-else.

     


    This page titled 5.2: If Statements is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Peter Staab.

    • Was this article helpful?