Control Flow in Matlab Objective: 3.1 If/Else If Statement
Control Flow in Matlab Objective: 3.1 If/Else If Statement
Control Flow in Matlab Objective: 3.1 If/Else If Statement
LAB # 3
CONTROL FLOW IN MATLAB
OBJECTIVE
To learn control flow in Matlab.
THEORY
MATLAB interpreter moves through a script linearly, executing each statement in
sequential order. However, we can use several structures to introduce branching and
looping into the flow of our programs.
MATLAB executes the statements in an “if ” block or a “elseif ” block only if its
associated condition is true. Otherwise, the MATLAB interpreter skips that block. If
none of the conditions were true, MATLAB executes the statements in the “else”
block (if there is one).
SYNTAX:
if expression
statements
elseif expression2
statements
end
EXAMPLE 3.1
SYNTAX:
switch variable
case value1
statements
case value2
statements
...
otherwise
statements
end
The end is only necessary after the entire switch block, not after each case. If you
terminate the switch statement and follow it with a "case" statement you will get an
error saying the use of the "case" keyword is invalid. If this happens it is probably
because you deleted a loop or an "if" statement but forgot to delete the "end" that
went with it, thus leaving you with surplus "end"s. Thus MATLAB thinks you ended
the switch statement before you intended to.
The otherwise keyword executes a certain block of code (often an error message) for
any value of variable other than those specified by the "case" statements.
SYNTAX:
r = [9 4 0];
c = [8 7 5];
sum = 0;
for i = 1:3 % The counter is ’i’, and the range is ’1:3’
sum = sum + r(i) * c(i); % This will be executed 3 times
end
CE -302:Signals and Systems 2
Lab #3:CONTROL FLOW IN MATLAB SSUET/QR/114
The while statement executes code until a certain condition evaluates to false or zero.
SYNTAX:
while condition
statements
end
EXAMPLE 3.3
i=3
while i > 0
disp(i)
i = i - 1;
end
disp(’Blastoff!’)
for a=0:50
disp('Hello World')
end
Ans. 51 Times
2. How many times will this program print " communication system"?
for a=-1:-1:-50
disp('communication system')
end
Ans. 50 Times
3. How many times will this program print " sir syed university of engineering &
technology'"?
for a=10:10:50
for b=0:0.1:1
disp('sir syed university of engineering & technology')
end
end
n = 10;
for j = 1:n
n = n-1;
j
end
In this code. Loop will run 10 times and there is initial value 6 and other 9
values will be 0 then in each turn there will be increment of 4 i.e 6+4=10 and
other values will remain 0 then again there will increment of 4 in last value i.e
10+4=14
Code:
7. P=7;
8. G=4;
9. X=[6 zeros(1,P-1)];
10. for k=2:P
11. X(k)=X(k-1)+G
12. end
13.
14. Show the output of the “while loop” program and also explain the logic in
your words.
L=4;
while 1
Z=Z+L
if Z>100,break,end
end
Initial value will be 10 because when loop start value will be 6+4=10 then there
will be addition of 4 each time in previous value i.e 10+4=14 until it will be
greater than 100 and also we print value before the checking condition of loop.
15. In “while loop” program explain why the generated result is 102? Why more
than 100? Why not more than 102?
Ans. Because we have put end condition of loop that when value of z will be
greater than 100 then loop will break/stop and as per our condition and
variable initialization, loop will end on 102 and also we print value before the
checking condition of loop.
16. Modify the “while loop” program to generate the result; Z is 90.
Code:
Z=0;
L=10;
while 1
Z=Z+L
if Z>80,break,end
end
Output: