Académique Documents
Professionnel Documents
Culture Documents
111
Programming for Engineers
WEEK 4
Conditional Statement, IF & ELSE
Maya K Endoh
If Statement
The if statement is used to determine whether or not a
statement or group of statements is to be executed
General form:
if condi&on
ac&on
end
Check the
dierence
between disp
and fprinY
if num<0
disp('The number is changed to the absolute value, as it is
negative')
num=abs(num);
end
Will be used to check the entered values sign to run the
dierent script or func'on
Sample solu'on
%This script calculates the volume of a hollow sphere
%prompts the user for the radius
What IF riro ?!
ri = input('Enter the inner radius: ');
ro = input('Enter the outer radius: ');
volume= 4/3 * pi * (ro^3 - ri^3);
volume=fn_hol_vol_chap3(ri,ro);
fprintf('The hollow volume of inner radius of %.2f and outer radius of
%.2f is\n',ri,ro)
fprintf('%.2f.\n',volume)
7<5 or
nonzero value
which represent
true
If sentence is
TRUE
>> number<5||number>0
>> if number<5||0
disp('yes')
end
TRUE or FALSE?
7<5 or zero
value which
represent false
If sentence is
FALSE
Thus skipped
If-else Statements
The if-else statement chooses between two ac'ons
General form:
if condi'on
One and only one ac'on is
TRUE
ac'on1
executed
else
which one depends on the
FALSE
value of the condi'on
ac'on2
ac'on1 if it is logical true
end
ac'on2 if it is false
Ex) error-checking
Check for errors in the input to a script
Check that the user enters invalid/valid value for the script
Chose the ac'on by if-else statement
condition3
condition2
condition1
conditionN
if condi'on1
ac'on1
elseif condi'on2
ac'on2
elseif condi'on3
ac'on3
% etc: there can be many of these
else
ac'onn % the nth ac'on
end
Compare with
nested if-else
statement
ra > rb
ra < rb
ra = rb
ra and rb should be posi've value!
switch (switch_expression)
case {caseexp1}
switch_expression=caseexp1
ac:on1
case {caseexp2}
ac:on2
A case_expression cannot
case {caseexp3}
include rela'onal operators
ac:on3
such as <, or, > for comparison
% etc: there can be many of these
against the switch_expression.
otherwise
To test for inequality, use if,
ac:onN
elseif, else statements.
end
this can be used when comparing the switch_expression to see if it is equal to the
values on the case labels (the otherwise clause handles all other possible values)
EX)
Write a script that will prompt the user for a temperature in
Celsius, and convert to either Fahrenheit or to Kelvin.
The script will print the corresponding temperature in the scale
specied by the user (either F or K). For example, the output
might look like this:
Enter the temp in degrees C: 29.3
Do you want K or F? F
The temp in degrees F is 84.7
Sample Solu'on
temp_convert.m
% Converts a temperature from C to F or K
cel = input('Enter the temp in degrees C: ');
choice = input('Do you want F or K? ', 's');
clear temperature;
switch choice
case {'F','f'}
temperature=fn_CtoF(cel);
fprintf('The temp in degrees F is %.1f\n', temperature)
case {'K', 'k'}
temperature=fn_CtoK(cel);
fprintf('The temp in degrees K is %.1f\n', temperature)
otherwise
disp('Unknown Units. Please re-enter')
end
fn_CtoF.m
fn_CtoK.m
function F=fn_CtoF(C)
function K=fn_CtoK(C)
F=(9/5).*C+32;
K=C+273.15;
end
end
The is func'ons
There are many is func'ons in MATLAB that
essen'ally ask a true/false ques'on, and
return logical 1 for true or 0 for false
isleDer : 1 for every character in a string is a leqer
of the alphabet or 0 if it is not
isempty : 1 if the variable argument is empty, or 0
if not
iskeyword : 1 if the string argument is a keyword,
or 0 if not in MATLAB