Continue in For loop

4 views (last 30 days)
Mert ÖZKAYA
Mert ÖZKAYA on 22 Nov 2021
Commented: Mert ÖZKAYA on 22 Nov 2021
Hello,
I'm trying to make a for loop something like this. However the output is not like that I want.
Isn't it should be like:
1ler
1ler
0lar
1ler
But Matlab's output is just
1ler
Why it doesn't continue to the iteration?
A=[1 0 1 0 0 0 1];
for i=1:2:A(1,end)
if A(1,i)==1
display("1ler")
elseif A(1,i)==0
display("0lar")
else
display("error")
end
end
"1ler"

Accepted Answer

Yusuf Suer Erdem
Yusuf Suer Erdem on 22 Nov 2021
Hi Mert. Could you try like below. I just changed "for i=1:2:A(1,end)" into "for i=1:length(A)".
A=[1 0 1 0 0 0 1];
for i=1:length(A)
if A(1,i)==1
display("1ler")
elseif A(1,i)==0
display("0lar")
else
display("error")
end
end
  1 Comment
Mert ÖZKAYA
Mert ÖZKAYA on 22 Nov 2021
Thank you for your answer. Since I need only odd numbers for i value, I changed for i=1:length(A) into i=1:2:length(A).

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!