# 7.1: if, elseif, else with Error Checking

$$\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}}$$

By Carey A. Smith

This section gives more if, elseif, else, examples. It also shows how to use the error() function.

##### Example $$\PageIndex{1}$$ add2() function with error checking

Previously we saw the add1() function, which adds 2 vectors. add1() assumed that the 2 vectors were the same length, but did not check that.

Here we create add2(), which checks that the 2 vectors are the same length.

function result = add2(a,b)

% This adds vectors a and b % The inputs are a and b % It first checks to see that a and b are the same length % If not, it creates an error message exits the function. % The output is "result" a_len = length(a) b_len = length(b) if(a_len == b_len)     result = a + b; else % else, the vectors are different lengths, so it creates an error message     result = 0     error('length(a) must = length(b)')

    % The error() function displays the message plus the function name and line where the error occured.

    % Then error() cause the program to stop end

----

This test script demonstrates normal runs with both add1() and add2() and runs with inputs that are not the same length.

add2_test.m

%% Test add functions % (1 pt) Clear any variables etc. clear all; close all; format compact; clc;

%% First, test add1.m and add2.m with 2 vectors that are the same length x = 1:4 y = [0, 1, 10, 100] z = add1(x,y) z = add2(x,y)

%% Second, test add1.m and add2.m with 2 vectors that are not the same length w = [1, 2] y = [0, 1, 10, 100] z = add1(w,y) z = add2(w,y)

The error() function in add2.m displays this message:

Error using add2 (line 14) length(a) must = length(b)

.

##### Exercise $$\PageIndex{1}$$ Area of a Square

(1 pt) Write a function that computes the area of a square that expects a scalar input, not a vector.

The output will be the variable "sq_area"
(1 pt) Use the input function to get the length of a side with this function:
s = input('Enter the length of one side of a square: ');

(2 pts) Check that s is a single number.
% Use s_len = length(s) to find the length of s
% If s_len is not 1, then display s and generate this error:
error('Area_of_square.m: the input must be a single number') % Note the space after the colon
% The error function causes the program to stop

% (2 pts) Check that s is >= 0 (The side can't be < 0)
% If s is negative, then display s and generate this error:
error('Area_of_square.m: the input is < 0')
% The error function causes the program to stop

% (2 pts) Compute sq_area = the area of the square. (Don't call this variable "area", because that is the name of a plotting function.)

% Test it with these input values:
s = 3
s = -1
s = 1:3

See the attached file Area_of_square.m

## Homework:

##### Exercise $$\PageIndex{2}$$ Area of a Circle

% (1 pt) Write a function that computes the area of a circle.
% (1 pt) Use the input function to get the radius with this function:
r = input('Enter the radius of a circle: '); % Note the space after the colon
% The input() function displays the message, then reads a number and stores it in the variable r

% (2 pts) Check that r is a single number.
% Use r_len = length(r) to find the length of r
% If r_len is not 1, then display r and generate this error:
error('Area_of_circle.m: the input must be a single number')
% (2 pts) Check that r is >= 0
% If r is negative, then display r and generate this error:
error('Area_of_circle.m: the input is < 0')
% (2 pts) Compute the area of the circle.

% Test your function with these input values:
r = 3
r = -1
r = 1:3

Since these are entered via the input() function, you do not need a test file.