Академический Документы
Профессиональный Документы
Культура Документы
5
Conditional statements in MATLAB
if-else-end
if-elseif-else-end
Nested conditional statements
if CONDITION
if CONDITION
EXECUTE THESE
EXECUTE THESE
end
EXECUTE THESE
elseif CONDITION2
EXECUTE THESE
else
EXECUTE THESE
if CONDITION1
end
5 switch
EXECUTE THESE
elseif CONDITION2
EXECUTE THESE
case METHOD1
EXECUTE THESE
case METHOD2
EXECUTE THESE
else
case METHOD3
EXECUTE THESE
end
METHOD_STRING
EXECUTE THESE
end
if CONDITION1
end
if CONDITION1a
if CONDITION2a
EXECUTE THESE
elseif CONDITION2b
EXECUTE THESE
end
elseif CONDITION1b
EXECUTE THESE
else
EXECUTE THESE
end
7
switch
METHOD_STRING
case METHOD1
if CONDITION
EXECUTE THESE
end
case METHOD2
EXECUTE THESE
case METHOD3
if CONDITION1
EXECUTE THESE
elseif CONDITION2
EXECUTE THESE
else
EXECUTE THESE
end
end
Conditional statements are basically used to decide what to do upon having choices to make and also when the
conditions needed to make the right choice is also available. We shall study two conditional statements, which
are if-elseif-else-end and switch-case-end statement.
if-elseif-else-end
Say for example, if the choices that you have are to study and to not to study and the conditions that are to be
met are that you need to progress and you need not progress, then the conditional statement takes the form:
First, variable A is assigned with a value of 10. Then, if block begins. It compares the value of A and 8. Only if
found less than 8, then the command B = 1 is executed. If it finds that A < 8 is false, the command B = 1 is
ignored and proceeds to next control statement, in this case, it is end and the if-end ends.
Example: The use of if-else-end statements: look at the following code segment:
A = 10;
if A < 8
B = 1;
else
B = 0;
end
First, A is assigned to 10. The first condition, A < 8 is not met, so MATLAB proceeds further to the next
control statement which is else. The moment it sees this, it executes whatever command is given between this
control statement and the end statement. Hence, the command B = 0 is executed.
If in case, A were assigned with a value of 5, then the command B = -1 would get executed.
Example: The use of if-elseif-else-end statements: look at the following code segment:
A = 10;
if A < 8
B = 1;
elseif A > 8 || A <=10
B = 0;
elseif A == 11 && A ==13
B = -1;
else
B = -2;
end
First, A is assigned to 10. The first condition, A < 8 is not met, so MATLAB proceeds further to the next
control statement which is elseif. The condition to be met is written as A > 8 && A <=10 . Here, the ||
signifies or operation. This condition has two sub-conditions A > 8 and A <= 10. Since, we use ||, any one of
these commands needs to be satisfied. In this case, since the value of A is 10, the second sub-condition satisfies.
The moment one of the condition is satisfied, all the other control statements are ignored and after executing the
command B = -1, MATLAB directly jumps to end and ends the execution of this code segment.
switch-case-end
In some cases, given that you have something to be done, which could be anything like make
many options and a lot of sorting of tasks to be done according to the available options, and that the task to be
done , then the switch case command comes very much handy.
Name_of_the_option1 = 'Coffee';
Name_of_the_option2 = 'AssignMEnt';
Name_of_the_option3 = 'morning_6am';
NOTE: When something is enclosed within single inverted comma, it means it is not a numerical value but a
string.
For example: 456 is a number while 456 is a string (that is a set of characters. Here, 4 5 and 6 are all treated
as alphabets instead of numbers).
String assignments are case sensitive on their own (provided that no extra commands are provided to convert
the string case to what we need).
For example: 'AssignMEnt' is different from 'assignment'.
Commands such as upper() and lower() are used to convert all the characters in a strings to upper case and
lower case respectively.
For example: the command upper(MATlab) returns MATLAB while the command
lower (MATlab) returns matlab.
Name_of_the_option1 = 'Coffee';
Name_of_the_option2 = 'AssignMEnt';
Name_of_the_option3 = 'morning_6am';
switch lower(Name_of_the_option1)
switch lower(Name_of_the_option2)
case {'coffee'}
case {'AssignMEnt'}
disp('Do you want coffee')
disp('Did you forget do do ?)
case 'idly'
case 'Test'
disp('Only idly ?. What else do
disp( Study well)
you need ?')
case 'chatney'
case 'chatney'
disp('dont want idly?')
disp('dont want idly?')
otherwise
disp(All is well')
otherwise
disp(' I dont get it. What is it end
that you want ')
end
switch Name_of_the_option3
case {'morning_6AM', 0600hrs}
disp(Waky wakyyy. Breakfast
ready)
case '1000PM'
disp( No more movies. Go to
sleep)
case '0230pm'
disp('dont want idly?')
otherwise
disp(All is well')
end
The MethodToUse is set to ColonOperator in the beginning. The switch case statement begins from the next
line. The method to be compared against the different cases is first converted to lower case using lower()
command at the end of which the ColonOperator becomes colonoperator. Next, different cases are
presented. In the first case, this lower(MethodToUse) (whos value is colonoperator) is compared against
colonoperator next to the first case statement. MATLAB finds that there is a match. The moment it finds a
match, it ignores all other case statements and executes all the commands between itself and the next control
statement. Thus, the commands, disp(Using the colon operator to make a linear array) and A = 1:2:10; are
executed.
If in case, MethodToUse = linspace, then the second case-statement would satisfy and the commands
disp(Using linspace function to make a linear array) and A = linspace(0,10,2) would get executed.
If you were to use upper() instead of lower(), the above code would be:
MethodToUse = ColonOperator;
switch upper(MethodToUse)
case COLONOPERATOR
disp(Using the colon operator to make a linear array)
A = 1:2:10;
case LINSPACE
disp(Using linspace function to make a linear array)
A = 1:2:10;
end