Cant figure out why this code isn't working...

I have it so it enters what the value of I is and im trying so that it has a response of I counting down to 0. ie if I=3 it goes k=3; k=2; k=1; k=0.
k = I; %display each number form user entered integer to zero
while I>k>0
k=k-1
end

Answers (1)

I don't know where you've seen the syntax I>k>0. Certainly, not in any matlab course. I>k>0 is processed as (I>k)>0. (I>k) will be false (0) the first time and would be true (1), if k were to become less than I. If (I>k) is false, then the expression is in effect 0>0 which is false. Hence your while doesn't start true, the loop is never entered. If it had become true, then as said (I>k) would be 1, and 1>0 is always true so you would never exit the loop.
while I>k && k>0
is the proper syntax. Or even simpler
for k = I:-1:1
disp(k)
end

This question is closed.

Products

Release

R2018a

Asked:

on 3 Aug 2018

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!