



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of loop types in MATLAB, including while loops, for loops, and nested loops. Learn the syntax and usage of each loop type, and see examples to help you understand how they work. If you're a university student studying computer science or engineering, this document could be useful for your studies as lecture notes, summaries, or cheat sheets.
Typology: Summaries
1 / 7
This page cannot be seen from the preview
Don't miss anything!




while
a = 10;
while( a < 20 ) fprintf('value of a: %d\n', a); a = a + 1; end
value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19 For loop
for index = values
Example for a = 10: fprintf('value of a: %d\n', a); end
The Nested Loops
for m = 1 :j for n = 1 :k
while
for i=2: for j=2: if(~mod(i,j)) break; % if factor found, not prime end end if(j > (i/j))
fprintf('%d is prime\n', i); end end
2 is prime 3 is prime 5 is prime 7 is prime 11 is prime 13 is prime 17 is prime 19 is prime 23 is prime 29 is prime 31 is prime 37 is prime 41 is prime 43 is prime 47 is prime 53 is prime 59 is prime 61 is prime 67 is prime 71 is prime 73 is prime 79 is prime 83 is prime 89 is prime 97 is prime Loop Control Statements
Example a = 10;
while a < 20 if a == 15 % skip the iteration a = a + 1; continue; end fprintf('value of a: %d\n', a); a = a + 1; end
value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 16 value of a: 17 value of a: 18 value of a: 19