electricalenggtutorial.blogspot.com
1
INTRODUCTION
In this lecture we will discuss more
about Loop control.
- Switch – case commands
- Nested lops
- break command
- continue command
2
e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m
SWITCH - CASE
• There is no conditional statement.
• It choose code to execute based on
value of scalar or string, not just
true/false
• Different codes can be executed
based on the value of the scalar.
• It is easier than if-elseif-end
commands if the conditions are more.
e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m
3
SWITCH-CASE SYNTAX
e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m
4
SWITCH-CASE SYNTAX…
• The expression after switch command is
evaluated first.
• If the obtained value is equal to
value1 – then commands below case value1
is executed up to
next case or otherwise or
or end statement.
• Similarly if the obtained value is equal
to value2 – then then commands below case
value2 is executed.
• If switch-expression not equal to any of
the values in case statement, commands
after otherwise executed.
• If otherwise command is not present, no
commands are executed
e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m
5
SWITCH-CASE EXAMPLE
id = input(‘enter your id’)
switch id
case ‘321456'
display (‘You name is Martin’)
case ‘320423'
display (‘You name is Allen’)
case ‘332458'
display (‘You name is Mohan’)
case ‘321564'
display (‘You name is Vicky’)
otherwise
display (‘You are not a member of the
class’)
end
e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m
6
NESTED LOOP
• A loop or conditional statement is
placed inside another loop or
conditional statement.
• Simply, a loop within a loop
• Both for and while loops can be
nested.
e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m
7
NESTED LOOP SYNTAX
for ii = 1:n
for jj = 1:m
<Commands>
end
end
while expression1
while expression2
< Commands >
end
end
e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m
8
NESTED LOOP EXAMPLE
Program to display a 2D matrix
M = [1 2 3; 4 5 6; 7 8 9];
[r c] = size(M);
for Row = 1:r
for Col = 1:c
fprintf('Element(%d,%d)= %d.n',
Row, Col, M(Row, Col))
end
end
e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m
9
NESTED LOOP EXAMPLE…
I=1; N=10;
while I<=N
J=1;
while J<=N
A(I,J)=1/(I+J-1);
J=J+1;
end
I=I+1;
end
e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m
10
BREAK COMMAND
• When inside a loop (for and
while), break terminates
execution of loop
• MATLAB jumps from break to end
command of loop, then continues
with next command (does not go
back to the for or while command
of that loop).
• ‘break’ ends whole loop, not just
last pass
• If break inside nested loop, only
nested loop terminated (not any
outer loops)
• break command in script or
function file but not in a loop
terminates execution of file
e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m
11
BREAK EXAMPLE
Program to display prime numbers from 1 to
50. The program exit from the inner loop
using break, if the number is not a prime.
for n=2:50
for m=2:50
if(~mod(n,m))
break
end
end
if(m > (n/m))
fprintf('%d is primen', n); end
end
e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m
12
CONTINUE COMMAND
• Used to stop current iteration
and start next iteration in a
loop
• Can be used with both for- and
while loops
• ‘continue’ is used usually as
part of a conditional
statement.
• When the program execution
reaches ‘continue’ it stop
executing the remaining
commands in loop and jump to
the end command of loop and
then starts a new iteration
e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m
13
CONTINUE EXAMPLE
a = 1;
while a < 20
if a == 15
a = a + 1;
continue; % skip the iteration for a=15
end
fprintf('value of a: %dn', a);
a = a + 1;
end
e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m
14

MATLAB Programming - Loop Control Part 2

  • 1.
  • 2.
    INTRODUCTION In this lecturewe will discuss more about Loop control. - Switch – case commands - Nested lops - break command - continue command 2 e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m
  • 3.
    SWITCH - CASE •There is no conditional statement. • It choose code to execute based on value of scalar or string, not just true/false • Different codes can be executed based on the value of the scalar. • It is easier than if-elseif-end commands if the conditions are more. e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m 3
  • 4.
    SWITCH-CASE SYNTAX e le c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m 4
  • 5.
    SWITCH-CASE SYNTAX… • Theexpression after switch command is evaluated first. • If the obtained value is equal to value1 – then commands below case value1 is executed up to next case or otherwise or or end statement. • Similarly if the obtained value is equal to value2 – then then commands below case value2 is executed. • If switch-expression not equal to any of the values in case statement, commands after otherwise executed. • If otherwise command is not present, no commands are executed e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m 5
  • 6.
    SWITCH-CASE EXAMPLE id =input(‘enter your id’) switch id case ‘321456' display (‘You name is Martin’) case ‘320423' display (‘You name is Allen’) case ‘332458' display (‘You name is Mohan’) case ‘321564' display (‘You name is Vicky’) otherwise display (‘You are not a member of the class’) end e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m 6
  • 7.
    NESTED LOOP • Aloop or conditional statement is placed inside another loop or conditional statement. • Simply, a loop within a loop • Both for and while loops can be nested. e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m 7
  • 8.
    NESTED LOOP SYNTAX forii = 1:n for jj = 1:m <Commands> end end while expression1 while expression2 < Commands > end end e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m 8
  • 9.
    NESTED LOOP EXAMPLE Programto display a 2D matrix M = [1 2 3; 4 5 6; 7 8 9]; [r c] = size(M); for Row = 1:r for Col = 1:c fprintf('Element(%d,%d)= %d.n', Row, Col, M(Row, Col)) end end e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m 9
  • 10.
    NESTED LOOP EXAMPLE… I=1;N=10; while I<=N J=1; while J<=N A(I,J)=1/(I+J-1); J=J+1; end I=I+1; end e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m 10
  • 11.
    BREAK COMMAND • Wheninside a loop (for and while), break terminates execution of loop • MATLAB jumps from break to end command of loop, then continues with next command (does not go back to the for or while command of that loop). • ‘break’ ends whole loop, not just last pass • If break inside nested loop, only nested loop terminated (not any outer loops) • break command in script or function file but not in a loop terminates execution of file e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m 11
  • 12.
    BREAK EXAMPLE Program todisplay prime numbers from 1 to 50. The program exit from the inner loop using break, if the number is not a prime. for n=2:50 for m=2:50 if(~mod(n,m)) break end end if(m > (n/m)) fprintf('%d is primen', n); end end e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m 12
  • 13.
    CONTINUE COMMAND • Usedto stop current iteration and start next iteration in a loop • Can be used with both for- and while loops • ‘continue’ is used usually as part of a conditional statement. • When the program execution reaches ‘continue’ it stop executing the remaining commands in loop and jump to the end command of loop and then starts a new iteration e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m 13
  • 14.
    CONTINUE EXAMPLE a =1; while a < 20 if a == 15 a = a + 1; continue; % skip the iteration for a=15 end fprintf('value of a: %dn', a); a = a + 1; end e l e c t r i c a l e n g g t u t o r i a l . b l o g s p o t . c o m 14