Ch6 Loops in MATLAB II

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Chapter VI.

LOOPS IN MATLAB - II
Outline
Nested loops
break and continue commands

4
Nested loops
In nested loops, on each iteration of outer loop, the inner
loop is executed completely. This continues until the outer loop
ends.

for i=1:n
for j=1:m
........ inner outer
........ loop loop
........
end
end
Every time i increases by 1, the inner loop executes m times.
Overall, the group of commands are executed nxm times.
Nested loops - Examples
for i=1:5 for i=1:5 sum=0;
sum=0; for j=1:5 for i=1:5
for j=1:5 sum=0; for j=1:5
sum=sum+j; sum=sum+j; sum=sum+j;
end end end
disp(sum*i); disp(sum*i); disp(sum*i);
end end end

15 5 15
30 10 60
45 15 135
60 20 240
75 25 375
Example- Multiplication table using nested loops
for i=1:10
for j=1:10
prod=i*j;
fprintf('%d*%d=%d\n', i, j, prod);
end
disp('-----------------------');
end
Write a MATLAB Program to find Pythagorean
Triplets using nested loops
A Pythagorean triplet is a set of three positive integers
a, b and c such that a2 + b2 = c2 and a<b<c.

Write a program in MATLAB to find all Pythagorean


Triples with values smaller than a given number.
MATLAB Program to find Pythagorean Triplets
n=input('Enter a number');
for a=1:n-1
for b=a+1:n
c=(a*a+b*b)^0.5;
if round(c)==c && c<n
fprintf('%d-%d-%d\n',a,b,c);
end
end
end
The break and continue commands
In the for and while loops, the program flow can be controlled
with the break statement.
When inside a loop (for and while), the break command
terminates the execution of the loop (the whole loop, not just the
last pass). When the break command appears in a loop, MATLAB
jumps to the end command of the loop and continues with the
next command (does not go back to the for command of that
loop).

1-10
The break command
If the break command is inside a nested loop, only the nested
loop is terminated.
When a break command appears outside a loop in a script, or
function file, it terminates the execution of the file.
The break command is usually used within a conditional
statement. In loops, it provides a method to terminate the looping
process if some condition is met. For example, if the number of
loops exceeds a predetermined value, or an error in some
numerical procedure is smaller than a predetermined value.
When typed outside a loop, the break command provides a means
to terminate the execution of a file, such as if data transferred into
a function file is not consistent with what is expected.
The continue command
The continue command can be used inside a loop
(for and while) to stop the present pass and start the
next pass in the looping process.
The continue command is usually a part of a
conditional statement. When MATLAB reaches the
continue command, it does not execute the
remaining commands in the loop, but skips to the end
command of the loop and then starts a new pass.
break and continue commands - Examples
i=1; for i=1:20
while i<=10 if mod(i,2)==0
if i==4 continue;
break; end
end fprintf('%d\n',i);
fprintf('%d',i); end
i=i+1;
end 1
3
123 5
7
9
11
13
15
17
19

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy