Tuesday, June 16, 2009

do till condition is met "continue" example

This is a nice example.



clear

y = 1:100;
count = 0
for j = 1:length(y)

if y(j)>50, continue, end
count = count+1
x(j)=y(j)
pause


end



This takes x until y(j)>50. After the condition is met it simply passes out.


Better explained by:
clear

y = 1:100;
count = 0
for j = 1:length(y)
disp('go')
if y(j)>50, continue, end
count = count+1
x(j)=y(j)
pause


end

continue keeps on taking values does not break the chain.
while break breaks it and comes out of the loop



Contrasted over the
clear

y = 1:100;
count = 0
for j = 1:length(y)
disp('go')
if y(j)>50, break, end
count = count+1
x(j)=y(j)
pause


end


lastly;


clear
clc
y = 1:100;
count = 0
for j = 1:length(y)
disp('go')
if y(j)<20||y(j)>50, break, end
count = count+1
x(j)=y(j)
pause


end

this does not even work while continue will keep looking for the situation.
Break just breaks while continue keep working escaping to the next one and checking!

No comments: