The document discusses loop control in programming, covering switch-case commands, nested loops, break, and continue commands. It explains the syntax and usage of each command with examples, illustrating how they function within loops. The tutorial aims to provide foundational knowledge for handling loops and conditions effectively in programming.
This slide introduces the concepts of loop control in programming, including switch-case commands, nested loops, break command, and continue command.
This slide explains the switch-case structure, highlighting its advantage over if-elseif statements by allowing execution of different code based on scalar or string values.
Slide outlines the syntax of the switch-case statement, detailing the evaluation process and command execution based on matched case values.
Further details the switch-case execution flow and conditions where commands are executed or skipped based on matched case values.
An example demonstrating switch-case usage for inputting an ID and displaying corresponding names or a message if the ID is not a member.
This slide introduces nested loops, describing the concept of placing one loop within another, applicable to both for and while loops.
Provides the syntax for implementing nested loops using for and while statements in programming.
Illustrates a nested loop example that displays elements of a 2D matrix by iterating through its rows and columns.
Shows an additional nested loop example for generating and populating a matrix with calculated values using while loops.
Describes the 'break' command behavior, which terminates loops and discusses how it affects multiple nested loops.
An example program to find and display prime numbers, demonstrating the use of the break command to exit from inner loops.
Introduces the 'continue' command which skips the current iteration of the loop and proceeds to the next iteration.
An example showing how the continue command is used to skip an iteration for a specific value in a while loop.
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