Skip to main content
Engineering LibreTexts

7.4.1: Compound if, elseif Logic Examples

  • Page ID
    89058
  • \( \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}}\)

    When we want to test if a variable is between 2 values, we can use compound if or elseif logic. 

     

    Example \(\PageIndex{1}\) BodyTemperature Compound if-else with 'or' logic

    % Body Temperature version 1
    % Normal body temperatures are between 36.1°C and 37.2°C
    % https://medlineplus.gov/ency/article/001982.htm
    % For this script, the user will input their body temperature:
    body_temp = input('Enter your body temperature in degrees C: ')
    if ((body_temp < 36.1) | (body_temp > 37.2))  % if (body_temp < 36.1) or if  (body_temp > 37.2)
       disp('Your body temperature is outside the normal range.')
       if (body_temp < 36.1) 
          disp('It is lower than normal.')
       else 
          disp('It is higher than normal.')
          disp('You may have an infection or illness.')
       end
    else
          disp('Your body temperature is normal.')
    end

    Solution

    Add example text here.

    Example \(\PageIndex{2}\) Divergent Car Control

    This is an example of a divergent series. Instead of the terms getting smaller, these terms get larger in absolute value. The loop is broken when the terms get too large.

    % Divergent loop due to unstable feedback
    This models vehicles on a freeway with 3 lanes.
    Initial conditions:
    Car 1 is in freeway lane 1, on the left next to the center divider.
    A semi truck is in lane 2 
    Car 3 is in freeway lane 3, on the right next to the shoulder.
    Cars 1 and 3 can't see each other becuz of the semi truck.
    Action:
    Car 1 passes the truck and begins to move to the right into lane 2.
    Car 3 also passes the truck begins to move to the left into lane 2
    So the driver of car 1 turns hard to the left. 
    Due to the speed of the car and the delay to realize how far he has turned,
    each time he "over corrects"--that is, he turns too far each time. This is called unstable feedback.

    x is the number of feet car 1 he has moved to the right, measured from the center of lane 3
    Negative values of x are to the left of the center of lane 3.

    The for loop is broken if either the change in position, dx, gets too large or if the total position error gets too large.

    Solution

    % Initialize 
    x(1) = 3; % Position at time 1 when he make his 1st correction
    r = 2.1;  % Over correction factor
    for m = 2:20  % m is the interation index
    %  The change in position each iteration isDiverent_car_control_figure.jpg
       dx = -r*x(m-1);
       % Add this to the current position:
       x(m) = x(m-1) + dx;
       % If either the absolute value of dx is greater than 15, 
       % or the absolute value of x is greater than 10
       %  then use the "break" keyword to stop looping.
       if ((abs(dx) > 15) | (abs(x(m)) > 10))
          break;
       end
    end

    .

    .


    This page titled 7.4.1: Compound if, elseif Logic Examples is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Carey Smith.