Branching constructs
MATLAB provides a number of language constructs for branching a program's control of flow.
if‐end Construct : The most basic construct is: if
end
Here the condition is a logical expression that will evaluate to either true or false (i.e., with values 1 or 0). When a logical expression evaluates to 0, program control moves on to the next program construction. You should keep in mind that MATLAB regards A==B and A<=B as functions with values 0 or 1.
Example:
>> a = 1;
>> b = 2;
>> if a < b
c = 3;
end
>> c c = 3
If‐else‐end Construct: Frequently, this construction is elaborated with
if
In this case if condition is 0, then program2 is executed.
If-elseif-end Construct: Another variation is
if
elseif
end
Now if condition1 is not 0, then program1 is executed, if condition1 is 0 and if condition2 is not 0, then program2 is executed, and otherwise control is passed on to the next construction.
-------------------------TASK 03--------------------------
For 0 < a ≤ 16, find the values of C defined as follows:
C = 4ab for 1 ≤ a ≤ 8
ab for 8 < a ≤ 16
and b = 12.
-------------------------TASK 04--------------------------
Rewrite the following statements to use only one if statement.
if x < y
if z < 5
w = x*y*z end
end
Dostları ilə paylaş: |