Cant figure out why this code isn't working...
Info
This question is closed. Reopen it to edit or answer.
Show older comments
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)
Guillaume
on 3 Aug 2018
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
1 Comment
Stephen23
on 3 Aug 2018
The correct syntax is also described in the MATLAB documentation:
This question is closed.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!