BME1901 Week 4
BME1901 Week 4
• Grading:
• Midterm exam 30%
• Project 30%
• Final exam 40%
3 control structures:
1. sequence structure,
2. selection structure,
3. repetition structure.
2. A selection structure allows the programmer to execute one command (or set
of commands) if some criterion is true and a second command (or set of
commands) if the criterion is false. A selection statement provides the means
of choosing between these paths, based on a logical condition . The
conditions that are evaluated often contain both relational and logical
operators or functions.
The operators <, >, <=, and >= use only the real part of
their operands for the comparison. The
operators == and ~= test real and imaginary parts.
x = [ 1, 2, 3, 4, 5];
y = [-2, 0, 2, 4, 6];
z = [ 8, 8, 8, 8, 8];
z>x & z>y
returns
ans =
11111
because z is greater than both x and y for every element. The statement
x>y | x>z
is read as “ x is greater than y or x is greater than z ” and returns
ans =
11100
if (comparison)
statements
end
if (condition1)
statement1;
elseif (condition2)
statement2;
elseif (condition3)
statement3;
else
statement4;
end
if(grade>90) %if_1
disp('A'); If statement of if_1
else
if (grade>80); if_2 Else statement(s) of if_1
disp('B'); If statement of if_2
else
if (grade>70) % if_3 Else statement(s) of if_2
disp('C'); If statement of if_3
else
disp('F'); Else statement(s) of if_3
end %end of if_3
end %end of if_2
end % end of if_1
Flow-chart
Y
Cond1 Statement1
N If statement
Flow-chart
Y
Cond1 Statement1
N If - else statements
Statement2
Y
Cond1 Statement1
Flow-chart
N
Y
Cond2 Statement2
N
If – elseif – else statements
Y
Cond3 Statement3
N
Statement4
• As a matter of fact, anything you can do with switch-case could be done with if-
elseif-else.
• However, the code is a bit easier to read with switch-case, a structure that allows
you to choose between multiple outcomes, based on some criterion.
If, when you run this script, you reply 'Boston' at the prompt, MATLAB responds
city =
Boston
$345