Вы находитесь на странице: 1из 8

Lab #3:CONTROL FLOW IN MATLAB SSUET/QR/114

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.

3.1 IF/ELSE IF STATEMENT


An IF statement can be used to execute code once when the logical test (expression)
returns a true value (anything but 0). An "else" statement following an “if ” statement
is executed if the same expression is false (0).

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

team1_score = rand() % a random number between 0 and 1


team2_score = rand() % a random number between 0 and 1

if(team1_score > team2_score)


disp(’Team 1 wins!’) % Display "Team 1 wins!"
elseif(team1_score == team2_score)
disp(’It’s a tie!’) % Display "It’s a tie!"
else
disp(’Team 2 wins!’) % Display "Team 2 wins!"
end

CE -302:Signals and Systems 1


Lab #3:CONTROL FLOW IN MATLAB SSUET/QR/114

3.2 SWITCH STATEMENT


Switch statements are used to perform one of several possible sets of operations,
depending on the value of a single variable. They are intended to replace nested "if"
statements depending on the same variable, which can become very cumbersome.

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.

3.3 FOR LOOP


To execute a block of code a specific number of times, we can use a for loop. A for
loop takes a counter variable and a vector. MATLAB executes the statements in the
block once for each element in the vector, with the counter variable set to that
element.

SYNTAX:

for iterator = startvalue:increment:endvalue


statements
end
EXAMPLE 3.2

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

% After the loop, sum = 100


3.4 WHILE LOOP
A while loop works the same way as an if statement, except that, when the MATLAB
interpreter reaches the “end” keyword, it returns to the beginning of the while block
and tests the condition again. MATLAB executes the statements in the while block
repeatedly, as long as the condition is true. A break statement within the while loop
will cause MATLAB to skip the rest of the loop.

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!’)

% This will display:


%3
%2
%1
% Blastoff!
CLASS ACTIVITY
1. How many times will this program print "Hello World"?

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')

CE -302:Signals and Systems 3


Lab #3:CONTROL FLOW IN MATLAB SSUET/QR/114

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

Also explain the code in your words.


Ans. This program will print sir syed university of engineering & technology 55 times
because outer loop runs 5 times while inner loop will run 11 times as it’s initial value
is 0.

4. What sequence of numbers will the following for loop print?

n = 10;
for j = 1:n
n = n-1;
j
end

Explain why this code does what it does.


Ans. This program run until n becomes 0 and j becomes 10. Variable j is doing
increment while n is doing decrement of 1 from it’s last value.
HOME TASK
5. Show the output of the “for loop” program and also explain the logic in your
words.

FOR LOOP PROGRAM


P=10;
G=4;
X=[6 zeros(1,P-1)];
for k=2:P
X(k)=X(k-1)+G
end

CE -302:Signals and Systems 4


Lab #3:CONTROL FLOW IN MATLAB SSUET/QR/114

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

6. Modify the “for loop” program to terminate the sequence at 30.

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.

WHILE LOOP PROGRAM


Z=6;
CE -302:Signals and Systems 5
Lab #3:CONTROL FLOW IN MATLAB SSUET/QR/114

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

CE -302:Signals and Systems 6


Lab #3:CONTROL FLOW IN MATLAB SSUET/QR/114

17. Create a program to make a calculator by using SWITCH statement.


Code:

Output:

CE -302:Signals and Systems 7


Lab #3:CONTROL FLOW IN MATLAB SSUET/QR/114

18. In Matlab what is the function of Break and Continue command?


Ans: Break function is used to break/stop the loop while continue function
is used to resume/continue the loop.
19. What is the function of zeros and ones?
Ans: zeros is often used to allocate a variable to a known size and ones
could be used to allocate a variable with a constant.

CE -302:Signals and Systems 8

Вам также может понравиться