# Thoughts on the Engineering Industry

## Four Basic Types of Loops and Conditionals in Matlab

Hello everyone.  I’ve been enjoying my spring break from school.  I’ve looked over material for a school project and plan on applying for jobs this weekend.  I hope your week is going well.  Today I wanted to describe two types of functions in Matlab, the loop and conditional, as it is used in coding for Matlab.  A loop is a function used to repeat a cycle.  A conditional is used to decide which calculation or function to perform based on a set of criteria.  Matlab has four types of loops and conditionals available by default: if conditional, while loop, for loop, and switch conditional.  In this post, I will describe to you each type, how to use them, and give a basic example.

If conditional

Description: The if conditional is a function that takes a piece of data, checks for a mathematical condition, and then uses that to apply the desired function.

Standard Usage: This function is commonly used to perform calculations that accurately account for large quantities of diverse data.

Example: In this case, y=-2+2*4=6

%

x=4;

if x < 0

y=x^2;

elseif x < 2

y=x;

else

y=-2+2*x;

end

%

While Loop

Description: The while loop executes a function as long as a mathematical condition is true.

Standard Usage: This is commonly used to iterate a process until a specific criterion is met.

Example: In this case, y=1+2+3+4+5+6+7+8+9=45

%

y=0; x=0;

while x < 10

y=y+1;

x=x+1;

end

%

For Loop

Description: This is a loop that executes a function for a set number times.

Standard Usage: This is commonly used to perform calculations on a specific set of data for each individual data element.  This can also be used to perform calculations for cases where a specific number of iterations are used.

Example: y=[2,4,6,8]

%

x=1:1:4; y=zeros(4);

for i = 1:length(x)

y(i)=2*x(i)

end

%

Switch Conditional

Description: This is a conditional that takes a value or string variable and uses it choose which calculation to perform.

Standard Usage: This is commonly used to write a program to execute a variety of calculations depending on a user specified situation.

Example: In this case, y=4

%

x=2;

calc=’square’

switch lower(calc)

case {‘linear’}

y=x

case {‘square’}

y=x^2

case {‘cubic’}

y=x^3

otherwise

disp(‘y is unknown’)

end

%

These are the four types of loops expressed in a very basic way.  This information can also be found by typing help and the name of the function, ie ‘help if’.  However, I thought this would be helpful because this shows them in their simplest form while also telling you when and how they are used.  When learning to program in Matlab, the issues I found when using loops and conditionals were not always how to find the information but figuring what function to use and how to use it in a specific circumstance.  Hopefully, this post will help with that if you are initially learning this language and get stuck with these types of functions.

If you a learning how to use loops in Matlab right now, what do you find difficult and easy?  If you already have experience using this, what did you find difficult and easy when you first learned this?  Is there still anything specific about these loops and conditionals that cause issues?  Let me know if you have any questions and have a good week!